Skip to content

Instantly share code, notes, and snippets.

@kshoufer
Last active August 29, 2015 14:07
Show Gist options
  • Save kshoufer/7c2748adc0d4133574d5 to your computer and use it in GitHub Desktop.
Save kshoufer/7c2748adc0d4133574d5 to your computer and use it in GitHub Desktop.
Simple PHP paginator
<html>
<head>
<title>Paging Using PHP</title>
</head>
<body>
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$rec_limit = 5;
$PHP_SELF = $_SERVER['PHP_SELF'];
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('classicmodels');
/* Get total number of records */
$sql = "SELECT count(employeeNumber) FROM employees ";
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
$row = mysql_fetch_array($retval);
$rec_count = $row[0];
if( isset($_GET['page'] ) )
{
$page = $_GET['page'] + 1;
$offset = $rec_limit * $page ;
}
else
{
$page = 0;
$offset = 0;
}
$left_rec = $rec_count - ($page * $rec_limit);
$sql = "SELECT employeeNumber, lastName, email ".
"FROM employees ".
"LIMIT $offset, $rec_limit";
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval))
{
echo "EMP ID :{$row['employeeNumber']} <br> ".
"EMP NAME : {$row['lastName']} <br> ".
"EMP SALARY : {$row['email']} <br> ".
"--------------------------------<br>";
}
if( $page > 0 && $left_rec >= $rec_limit )
{
$last = $page - 2;
echo "<a href=\"$PHP_SELF?page=$last\">Prev</a> | ";
echo "<a href=\"$PHP_SELF?page=$page\">Next</a>";
}
else if( $page == 0 )
{
echo "<a href=\"$PHP_SELF?page=$page\">Next</a>";
}
else if( $left_rec < $rec_limit )
{
$last = $page - 2;
echo "<a href=\"$PHP_SELF?page=$last\">Prev</a>";
}
mysql_close($conn);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment