Hi there,
I am new to PHP and have a problem. A pice of code works on my own test server but not when I upload to my webspace.
This code try's to read a file containing preformatted links, this file index's articles submitted by users. I want to extract the first 5 links from the file and reformat them for the main page (remove list item, bring on to one line etc).
this is the code
<?
#open file
$myfile = "/PATH/TO/MYDAT/FILE/data.dat";
#number of files wanted plus one 2 gives 3
$maxlatest =4;
# Article links file
if(file_exists($myfile)){
$fh = fopen($myfile, "r");
$old_news = fread($fh, filesize($myfile));
fclose($fh);
}
# get first five article links
$articles = explode("</li>", $old_news);
$articles = str_replace("<li>","",$articles);
$articles = str_replace("<br />"," by ",$articles);
$articles = str_replace("2001</span>","2001</span><br />",$articles);
$i=0;
foreach ($articles as $article){
if(count($articles)>$i){
if($maxlatest >= $i++){
print $article;
}
}
}
?>
----end code
The file it reads contains links as list items...
<li><a href="Sep-06-213155.php">Formatting iNdex Page</a><br /><span class="redtext"><b>Mike L</b></span> <span class="greytext">6th September 2001</span></li>
<li><a href="Sep-06-213155.php">Formatting iNdex Page22</a><br /><span class="redtext"><b>Mi</b></span> <span class="greytext">6th September 2001</span></li>
<li>
Each LI is on it's own line.
It works fine on my test server but I get a warning on my ISP's server ..
warning: invalid argument supplied in foreach() statement in /PATH/TO/SCRIPT.php on Line 25
Line 25 is the start of the foreach.
I have used this script to learn but am very confused why it does not work. I also think that maybe there is another way to do this?
I have it in my head that I am not using an array properly and this is why the foreach fails, but it does work on my linux box fine.
the only other thing I can think of is a version problem, my ISP has installed PHPver 4.0.4, mine is 4.0.5
Any help or advice would be greatly appreciated,
Best wishes
Mike