title: Next and Previous MySQL row based on name
body: <p>I have a table with details on personnel. I would like to create a Next/Previous link based on the individual's last name. Since personnel were not added in alphabetical order, selecting the next or previous row based on its ID does not work.</p>
  
  <p>It is a hefty table - the pertinent fields are id, name_l, and name_f. I would like to order by name_l, the individuals' last name.</p>
  
  <p>How would I go about accomplishing this task?</p>
  
  <p>Thanks!</p>
  
  <p><strong>Edit</strong>
  This will be used on a Personnel Details page, the result will generate links to the next/prev entry in the database (ordered by last name) based on the current row. For example, if I am viewing Joe Hammer, the Next link would link to Frank Ingram.</p>
  
  <p><strong>Final code</strong></p>
  
  <p>Thanks to Daniel, here is what I finally got to work:</p>
  
  <p>First, I set an increment at 0: <strong>$i = 0</strong>. Then, while looping through records with a <strong>while</strong> loop, I increased this by 1 = <strong>$i++</strong>. I then made a link to the details page for that particular entry:</p>
  
  <pre><code>&lt;a href="details.php?id=&lt;?php echo $member['id'];?&gt;&amp;amp;row=&lt;?php echo $i;?&gt;"&gt;Details&lt;/a&gt;
  </code></pre>
  
  <p>On the Details page, I used the following SQL to select the next record:</p>
  
  <pre><code>$row = $_GET['row'];
  $getNext = mysql_query("SELECT * FROM members ORDER BY name_l, id LIMIT ".$row.", 1");
  $next = mysql_fetch_assoc($getNext);
  $nextLink = $row + 1;
  </code></pre>
  
  <p>Finally, the link:</p>
  
  <pre><code>&lt;a href="member_details.php?id=&lt;?php echo $next['id'];?&gt;&amp;amp;row=&lt;?php echo $nextLink;?&gt;"&gt;&lt;?php echo $next['name_l'] . ", " . $next['name_f'];?&gt;&lt;/a&gt;
  </code></pre>