Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The easiest way would be to include an integer <code>$_GET</code> variable that is the row number + 1.</p> <pre><code>&lt;a href="index.php?id=&lt;?php echo ($id + 1) ?&gt;" /&gt; </code></pre> <p>Then for your PHP script, you need to get the value, or use a default value (<code>0</code>) if one is not provided.</p> <pre><code>&lt;?php if(!isset($_GET['id'])) $id = 0; else $id = $_GET['id']; ?&gt; </code></pre> <p>Don't forget to bound your integer, so you can't get invalid results. In your case, make sure that <code>$id</code> does not go below <code>0</code> or above <code>9</code>. Something simple like</p> <pre><code> if($id &lt; 0) $id = 0; if($id &gt; 9) $id = 9; </code></pre> <p>should work. This goes immediately after the previous code sample.</p> <p>Then for your query, simply do</p> <pre><code>$sql = "select * from mytable where city = 'atlanta' LIMIT {$id}, 1"; </code></pre> <p>Notice the <code>LIMIT</code> clause here that takes two parameters. The first, your current <code>$id</code> number tell the query where to start looking in the result set. The second parameter tells it how many results from the start it should take. In this case, we always want one, so we'll leave it as <code>1</code>.</p> <p>Of course, you should parametrize this query, but since it sounds like you're just learning SQL, that's not a huge deal. If this was a public site, you would need to though.</p> <p>You could also use <code>mysql_num_rows</code> or the PDO equivalent to get the total number of applicable rows, then determine whether <code>$id + 1</code> is greater than the total number of rows (meaning you were on the last record), and if so, not display the link to the next record.</p> <p>Complete code</p> <pre><code>&lt;?php if(!isset($_GET['id'])) $id = 0; else $id = $_GET['id']; if($id &lt; 0) $id = 0; if($id &gt; 9) $id = 9; $sql = "select * from mytable where city = 'atlanta' LIMIT {$id}, 1"; $result = mysql_fetch_assoc($sql); ?&gt; Display your result here &lt;?php if( ( $id + 1 ) &lt; mysql_num_rows($result) ) : &lt;a href="index.php?id=&lt;?php echo ($id + 1) ?&gt;" /&gt; &lt;?php endif; ?&gt; </code></pre>
 

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