Jump to content

display order


esoteric

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/245025-display-order/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/245025-display-order/#findComment-1258567
Share on other sites

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;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/245025-display-order/#findComment-1258786
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.