<?php
/* You must provide the proper database information here */
$username="<-usernam->";
$password="<-password->";
$database="<-database->";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
// How many rows of results need to be on each page?
$rowsPerPage = 5;
// If you would like to make $rowsPerPage adjustable, include this
if (isset($_GET['rowsPerPage'])) {
$rowsPerPage = $_GET['rowsPerPage'];
}
// by default we show first page
$pageNum = 1;
// if $_GET['page'] defined, use it as page number
if(isset($_GET['page'])) {
$pageNum = $_GET['page'];
}
// Count the offset so each page will know how many rows to skip in order to get to the rows meant for each page
$offset = ($pageNum - 1) * $rowsPerPage;
/* You must change 'table' in the next line to suit your table */
$query = "SELECT * FROM <-tablename-> LIMIT $offset, $rowsPerPage";
$result=mysql_query($query);
$num=mysql_numrows($result);
$i=0;
while ($i < $num) {
// Establish your variable in preparation to print your results
$variable1=mysql_result($result,$i,"variable1");
// Print variables
echo $variable1 . '<BR />';
}
// We have to query the database again to count how many rows we have total
/* You must change 'id' and 'table' in the next line */
$query2 = "SELECT COUNT(id) AS numrows FROM <-tablename->";
$result2 = mysql_query($query2) or die('Error, query2 failed');
$row = mysql_fetch_array($result2, MYSQL_ASSOC);
$numrows = $row['numrows'];
// how many pages we have when using paging?
$maxPage = ceil($numrows/$rowsPerPage);
// Print page number links
$self = $_SERVER['PHP_SELF'];
$nav = '';
for($page = 1; $page <= $maxPage; $page++) {
if ($page == $pageNum) {
$nav .= " $page "; // no need to create a link to current page
} else {
$nav .= " <a href=\"$self?page=$page&rowsPerPage=$rowsPerPage\" class='page'>$page</a> ";
}
}
// creating previous and next link
// plus the link to go straight to
// the first and last page
if ($pageNum > 1) {
$page = $pageNum - 1;
$prev = " <a href=\"$self?page=$page&rowsPerPage=$rowsPerPage\" class='page'><</a> ";
$first = " <a href=\"$self?page=1&rowsPerPage=$rowsPerPage\" class='page'><<</a> ";
} else {
$prev = ' '; // we're on page one, don't print previous link
$first = ' '; // nor the first page link
}
if ($pageNum < $maxPage) {
$page = $pageNum + 1;
$next = " <a href=\"$self?page=$page&rowsPerPage=$rowsPerPage\" class='page'>></a> ";
$last = " <a href=\"$self?page=$maxPage&rowsPerPage=$rowsPerPage\" class='page'>>></a> ";
} else {
$next = ' '; // we're on the last page, don't print next link
$last = ' '; // nor the last page link
}
// print the navigation link
echo $first . $prev . $nav . $next . $last;
// and close the database connection
mysql_close();
?>