Simple php pagination

What is Pagination?

Think about if you have a mysql table with a thousand rows, and you want to allow the user to browse through the entire table. Displaying all the records in that table in one page would not be a good idea. Instead you should break the table up into smaller parts and let the user navigate through it. This is what pagination is, it allows you to break up large results from a database query, and displays a better navigation for the users.

The following code is a quick and dirty example of php/mysql pagination but if you are familiar with CSS, you can easily style the way the page navigation looks.
<?php 
if(!empty($_GET["start"])){
    
$start $_GET['start'];// To take care global variable if OFF
}else{
    
$start  0;
}
if(!(
$start 0)) { // This variable is set to zero for the first page
    
$start 0;
}

$eu = ($start 0);
$limit           5// No of records to be shown per page.
$whathis      $eu $limit;
$back          $eu $limit;
$next          $eu $limit;

// to check the total number of records
$query         mysql_query(" SELECT * FROM <tablename> ") or die (mysql_error());
$total_rows     mysql_num_rows($query);

//select the record with limitation
$query         mysql_query(" SELECT * FROM <tablename> limit $eu, $limit ") or die (mysql_error());

//code for previous
if($back >=0) {
echo 
"<a href='yourpage.php?start=$back'><font face='Verdana' size='2'>PREV</font></a>&nbsp;&nbsp;";
}

//code for the number of page with links
$i     0;
$x    1;
for(
$i=0;$i $total_rows;$i=$i+$limit){
if(
$i != $eu){
    echo 
"<a href='yourpage.php?start=$i'><font face='Verdana' size='2'>$x</font></a> ";
}else { 
    echo 
"<font face='Verdana' size='4' color=red>$x</font>";
// Current page is not displayed as link and given font color red

$x    $x+1;
}
//code for next
if($whathis $total_rows) {
echo 
"<a href='yourpage.php?start=$next'><font face='Verdana' size='2'>NEXT</font></a>";
}    
?>

0 comments: