title: PHP Pagination code not working
body: <p>I am unable to do pagination. The code display all the data retrieve from the mysql. Please help me to get it right.....</p>
  
  <p><strong>function.php</strong></p>
  
  <pre><code>function pagination($limit=5) //here limit is not taken by the code
  {
  
  //$limit=5; if I uncomment this line limit will work
  global $admin;
  
  $sql = "SELECT FOUND_ROWS();";
  $result = mysql_query($sql) or die(mysql_error());
  $row = mysql_fetch_array($result);
  $numrows = $row[0];
  $pagelinks = "&lt;div class=pagelinks&gt;";
  if ($numrows &gt; $limit) {
  if(isset($_GET['page'])){
    $page = $_GET['page'];
  } else {
    $page = 1;
  }
  $currpage = $_SERVER['PHP_SELF'] . "?" . $_SERVER['QUERY_STRING'];
  $currpage = str_replace("&amp;page=".$page,"",$currpage);
  
  if($page == 1){
    $pagelinks .= "&lt;span class='pageprevdead'&gt;&amp;lt; PREV&lt;/span&gt;";
  }else{
    $pageprev = $page - 1;
    $pagelinks .= "&lt;a class='pageprevlink' href='" . $currpage .
      "&amp;page=" . $pageprev . "'&gt;&amp;lt; PREV&lt;/a&gt;";
  }
  
  $numofpages = ceil($numrows / $limit);
  $range = $admin['pageRange']['value'];
  if ($range == "" or $range == 0) $range = 7;
  $lrange = max(1,$page-(($range-1)/2));
  $rrange = min($numofpages,$page+(($range-1)/2));
  if (($rrange - $lrange) &lt; ($range - 1)) {
    if ($lrange == 1) {
      $rrange = min($lrange + ($range-1), $numofpages);
    } else {
      $lrange = max($rrange - ($range-1), 0);
    }
  }
  
  if ($lrange &gt; 1) {
    $pagelinks .= "..";
  } else {
    $pagelinks .= "&amp;nbsp;&amp;nbsp;";
  }
  for($i = 1; $i &lt;= $numofpages; $i++){
    if($i == $page){
      $pagelinks .= "&lt;span class='pagenumdead'&gt;$i&lt;/span&gt;";
    }else{
      if ($lrange &lt;= $i and $i &lt;= $rrange) {
        $pagelinks .= "&lt;a class='pagenumlink' href='" . $currpage .
        "&amp;page=" . $i . "'&gt;" . $i . "&lt;/a&gt;";
      }
    }
  }
  if ($rrange &lt; $numofpages) {
    $pagelinks .= "..";
  } else {
    $pagelinks .= "&amp;nbsp;&amp;nbsp;";
  }
  
  if(($numrows - ($limit * $page)) &gt; 0){
    $pagenext = $page + 1;
    $pagelinks .= "&lt;a class='pagenextlink' href='" . $currpage .
      "&amp;page=" . $pagenext . "'&gt;NEXT &amp;gt;&lt;/a&gt;";
  } else {
    $pagelinks .= "&lt;span class='pagenextdead'&gt;NEXT &amp;gt;&lt;/span&gt;";
  }
  } else {
  $pagelinks .= "&lt;span class='pageprevdead'&gt;
    &amp;lt; PREV&lt;/span&gt;&amp;nbsp;&amp;nbsp;";
  $pagelinks .= "&lt;span class='pagenextdead'&gt;
    NEXT &amp;gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp;";
  }
  $pagelinks .= "&lt;/div&gt;";
  return $pagelinks;
  }'
  </code></pre>
  
  <p><strong>index.php</strong></p>
  
  <pre><code>$sql = "SELECT id FROM articles WHERE is_published=1 " .
  "ORDER BY date_published DESC"; //this is the query to retrive data from mysql
  
  $result = mysql_query($sql,$conn);
  
  if (mysql_num_rows($result) == 0) {
  echo "  &lt;br /&gt;\n";
  echo "  There are currently no articles to view.\n";
  } else {
  while ($row = mysql_fetch_array($result)) {
  outputStory($row['article_id'],TRUE);
  }
  }
  
  echo pagination($limit);`
  </code></pre>