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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

$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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

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.