Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is wrong with my pagination?
    primarykey
    data
    text
    <p>I am working on a site on which news is split in to different pages, I already have some PHP code for this but this doesn't seem to work correctly. The news on my site is split correctly, but when there are only enough messages for one page, the "next" button still pops up and you can scroll through empty pages. I am relatively new to PHP so I couldn't quite come up with a solution for this, hopefully someone here can help. </p> <p>Here is the code:</p> <pre><code>else { $conn = mysql_connect($host, $user, $pass); if(! $conn ) { die('Could not connect: ' . mysql_error()); } mysql_select_db('nieuws'); $rec_limit = 3; $sql = "SELECT count(id) FROM updates "; $retval = mysql_query( $sql, $conn ); if(! $retval ) { die('Could not get data: ' . mysql_error()); } $row = mysql_fetch_array($retval, MYSQL_NUM ); $rec_count = $row[0]; if( isset($_GET{'page'} ) ) { $page = $_GET{'page'} + 1; $offset = $rec_limit * $page ; } else { $page = 0; $offset = 0; } $left_rec = $rec_count - ($page * $rec_limit); $sql = "SELECT id, titel, inleiding, bericht, datum ". "FROM updates ". "LIMIT $offset, $rec_limit"; $retval = mysql_query( $sql, $conn ); if(! $retval ) { die('Could not get data: ' . mysql_error()); } echo "&lt;hr&gt;"; while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) { $intro = substr($row['bericht'], 0, 330); echo '&lt;b style="font-size:18px;font-family:Arial,Helvetica,sans-serif;color:#2B2B2B;"&gt;'.htmlspecialchars(stripslashes($row['titel'])).'&lt;/b&gt; &lt;br&gt; '. '&lt;i style="font-size:13px;"&gt;'.htmlspecialchars(stripslashes($row['datum'])).'&lt;/i&gt;&lt;br&gt;&lt;br&gt; '. "{$row['inleiding']}". '... &lt;a href="'.$_SERVER['PHP_SELF'].'?id='.$row['id'].'"&gt;Lees verder&lt;/a&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;'; echo '&lt;hr&gt;'; } if( $page &gt; 0 ) { $last = $page - 2; echo "&lt;a style=\"margin-left:0px;\"href=\"{$_SERVER['PHP_SELF']}?page=$last\"&gt;Vorige&lt;/a&gt;"; echo "&lt;a style=\"margin-left:645px;\" href=\"{$_SERVER['PHP_SELF']}?page=$page\"&gt;Volgende&lt;/a&gt;"; } else if( $page == 0 ) { echo "&lt;a style=\"margin-left:685px;\" href=\"{$_SERVER['PHP_SELF']}?page=$page\"&gt;Volgende&lt;/a&gt;"; } else if( $left_rec &lt; $rec_limit ) { $last = $page - 2; echo "&lt;a style=\"margin-left:0px;\" href=\"{$_SERVER['PHP_SELF']}?page=$last\"&gt;Vorige&lt;/a&gt;"; } echo '&lt;br&gt;&lt;br&gt;&lt;br&gt;'; </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    1. This table or related slice is empty.
    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