Forum Moderators: coopster
I have a csv file where each row contains an id number, the title of the article, and the article contents. I've displayed the latest 10 articles on my page with the following code;
$handle = fopen("crazyarticles.csv", "a+");
while (($data = fgetcsv($handle, 1000, " "))!== FALSE) {
echo "<TABLE bgcolor=\"black\" border=\"1\" width=\"550\"><TR><TD>" . $data[1] . "</td></tr>";
echo "<TR><TD>" . $data[2] . "</td></tr>";
$count++;
if ($count==10) {
echo "<a href=\"archives.php\">Previous Posts</a>";
exit();
}
}
fclose($handle);
This works fine, apart from one thing: it's displaying the articles in the order I wrote them rather than displaying the most recent (ie. it's reading from top to bottom from the CSV file).
Is there anyway I can get it to read from the bottom row up in the CSV so that my most recent article will be displayed at the top of the page? Any suggestions would be fantastic!
Cheers,
Neil
$handle = fopen("crazyarticles.csv", "a+");
$dataarray = array();
$count = 1;
while (($data = fgetcsv($handle, 1000, " "))!== FALSE) {
$dataarray[] = $data;
}
fclose($handle);
$dataarray = array_reverse($dataarray);
foreach($dataarray as $data){
echo "<TABLE bgcolor=\"black\" border=\"1\" width=\"550\"><TR><TD>" . $data[1] . "</td></tr>";
echo "<TR><TD>" . $data[2] . "</td></tr>";
$count++;
if ($count==10) {
echo "<a href=\"archives.php\">Previous Posts</a>";
exit();
}
}
Once this csv file gets pretty long, you'll either want to write your own function that parses exclusively those lines that you want, to save memory, or else switch to a database (even better).