title: Previous or Next 10 links for search results page
body: <p>I am trying to create a PDO database results page with a previous and next for the previous 10 or next 10 records. I just can't figure out what should go in the 4 ????? areas below. I have tried several different approaches with all sorts of incorrect results. Any ideas on the missing pieces to make this work?</p>
  
  <pre><code>$results = $dbh-&gt;prepare("select 
  stories.SID,
  stories.story_name,
  stories.category,
  stories.genre
  FROM stories
  WHERE stories.category = :cat OR stories.genre = :gen LIMIT 10");
  $results-&gt;bindParam(':cat', $scategory, PDO::PARAM_STR);
  $results-&gt;bindParam(':gen', $sgenre, PDO::PARAM_STR);
  $results-&gt;execute();
  $row = $results-&gt;fetchAll(PDO::FETCH_ASSOC);
  }
  
  
  if ($row) {
  echo '&lt;table&gt;';
  echo '&lt;tr&gt;';
  foreach ($row as $all) {
  echo '&lt;tr&gt;';
  echo "&lt;td&gt;&lt;a href=\"http://example.com/?w=$all[SID]\"&gt;$all[story_name]&lt;/a&gt;
  &lt;/td&gt;";
  echo "&lt;td&gt;$all[category]&lt;/td&gt;";
  echo "&lt;td&gt;$all[genre]&lt;/td&gt;";
  }
  echo '&lt;/table&gt;';
  }
  
  
  //get all results from PDO SQL query and pass to previous &amp; next links
  $id = $row;
  
  $stmt_a = $pdo-&gt;prepare("
  (SELECT * FROM stories WHERE ???? ORDER BY id DESC LIMIT 1)
  UNION (SELECT * FROM stories WHERE id = (SELECT MAX(id) FROM stories)) LIMIT
  1 prev_id");
  $stmt_b = $pdo-&gt;prepare("
  (SELECT * FROM stories WHERE ????? ORDER BY id ASC LIMIT 1)
   UNION (SELECT * FROM stories WHERE id = (SELECT MIN(id) FROM stories)) LIMIT
   1 next_id");
  
  // $vars = array(':id' =&gt; $id);
  $prev = $stmt_a-&gt;execute();
  $next = $stmt_b-&gt;execute();
  
  if ($prev) {
  while($row = $stmt_a-&gt;fetchObject()) {
  echo '&lt;a href="' ??????? '"&gt;Previous&lt;/a&gt;';
  } 
  } else {
  echo 'no previous';
  }
  
  if ($next) {
  while($row = $stmt_b-&gt;fetchObject()) {
  echo '&lt;a href="' ??????? '"&gt;Next&lt;/a&gt;';
  }
  } else {
  echo 'no next';
  }
  </code></pre>