ryanmetzler3 Posted December 21, 2013 Share Posted December 21, 2013 (edited) I got this section where people post comments. Here is a while loop that pulls all the comments and passes them to the function getComments. Then I will also show the function "getComments" that displays them. //while loop to get comments <?php $q = "SELECT * FROM threaded_comments WHERE parent_id = 0 ORDER BY id DESC"; $r = mysql_query($q); while($row = mysql_fetch_assoc($r)): getComments($row); endwhile; ?> // function to display them <?php function getComments($row) { echo "<li class='comment'>"; echo "<div class='aut'>".$row['author']."</div>"; echo "<div class='comment-body'>".$row['comment']."</div>"; echo "<div class='timestamp'>".$row['created_at']."</div>"; echo "<a href='#comment_form' class='reply' id='".$row['id']."'>Reply</a>"; $q = "SELECT * FROM threaded_comments WHERE parent_id = ".$row['id'].""; $r = mysql_query($q); if(mysql_num_rows($r)>0) { echo "<ul>"; while($row = mysql_fetch_assoc($r)) { getComments($row); } echo "</ul>"; } echo "</li>"; } ?> The second if-statement in the function to retrieve replies to comments. How could I go about doing pagination so maybe 15 original comments on a page display (I am not worried about replies)? Then have the option to page over and view more. I was thinking of using the LIMIT attribute in the first query, but then I would not know the total number of comments. Sorry for such a dumb question, but I am new to programming Edited December 21, 2013 by ryanmetzler3 Quote Link to comment https://forums.phpfreaks.com/topic/284885-pagination-for-comments-section/ Share on other sites More sharing options...
jcbones Posted December 21, 2013 Share Posted December 21, 2013 There is a pagination tutorial on this site that is quite good. http://forums.phpfreaks.com/page/tutorials/_/basic-pagination?pg=1 That should get you where you need to go, if you hit a roadblock, post back and we can help you resolve it. Quote Link to comment https://forums.phpfreaks.com/topic/284885-pagination-for-comments-section/#findComment-1462903 Share on other sites More sharing options...
Solution ryanmetzler3 Posted December 21, 2013 Author Solution Share Posted December 21, 2013 There is a pagination tutorial on this site that is quite good. http://forums.phpfreaks.com/page/tutorials/_/basic-pagination?pg=1 That should get you where you need to go, if you hit a roadblock, post back and we can help you resolve it. Thanks for the link. I am basically following it, but I am stuck on one part. You can see in my original code my SQL query takes the comments out of the DB and passes them to a function. Like so: $q = "SELECT * FROM threaded_comments WHERE parent_id = 0 ORDER BY id DESC"; $r = mysql_query($q); while($row = mysql_fetch_assoc($r)): getComments($row); endwhile; In the pagination tutorial they do something like this: It looks like they put the data in an array and echo it from there. I really just want to pass it to my ajax function because I need the comments to update automatically. I got kinda stuck on how to do that. Any idea how to tweak this tutorial to make it work like my original query? // while there are rows to be fetched... while ($list = mysql_fetch_assoc($result)) { // echo data echo $list['id'] . " : " . $list['number'] . "<br />"; } // end while Quote Link to comment https://forums.phpfreaks.com/topic/284885-pagination-for-comments-section/#findComment-1462913 Share on other sites More sharing options...
ryanmetzler3 Posted December 21, 2013 Author Share Posted December 21, 2013 Nevermind, I solved my issues. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/284885-pagination-for-comments-section/#findComment-1462916 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.