title: Pagination not working for second page
body: <p>This page loads perfectly with the query results in a table. The pagination is in place and has correctly divided the pages per number of records. There are other items on the page that have nothing to do with the table. When I try to go to the second page, I only get the header row with no other records and the other information shows up on the page as undefined index, almost like there was no query in the first place. I have searched the web for answers and I have exhausted what limited knowledge I have of fixing this problem. All I would like it to do is to paginate the query results from mysql database and refresh the table only everytime the user changes pages. I have looked at ajax and jquery to do this but cannot get a handle on what and how to implement it in my code. I can understand about the page refreshing problem but I would have assumed the pagination would work anyways. Below is the code that I have if anyone can point out any errors or suggest how best to make this work I would greatly appreciate it.</p>
  
  <p>
  
  <pre><code>$brandname = $_GET['brandname'];
  $picked = $_GET['picked'];
  $pickcheck = $_GET['pickcheck'];
  
  
  $brands =($brandname);
  $_SESSION['$brandname']= $brandname;
  $pick =($picked);
  $_SESSION['$picked']= $pick;
  $picker =($pickcheck);
  $_SESSION['$pickcheck']=$picker;
  
  $tbl_name="pickme";
  $adjacents = 3;
  
  $query = "SELECT COUNT(*) as num FROM tirestock";
  $total_pages = mysql_fetch_array(mysql_query($query));
  $total_pages = $total_pages["num"];
  
  $targetpage = "connecttest.php";    //your file name  (the name of this file)
  $limit = 5;                                 //how many items to show per page
  $page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
  $page=mysql_real_escape_string($page);
  $page = $_GET['page'];
  if($page)
      $start = ($page - 1) * $limit;          
  else
      $start = 0;
  
  $sql = "SELECT * FROM tirestock LIMIT $start, $limit";
  $result = mysql_query($sql);
  
  if ($page == 0) $page = 1;
  $prev = $page - 1;
  $next = $page + 1;
  $lastpage = ceil($total_pages/$limit);
  $lpm1 = $lastpage - 1;
  
  $pagination = "";
  if($lastpage &gt; 1)
  {
      $pagination .= "&lt;div class=\"pagination\"&gt;";
  
      if ($page &gt; 1)
          $pagination.= "&lt;a href=\"$targetpage?page=$prev\"&gt;« previous&lt;/a&gt;";
      else
          $pagination.= "&lt;span class=\"disabled\"&gt;« previous&lt;/span&gt;";
  
      //pages
      if ($lastpage &lt; 7 + ($adjacents * 2))
      {
          for ($counter = 1; $counter &lt;= $lastpage; $counter++)
          {
              if ($counter == $page)
                  $pagination.= "&lt;span class=\"current\"&gt;$counter&lt;/span&gt;";
              else
                  $pagination.= "&lt;a href=\"$targetpage?page=$counter\"&gt;$counter&lt;/a&gt;";
          }
      }
      elseif($lastpage &gt; 5 + ($adjacents * 2))
      {
  
          if($page &lt; 1 + ($adjacents * 2))
          {
              for ($counter = 1; $counter &lt; 4 + ($adjacents * 2); $counter++)
              {
                  if ($counter == $page)
                      $pagination.= "&lt;span class=\"current\"&gt;$counter&lt;/span&gt;";
                  else
                      $pagination.= "&lt;a href=\"$targetpage?page=$counter\"&gt;$counter&lt;/a&gt;";
              }
              $pagination.= "...";
              $pagination.= "&lt;a href=\"$targetpage?page=$lpm1\"&gt;$lpm1&lt;/a&gt;";
              $pagination.= "&lt;a href=\"$targetpage?page=$lastpage\"&gt;$lastpage&lt;/a&gt;";
          }
  
          elseif($lastpage - ($adjacents * 2) &gt; $page &amp;&amp; $page &gt; ($adjacents * 2))
          {
              $pagination.= "&lt;a href=\"$targetpage?page=1\"&gt;1&lt;/a&gt;";
              $pagination.= "&lt;a href=\"$targetpage?page=2\"&gt;2&lt;/a&gt;";
              $pagination.= "...";
              for ($counter = $page - $adjacents; $counter &lt;= $page + $adjacents; $counter++)
              {
                  if ($counter == $page)
                      $pagination.= "&lt;span class=\"current\"&gt;$counter&lt;/span&gt;";
                  else
                      $pagination.= "&lt;a href=\"$targetpage?page=$counter\"&gt;$counter&lt;/a&gt;";
              }
              $pagination.= "...";
              $pagination.= "&lt;a href=\"$targetpage?page=$lpm1\"&gt;$lpm1&lt;/a&gt;";
              $pagination.= "&lt;a href=\"$targetpage?page=$lastpage\"&gt;$lastpage&lt;/a&gt;";
          }
  
          else
          {
              $pagination.= "&lt;a href=\"$targetpage?page=1\"&gt;1&lt;/a&gt;";
              $pagination.= "&lt;a href=\"$targetpage?page=2\"&gt;2&lt;/a&gt;";
              $pagination.= "...";
              for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter &lt;= $lastpage; $counter++)
              {
                  if ($counter == $page)
                      $pagination.= "&lt;span class=\"current\"&gt;$counter&lt;/span&gt;";
                  else
                      $pagination.= "&lt;a href=\"$targetpage?page=$counter\"&gt;$counter&lt;/a&gt;";
              }
          }
      }
  
  
      if ($page &lt; $counter - 1)
          $pagination.= "&lt;a href=\"$targetpage?page=$next\"&gt;next »&lt;/a&gt;";
      else
          $pagination.= "&lt;span class=\"disabled\"&gt;next »&lt;/span&gt;";
      $pagination.= "&lt;/div&gt;\n";
  }
  </code></pre>