dachshund Posted August 28, 2009 Share Posted August 28, 2009 Hi, I have this code, but it only displays the single most recent comment. I want it to display the whole array of comments in order of id. Can anyone help? Here's my code: <?php if($_SESSION['uid']){ $sessionname=$_SESSION['uid']; $articleid=$rows['id']; $commentsql="SELECT * FROM comments WHERE articleid ='$id' ORDER BY id DESC LIMIT 10"; $commentresult=mysql_query($commentsql) or die (mysql_error()); $commentrows=mysql_fetch_array($commentresult); $query="SELECT * FROM users WHERE id = '$sessionname'"; $userresult=mysql_query($query); $userrow = mysql_fetch_assoc($userresult); echo "<ul>"; echo "<li>Leave a comment</li>"; echo "<li>"; echo "<form name=\"comment\" method=\"post\" action=\""; include $_SERVER['DOCUMENT_ROOT']; echo "/test/commentposted.php?id="; echo $articleid; echo "\">"; echo "<textarea name=\"comment\" rows=\"5\"></textarea>"; echo "</li>"; echo "<li>"; echo "<input type=\"submit\" name=\"submitcomment\" value=\"Comment\">"; echo "</form>"; echo "</li>"; echo "<li>"; echo $userrow['username']; echo "</li>"; echo "<li>"; echo $commentrows['comment']; echo "</li>"; echo "</ul>"; }else { echo "<ul><li>You must be logged in to post a comment.</li>"; echo "<li>"; echo $userrow['username']; echo "</li>"; echo "<li>"; echo $commentrows['comment']; echo "</li>"; echo "</ul>"; } mysql_close(); } ?> Link to comment https://forums.phpfreaks.com/topic/172280-solved-display-all-comments-not-just-one/ Share on other sites More sharing options...
lynxus Posted August 28, 2009 Share Posted August 28, 2009 I think you will need to put the ECHO code in a while loop. So basicallky it will echo each returned row form teh database, It looks like it will only do the last row in the db at the moment as the code isnt looping. Link to comment https://forums.phpfreaks.com/topic/172280-solved-display-all-comments-not-just-one/#findComment-908343 Share on other sites More sharing options...
dachshund Posted August 28, 2009 Author Share Posted August 28, 2009 like this? <?php $commentsql="SELECT * FROM comments WHERE articleid ='$id' ORDER BY id DESC LIMIT 10"; $commentresult=mysql_query($commentsql) or die (mysql_error()); $commentrows=mysql_fetch_array($commentresult); $query="SELECT * FROM users WHERE id = '$sessionname'"; $userresult=mysql_query($query); $userrow = mysql_fetch_assoc($userresult); while($commentrows=mysql_fetch_array($commentresult)){ ?> <li> <? echo $userrow['username']; ?> </li> <li> <? echo $commentrows['comment']; ?> </li> </ul> <?php } mysql_close() ?> that still only returns a single result. Link to comment https://forums.phpfreaks.com/topic/172280-solved-display-all-comments-not-just-one/#findComment-908350 Share on other sites More sharing options...
lynxus Posted August 28, 2009 Share Posted August 28, 2009 yeah it would be "like" that however my WHILES are rusty at best. Maybe another coder here could see the issue? Link to comment https://forums.phpfreaks.com/topic/172280-solved-display-all-comments-not-just-one/#findComment-908351 Share on other sites More sharing options...
dachshund Posted August 28, 2009 Author Share Posted August 28, 2009 ah i fixed it, just a minor thing. thanks for your help Link to comment https://forums.phpfreaks.com/topic/172280-solved-display-all-comments-not-just-one/#findComment-908364 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.