Jump to content

making comment listing


Twentyoneth

Recommended Posts

I have a page, where you can comment, making comments is fine, but listing them is seeming to be a bit of a task for me.

 

The problem is, it will display the first file (comment) in the directory. Rather than the latest or last.

 

<?php

$dir = "comments/";
$numberfile = "info/cammount.php"; //contains max number of comments allowed to be displayed
$topnumber = file_get_contents($numberfile);
$count = 0;

if (is_dir($dir)) {
   if ($dh = opendir($dir)) {
       while (($file = readdir($dh)) !== false) {
           if($file !== "." && $file !== ".." && $file !== "Thumbs.db" && $count < $topnumber) {
              //$file is the files in the "comments/" directory, the filename looks like this: 020107-11-57am - Test.php
              $date = substr($file, 0, 2) . "/" . substr($file, 2, 2) . "/" . substr($file, 4,2);
              $time = substr($file, 7, 2) . ":" . substr($file, 10, 4);
              $user = substr($file, 17, -4);
              $timestamp = ("Posted on <span class='date'>" . $date . "</span> at <span class='time'>" . $time . "</time> by <span class='user'>" . $user . "</span>");
              $commentlink = $dir.$file;
              $picfile = ("../" . $user . "/pictures/default/default.php");
              $picture = file_get_contents($picfile);
              $comment = file_get_contents($commentlink);
              echo "<div class='usercomment'>
   <table cellpadding='0' cellspacing='0' width='100%'>
      <tr>
         <td class='timestamp'>
            " . $timestamp . "
         </td>
      </tr>
      <tr>
         <td class='belowtimestamp'>
            <table cellpadding='0' cellspacing='0' width='100%'>
               <tr>
                  <td class='usercommentpic'>
                     <img src='" . $picture . "' width='120px' height='120px'></img>
                  </td>
                  <td class='usercommentcomment' vAlign='top'>
                     " . $comment . "
                  </td>
               </tr>
            </table>
         </td>
      </tr>
  </table>
</div>";
              $count++;
             }
       }
       closedir($dh);
   }
}

?>

 

Any ideas on how to get it to show the last 10 rather than the first 10?

Link to comment
https://forums.phpfreaks.com/topic/36676-making-comment-listing/
Share on other sites

Yes, but I have never used scandir, so I am not quite familiar with how to use it all that well :S

 

This is my current code:

<?php

$dir = "comments/";
$numberfile = "info/cammount.php";
$topnumber = file_get_contents($numberfile);
$count = 0;

if (is_dir($dir)) {
   if ($dh = opendir($dir)) {
       while (($file = scandir($dh, 1)) !== false) {
           if($file !== "." && $file !== ".." && $file !== "Thumbs.db" && $count < $topnumber) {
              $date = substr($file, 0, 2) . "/" . substr($file, 2, 2) . "/" . substr($file, 4,2);
              $time = substr($file, 7, 2) . ":" . substr($file, 10, 4);
              $user = substr($file, 17, -4);
              $timestamp = ("Posted on <span class='date'>" . $date . "</span> at <span class='time'>" . $time . "</time> by <span class='user'>" . $user . "</span>");
              $commentlink = $dir.$file;
              $picfile = ("../" . $user . "/pictures/default/default.php");
              $picture = file_get_contents($picfile);
              $comment = file_get_contents($commentlink);
              echo "<div class='usercomment'>
   <table cellpadding='0' cellspacing='0' width='100%'>
      <tr>
         <td class='usercommentpic'>
            <a href='../" . $user . "'><img src='" . $picture . "' width='120px' height='120px'></img></a>
         </td>
         <td vAlign='top'>
            <table width='100%' height='100%' class='commentspace' cellpadding='0' cellspacing='0'>
               <tr>
                  <td class='timestamp'>
                     " . $timestamp . "
                  </td>
               </tr>
               <tr>
                  <td class='usercommentcomment' vAlign='top'>
                     " . $comment . "
                  </td>
               </tr>
            </table>
         </td>
      </tr>
  </table>
</div>
<br>";
              $count++;
             }
       }
       closedir($dh);
   }
}

?>

 

It only returns 10 blank comments, there is only 2...and they are not blank. Did I do it wrong?

okay, you're going to have to modify it a bit.  first, scandir accepts the actual path string as the first argument, not the resource handle, so you would use your $dir var not that $dh resource handle. It should look like this:

 

$file = scandir($dir, 1);

 

2nd, scandir returns an array of all of the files, so you don't need to include it in the loop. You will have to loop the $file array that is returned, to display your info though.

$file  = scandir($dir, 1);
while ($count < $topnumber) {
   foreach($file as $value) {
                 if($value !== "." && $value !== "..") {
                    echo "this is " . $value . "<br>";
                    $count++;
                  }
     } 
}

 

That returns this:

 

