drifterz28
12/17/2012 - 3:54 PM

Simple PHP pagination

// paging code
// Query to count rows.
$result = mysql_query("SELECT * FROM Table_Name WHERE Column_Name = '$section'");
$items = 32; // number of items per page.
$all = $_GET['a']; //This is the view all param.
   
$num_rows = mysql_num_rows($result);
if($all == "all"){
  $items = $num_rows;
}
$nrpage_amount = $num_rows/$items;
$page_amount = ceil($num_rows/$items);
$page_amount = $page_amount-1;
$page = mysql_real_escape_string($_GET['p']);
if($page < "1"){
  $page = "0";
}
$p_num = $items*$page;
//end paging code
// Query that you would like to SHOW
$result = mysql_query("SELECT * FROM Table_Name WHERE Column_Name = '$section' ORDER BY 'name' ASC LIMIT $p_num , $items");
     
function paging(){
  global $num_rows;
  global $page;
  global $page_amount;
  global $section;
  if($page_amount != "0"){
    echo "<div class=paging>";
    if($page != "0"){
      $prev = $page-1;
      echo "<a href=\"section.php?q=$section&p=$prev\">Prev</a>";
    }
    for ( $counter = 0; $counter <= $page_amount; $counter += 1) {
      echo "<a href=\"section.php?q=$section&p=$counter\">";
      echo $counter+1;
      echo "</a>";
    }
    if($page < $page_amount){
      $next = $page+1;
      echo "<a href=\"section.php?q=$section&p=$next\">Next</a>";
    }
    echo "<a href=\"section.php?q=$section&a=all\">View All</a>";
    echo "</div>";
  }
}
// call on Pagination with function
paging();