esoteric Posted August 17, 2011 Share Posted August 17, 2011 This is the script i wrote to post news. How can i alter this script so it lists contents with the newest post at the the top? At the moment its posting post 1 post 2 post 3 I want it to post like; post3 post2 post1 so the last post i made is at the top, not the bottom, if that makes sense. <?php function getNewsList(){ $fileList = array(); // Open the actual directory if ($handle = opendir("news")) { // Read all file from the actual directory while ($file = readdir($handle)) { if (!is_dir($file)) { $fileList[] = $file; } } } rsort($fileList); return $fileList; } ?> <table width="500px"> <?php $list = getNewsList(); foreach ($list as $value) { $newsData = file("news/".$value); $newsTitle = $newsData[0]; $submitDate = $newsData[1]; unset ($newsData['0']); unset ($newsData['1']); $newsContent = ""; foreach ($newsData as $value) { $newsContent .= $value; } echo "<tr><th align='left'><h2>$newsTitle</h2></th><th align='right'>$submitDate</th></tr>"; echo "<tr><td colspan='2'>".$newsContent."<br/><hr size='1'/></td></tr>"; } ?> </table> thank you. Quote Link to comment Share on other sites More sharing options...
WebStyles Posted August 17, 2011 Share Posted August 17, 2011 that depends on how you're creating the files (why not use a database?). You're reading through a directory and adding each file content to an array, if the files are in the correct order in the directory (i.e. from oldest to newest), then all you need to do is reverse the array before displaying. check out array_reverse Quote Link to comment Share on other sites More sharing options...
esoteric Posted August 17, 2011 Author Share Posted August 17, 2011 I would prefer to add the news to a database but i never got it too work properly. It currently listing the newest listing at the bottom ill have a look into that array reverse Quote Link to comment Share on other sites More sharing options...
jcbones Posted August 17, 2011 Share Posted August 17, 2011 An alternative suggestion would be to array_pop(). <?php while(!empty($list)) { $newsData = file("news/". array_pop($list)); $newsTitle = $newsData[0]; $submitDate = $newsData[1]; unset ($newsData['0']); unset ($newsData['1']); $newsContent = ""; foreach ($newsData as $value) { $newsContent .= $value; } echo "<tr><th align='left'><h2>$newsTitle</h2></th><th align='right'>$submitDate</th></tr>"; echo "<tr><td colspan='2'>".$newsContent."<br/><hr size='1'/></td></tr>"; } ?> You could also sort the array based off of the filemtime(). This would be the best method, any others would possibly allow incorrect listings at times. Doing that, I would leave the foreach block intact, and change the function: <?php function getNewsList(){ $fileList = array(); // Open the actual directory if ($handle = opendir("news")) { // Read all file from the actual directory while (($file = readdir($handle)) !== false) { if (!is_dir($file)) { $fileList[filemtime('news/'.$file)] = $file; } } } krsort($fileList); return $fileList; } ?> Quote Link to comment Share on other sites More sharing options...
xyph Posted August 17, 2011 Share Posted August 17, 2011 Couldn't you just remove the rsort($fileList); from the getNewsList() function? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.