this is 020107-11-57am - Test.php
this is 020107-11-27am - Test.php
this is 020107-11-57am - Test.php
this is 020107-11-27am - Test.php
this is 020107-11-57am - Test.php
this is 020107-11-27am - Test.php
this is 020107-11-57am - Test.php
this is 020107-11-27am - Test.php
this is 020107-11-57am - Test.php
this is 020107-11-27am - Test.php

 

It's a start?

$count = 0;
while($file[$count]) {
   if($file[$count] !== "." && $file[$count] !== "..") {
      echo "this is " . $file[$count] . "<br>";
      $count++;
   } 
}

 

or

 

foreach($file as $val) {
   if($val !== "." && $val !== "..") {
      echo "this is " . $val . "<br>";
   } 
}

 

I have gotten it working with this code, once I realized that it reversed and echo'd them, just kept doing it, it is because the top number was telling it when to stop echoing, so this fixed it, alot of code, but it works :D

 

$dir = "comments/";
$numberfile = "info/cammount.php";
$topnumber = file_get_contents($numberfile);
$count = 0;
$total = 0;

$gettotal = scandir($dir, 1);
foreach($gettotal as $totalvalue) {
            if ($totalvalue !== "." && $totalvalue !== "..") {
                $total++;
               }
           }
if ($total >= $topnumber) {
$value  = scandir($dir, 1);
while ($count < $topnumber) {
          foreach($value as $file) {
                      if($file !== "." && $file !== ".." && $count < $topnumber) {
                         $date = substr($file, 0, 2) . "/" . substr($file, 2, 2) . "/" . substr($file, 4,2);
                         $time = substr($file, 7, 2) . ":" . substr($file, 10, 4);
                         $user = substr($file, 17, -4);
                         $timestamp = ("Posted on <span class='date'>" . $date . "</span> at <span class='time'>" . $time . "</time> by <span class='user'>" . $user . "</span>");
                         $commentlink = $dir.$file;
                         $picfile = ("../" . $user . "/pictures/default/default.php");
                         $picture = file_get_contents($picfile);
                         $comment = file_get_contents($commentlink);
                         echo "<div class='usercomment'>
   <table cellpadding='0' cellspacing='0' width='100%'>
      <tr>
         <td class='usercommentpic'>
            <a href='../" . $user . "'><img src='" . $picture . "' width='120px' height='120px'></img></a>
         </td>
         <td vAlign='top'>
            <table width='100%' height='100%' class='commentspace' cellpadding='0' cellspacing='0'>
               <tr>
                  <td class='timestamp'>
                     " . $timestamp . "
                  </td>
               </tr>
               <tr>
                  <td class='usercommentcomment' vAlign='top'>
                     " . $comment . "
                  </td>
               </tr>
            </table>
         </td>
      </tr>
  </table>
</div>
<br>";
                         $count++;
                       }
          } 
}
} elseif ($total < $topnumber) {
$value  = scandir($dir, 1);
while ($count < $total) {
          foreach($value as $file) {
                      if($file !== "." && $file !== ".." && $count < $topnumber) {
                         $date = substr($file, 0, 2) . "/" . substr($file, 2, 2) . "/" . substr($file, 4,2);
                         $time = substr($file, 7, 2) . ":" . substr($file, 10, 4);
                         $user = substr($file, 17, -4);
                         $timestamp = ("Posted on <span class='date'>" . $date . "</span> at <span class='time'>" . $time . "</time> by <span class='user'>" . $user . "</span>");
                         $commentlink = $dir.$file;
                         $picfile = ("../" . $user . "/pictures/default/default.php");
                         $picture = file_get_contents($picfile);
                         $comment = file_get_contents($commentlink);
                         echo "<div class='usercomment'>
   <table cellpadding='0' cellspacing='0' width='100%'>
      <tr>
         <td class='usercommentpic'>
            <a href='../" . $user . "'><img src='" . $picture . "' width='120px' height='120px'></img></a>
         </td>
         <td vAlign='top'>
            <table width='100%' height='100%' class='commentspace' cellpadding='0' cellspacing='0'>
               <tr>
                  <td class='timestamp'>
                     " . $timestamp . "
                  </td>
               </tr>
               <tr>
                  <td class='usercommentcomment' vAlign='top'>
                     " . $comment . "
                  </td>
               </tr>
            </table>
         </td>
      </tr>
  </table>
</div>
<br>";
                         $count++;
                       }
          } 
}
}

wait..all you want to do is simply list the most recent 10 right? you don't need all that extra code and conditions and stuff.  The initial scandir gets all of them into the array and orders it by most recent.  If all you wish to do is list the first 10, then simply do a for loop, from 0 to $toplist (it looks like $toplist is your variable that holds 10 so you can change it to something else?)

 

for($x = 0; $x < $toplist; $x++) {
   echo $file[$x];
}

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.