phpPagination Source code of phppagination/example3.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<head><title>phpPagination: Example 3: Print paged data from MySQL</title></head>
<html><body>
<?php
require_once('phppagination.class.php');
// NB!
// replace host, user, password, database and table to your values
$sMySqlHost = 'localhost';
$sMySqlUser = 'dbuser';
$sMySqlPassword = 'passwd';
$sDatabaseName = 'test';
$sTableName = 'my_table';
$nTotalItems = 0;
$nItemsPerPage = 5; // set length of page
// get page number passed via GET method
if (isset($_GET['page']))
$nCurrentPage = $_GET['page'];
else
$nCurrentPage = 1;
// connect to mysql
$link = mysql_connect($sMySqlHost, $sMySqlUser, $sMySqlPassword)
or die("Could not connect to '$sMySqlHost'");
// connect to database
mysql_select_db($sDatabaseName)
or die("Could not select database '$sDatabaseName'");
// get total items
$sSQL = "SELECT count(*) FROM `$sTableName`";
$result = mysql_query($sSQL)
or die ("Invalid query '$sSQL'");
$row=mysql_fetch_row($result);
$nTotalItems=$row[0];
// create pagination object
$oPagination = new phpPagination ($nTotalItems, $nItemsPerPage);
// print pagination for current page
echo $oPagination->GetHtml($nCurrentPage)."\n";
// get and print items for current page
$sSQL = "SELECT * FROM `$sTableName` LIMIT "
.($nCurrentPage-1)*$nItemsPerPage
.", $nItemsPerPage";
$result = mysql_query($sSQL)
or die ("Invalid query '$sSQL'");
echo "<table border=\"1\">\n";
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '<tr>';
foreach ($row as $col_value) {
echo "<td>$col_value</td>";
}
echo "</tr>\n";
}
echo "</table>\n";
// free resultset
mysql_free_result($result);
// closing connection
mysql_close($link);
?>
</body></html>
back