Twentyoneth Posted February 1, 2007 Share Posted February 1, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/36676-making-comment-listing/ Share on other sites More sharing options...
.josh Posted February 1, 2007 Share Posted February 1, 2007 You can first read everything and put it into a multi-dim array and reverse the order and then loop through it, with your tables. edit: i guess i should actually look at your code.. yeah I think doing the multi-dim array thing is your best bet Quote Link to comment https://forums.phpfreaks.com/topic/36676-making-comment-listing/#findComment-174863 Share on other sites More sharing options...
Twentyoneth Posted February 1, 2007 Author Share Posted February 1, 2007 I don't quite understand.... Quote Link to comment https://forums.phpfreaks.com/topic/36676-making-comment-listing/#findComment-174864 Share on other sites More sharing options...
.josh Posted February 1, 2007 Share Posted February 1, 2007 okay sorry, my brain was somewhere else. Actually, if your filename looks like this: 020107-11-57am - Test.php you can use scandir() to sort it in descending order, because the more recent the date/time, the higher the number string will be. Quote Link to comment https://forums.phpfreaks.com/topic/36676-making-comment-listing/#findComment-174871 Share on other sites More sharing options...
Twentyoneth Posted February 1, 2007 Author Share Posted February 1, 2007 How would I implement that into my current code? Quote Link to comment https://forums.phpfreaks.com/topic/36676-making-comment-listing/#findComment-174887 Share on other sites More sharing options...
.josh Posted February 1, 2007 Share Posted February 1, 2007 The only thing you should have to change is use scandir instead of readdir and set the sort order argument to 1 (did you look at the link to the manual I sent you?) Quote Link to comment https://forums.phpfreaks.com/topic/36676-making-comment-listing/#findComment-174899 Share on other sites More sharing options...
Twentyoneth Posted February 1, 2007 Author Share Posted February 1, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/36676-making-comment-listing/#findComment-174900 Share on other sites More sharing options...
.josh Posted February 1, 2007 Share Posted February 1, 2007 well it's pretty much the same as readdir. mostly. just give it a try. Quote Link to comment https://forums.phpfreaks.com/topic/36676-making-comment-listing/#findComment-174904 Share on other sites More sharing options...
Twentyoneth Posted February 1, 2007 Author Share Posted February 1, 2007 Sorry, you were posting at the same time I was editing, but my current code and what not is up a post. Quote Link to comment https://forums.phpfreaks.com/topic/36676-making-comment-listing/#findComment-174905 Share on other sites More sharing options...
.josh Posted February 1, 2007 Share Posted February 1, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/36676-making-comment-listing/#findComment-174917 Share on other sites More sharing options...
Twentyoneth Posted February 1, 2007 Author Share Posted February 1, 2007 $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? Quote Link to comment https://forums.phpfreaks.com/topic/36676-making-comment-listing/#findComment-174927 Share on other sites More sharing options...
.josh Posted February 1, 2007 Share Posted February 1, 2007 $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>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/36676-making-comment-listing/#findComment-174935 Share on other sites More sharing options...
Twentyoneth Posted February 1, 2007 Author Share Posted February 1, 2007 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 $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++; } } } } Quote Link to comment https://forums.phpfreaks.com/topic/36676-making-comment-listing/#findComment-174945 Share on other sites More sharing options...
.josh Posted February 1, 2007 Share Posted February 1, 2007 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]; } Quote Link to comment https://forums.phpfreaks.com/topic/36676-making-comment-listing/#findComment-174952 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.