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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.