title: My code not working properly for pagination in PHP, any suggestion?
body: <p>I am beginner in PHP . 
  I want to display my mysql database contents from table to main front page display as paging (with limit) of number of items from table of database. My code display a format with exact paging and it plot items in first page (as: <a href="http://www.swaminarayantravel.com/category.php?sc=Trekking" rel="nofollow">http://www.swaminarayantravel.com/category.php?sc=Trekking</a>), But the problem is , when I try to go to page 2 from paging below, it take to the false page.
  My code is: </p>
  
  <pre><code>&lt;?php
  
  //get the function
  include_once ('function.php');
  include_once ('includes/connect.php');
  
      $page = (int) (!isset($_GET["page"]) ? 1 : $_GET["page"]);
      $limit = 4;
      $startpoint = ($page * $limit) - $limit;
  
      //to make pagination
    $sql = "category where category.category='$selcat'";
    ?&gt;
  &lt;div class="records round"&gt;
  
  
    &lt;ul class="homelist"&gt;
  &lt;?php
  
          //show records
   $query = mysql_query("SELECT * FROM {$sql} LIMIT {$startpoint} , {$limit}");
  echo "&lt;table&gt;
  &lt;tr&gt;&lt;/tr&gt;";
  
          while ($row = mysql_fetch_assoc($query)) {   ?&gt;
  &lt;?php echo' &lt;li &gt; &lt;img src="images/subcat/'.$row['file'] .'" height="100" width="100" alt="'.$row['subcategory'] .'"/&gt; 
   &lt;a class="title"&gt;'; 
  $subcategory= $row["subcategory"];
                          echo "$subcategory"; 
  echo'&lt;/a&gt;
        &lt;p align="justify"&gt;'; 
  $detail= $row["subcat_detail"];
                          echo "$detail"; 
                          echo ' &lt;/p&gt; '; ?&gt; 
   &lt;?php   }   ?&gt; 
  &lt;/table&gt;&lt;/div&gt;
  &lt;?php   echo pagination($sql,$limit,$page);  ?&gt;
  </code></pre>
  
  <p>and function.php page contains following codes: </p>
  
  <pre><code>&lt;?php
  </code></pre>
  
  <p>function pagination($query, $per_page = 10,$page = 1, $url ='?'){<br>
          $query = "SELECT COUNT(*) as <code>num</code> FROM {$query}";
          $row = mysql_fetch_array(mysql_query($query));
          $total = $row['num'];
          $adjacents = "2"; </p>
  
  <pre><code>    $page = ($page == 0 ? 1 : $page);  
      $start = ($page - 1) * $per_page;                               
  
      $prev = $page - 1;                          
      $next = $page + 1;
      $lastpage = ceil($total/$per_page);
      $lpm1 = $lastpage - 1;
  
      $pagination = "";
      if($lastpage &gt; 1)
      {   
          $pagination .= "&lt;ul class='pagination'&gt;";
                  $pagination .= "&lt;li class='details'&gt;Page $page of $lastpage&lt;/li&gt;";
          if ($lastpage &lt; 7 + ($adjacents * 2))
          {   
              for ($counter = 1; $counter &lt;= $lastpage; $counter++)
              {
                  if ($counter == $page)
                      $pagination.= "&lt;li&gt;&lt;a class='current'&gt;$counter&lt;/a&gt;&lt;/li&gt;";
                  else
                      $pagination.= "&lt;li&gt;&lt;a href='{$url}page=$counter'&gt;$counter&lt;/a&gt;&lt;/li&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;li&gt;&lt;a class='current'&gt;$counter&lt;/a&gt;&lt;/li&gt;";
                      else
                          $pagination.= "&lt;li&gt;&lt;a href='{$url}page=$counter'&gt;$counter&lt;/a&gt;&lt;/li&gt;";                    
                  }
                  $pagination.= "&lt;li class='dot'&gt;...&lt;/li&gt;";
                  $pagination.= "&lt;li&gt;&lt;a href='{$url}page=$lpm1'&gt;$lpm1&lt;/a&gt;&lt;/li&gt;";
                  $pagination.= "&lt;li&gt;&lt;a href='{$url}page=$lastpage'&gt;$lastpage&lt;/a&gt;&lt;/li&gt;";      
              }
              elseif($lastpage - ($adjacents * 2) &gt; $page &amp;&amp; $page &gt; ($adjacents * 2))
              {
                  $pagination.= "&lt;li&gt;&lt;a href='{$url}page=1'&gt;1&lt;/a&gt;&lt;/li&gt;";
                  $pagination.= "&lt;li&gt;&lt;a href='{$url}page=2'&gt;2&lt;/a&gt;&lt;/li&gt;";
                  $pagination.= "&lt;li class='dot'&gt;...&lt;/li&gt;";
                  for ($counter = $page - $adjacents; $counter &lt;= $page + $adjacents; $counter++)
                  {
                      if ($counter == $page)
                          $pagination.= "&lt;li&gt;&lt;a class='current'&gt;$counter&lt;/a&gt;&lt;/li&gt;";
                      else
                          $pagination.= "&lt;li&gt;&lt;a href='{$url}page=$counter'&gt;$counter&lt;/a&gt;&lt;/li&gt;";                    
                  }
                  $pagination.= "&lt;li class='dot'&gt;..&lt;/li&gt;";
                  $pagination.= "&lt;li&gt;&lt;a href='{$url}page=$lpm1'&gt;$lpm1&lt;/a&gt;&lt;/li&gt;";
                  $pagination.= "&lt;li&gt;&lt;a href='{$url}page=$lastpage'&gt;$lastpage&lt;/a&gt;&lt;/li&gt;";      
              }
              else
              {
                  $pagination.= "&lt;li&gt;&lt;a href='{$url}page=1'&gt;1&lt;/a&gt;&lt;/li&gt;";
                  $pagination.= "&lt;li&gt;&lt;a href='{$url}page=2'&gt;2&lt;/a&gt;&lt;/li&gt;";
                  $pagination.= "&lt;li class='dot'&gt;..&lt;/li&gt;";
                  for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter &lt;= $lastpage; $counter++)
                  {
                      if ($counter == $page)
                          $pagination.= "&lt;li&gt;&lt;a class='current'&gt;$counter&lt;/a&gt;&lt;/li&gt;";
                      else
                          $pagination.= "&lt;li&gt;&lt;a href='{$url}page=$counter'&gt;$counter&lt;/a&gt;&lt;/li&gt;";                    
                  }
              }
          }
  
          if ($page &lt; $counter - 1){ 
              $pagination.= "&lt;li&gt;&lt;a href='{$url}page=$next'&gt;Next&lt;/a&gt;&lt;/li&gt;";
              $pagination.= "&lt;li&gt;&lt;a href='{$url}page=$lastpage'&gt;Last&lt;/a&gt;&lt;/li&gt;";
          }else{
              $pagination.= "&lt;li&gt;&lt;a class='current'&gt;Next&lt;/a&gt;&lt;/li&gt;";
              $pagination.= "&lt;li&gt;&lt;a class='current'&gt;Last&lt;/a&gt;&lt;/li&gt;";
          }
          $pagination.= "&lt;/ul&gt;\n";        
      }
  
  
      return $pagination;
  } 
  </code></pre>
  
  <p>?></p>