Note that there are some explanatory texts on larger screens.

plurals
  1. POMYSQL select comments, limit 3, 'click to see all' and display on same page. PHP
    primarykey
    data
    text
    <p>I have some code which retrieves user comments from my database:</p> <pre><code>$comments = mysql_query("SELECT * FROM comments WHERE ref = '$theID'LIMIT 0, 3;") or die(mysql_error()); while ($rowC = mysql_fetch_array($comments)) { echo "&lt;p&gt;On " .$rowC['date']. ", "; echo $rowC['username']. " said: &lt;br/&gt;"; echo $rowC['comment']; echo "&lt;/p&gt;&lt;hr/&gt;"; } if (mysql_num_rows($comments) == 0) { echo "&lt;p&gt;(No comments have been made yet)&lt;/p&gt;"; } </code></pre> <p>Comments are stored with a unique user reference in the database, and retrieved where they match the user id, this is called at the top of the page:</p> <pre><code>$theID = $_GET['id']; </code></pre> <p>What I am trying to do is limit the comments shown, and if there are more than 3, show a 'click to see more' type button or link which displays all the user comments on the same page in the same way as above. </p> <p>UPDATE, i am now using this, Trying to implement Johan's suggestion, but still cant get it to display more when link is clicked: </p> <pre><code> $comments = mysql_query("SELECT * FROM comments WHERE ref = '$theID' LIMIT 0, 4") or die(mysql_error()); while ($rowC = mysql_fetch_array($comments)) { echo "&lt;p&gt;On " .$rowC['date']. ", "; $username = htmlspecialchars($rowC['username']). " said: &lt;br/&gt;"; echo $username; $comment = htmlspecialchars($rowC['comment']); echo $comment; echo "&lt;/p&gt;&lt;hr/&gt;"; } $num_rows = mysql_num_rows($result); if ($num_rows &gt; 3) { $query = "SELECT * FROM comments WHERE ref = '$theID' LIMIT 4, 20"; } echo "&lt;p&gt;&lt;a href=''&gt;click to see more&lt;/a&gt;&lt;/p&gt;"; if (mysql_num_rows($comments) == 0) { echo "&lt;p&gt;(No comments have been made yet)&lt;/p&gt;"; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload