title: PHP - moving forwards and backwards through mysql rows
body: <p>I have a page that displays a row depending on the id given in the browser bar (page.php?id=1). I am trying to use forward and back buttons to display the corresponding next or previous row on the page. So essentially the prev and next buttons will just be links to page.php, with the id being the next in the list. It isn't a sequential list, so I can't just do id+1 etc. </p>
  
  <p>Anyway, I have it working fine, but basically I want the forward and back links to only display as text if there isn't a page to go to. I've been trying to run code to display only text if the $backformat is empty, but it seems to hold "Resource id#(and a random number)". So I've tried some other ways to match "Resource" in the string but that hasn't worked</p>
  
  <p>Any ideas?</p>
  
  <pre><code>  &lt;?php
  
          $backresult = mysql_query("SELECT id FROM studies WHERE niche = '$niche' AND date &lt; '$currentdate' ORDER BY date DESC LIMIT 1", $connection);
              if (!$backresult) {
                  die("Database query failed: " . mysql_error());
              }
  
              $forwardresult = mysql_query("SELECT id FROM studies WHERE niche = '$niche' AND date &gt; '$currentdate' ORDER BY date ASC LIMIT 1", $connection);
              if (!$forwardresult) {
                  die("Database query failed: " . mysql_error());
              }
  
      ?&gt;
  </code></pre>
  
  <p>
  
  <pre><code>while ($row = mysql_fetch_array($forwardresult)) {
                  $forward = $row["id"];
                  $forwardformat = preg_replace('/\s+/','',$forward);
  
                      echo $forwardformat;
  
                      $pos = strpos($forwardformat,'source');
  
                      if($pos == false) {
                       // string needle NOT found in haystack
                       echo 'Exploring moves us &lt;a href="casestudy.php?id=';
                      echo $forwardformat;
                      echo '"&gt;forward&lt;/a&gt;';
                      }
                      else {
                       // string needle found in haystack
                       echo "forward";
                      }
                                  //other code I've tried before
                  /* if (!empty($forwardformat)) {
                      echo 'Exploring moves us &lt;a href="casestudy.php?id=';
                      echo $forwardformat;
                      echo '"&gt;forward&lt;/a&gt;';
                  }
                  else {
                      echo "forward";
                  }*/
  }
  echo $backresult;
  while ($row = mysql_fetch_array($backresult)) {
                  $back = $row["id"];
                  $backformat = preg_replace('/\s+/','',$back);
  
                      echo $backformat;
  
                      //$pos = strpos($backformat,'source');
  
                      //$match = preg_match("R",$backformat);
  
                      if (substr($backformat,0,1) == 'R') {
                       // string needle NOT found in haystack
                      echo ', studying we look &lt;a href="casestudy.php?id=';
                      echo $backformat;
                      echo '"&gt;back&lt;/a&gt;';
                      }
                      else {
                       // string needle found in haystack
                       echo "back";
                      }
                                  //other code I've tried before          
                  /*if (!empty($backformat)) {
                      echo ', studying we look &lt;a href="casestudy.php?id=';
                      echo $backformat;
                      echo '"&gt;back&lt;/a&gt;';
                  }
                  if ($backformat==false) {
                      echo "back";
                  } */
  }
  </code></pre>