Here is a simple example of ten records per page. Pass a page number in the URL. Let's say that you are going to get the first set of records. Then you'd pass
http://www.mydomain.com/pagescript?pageid=1
Then pagescript looks like:
<?php
// clean up input
$pageid = substr($pageid, 0, 3); // or however big page max is
$pageid = EscapeShellCmd($pageid);
// build query - using 10 recs per page - but we read 11 rows to
// have a way of knowing if we are at end of file so we don't put
// out the 'next page' link
$skiprows = (($pageid - 1) * 10); // number of rows to skip in query
$query = "SELECT * FROM $table
LIMIT $skiprows, 11";
// connect to the database
if(!($dbh=mysql_connect ("localhost",
"$user",
"$pass")))
die("Cannot connect to the database");
// open the connected database
if (!mysql_select_db("$dbname",$dbh))
die("Cannot open the database");
// execute the query
if (!($result = @ mysql_query ($query, $dbh)))
die("Cannot execute query against the database");
// if we got some rows back then get some numbers and output page
$numrows = mysql_num_rows($result);
if ($numrows > 0)
{
for ($i = 1; $i <= 10; $i++)
{
// last page may have less than 10 rows
if ($row = @ mysql_fetch_array($result))
{
// do all the stuff to put out the row
} // if there are rows in array
} // for i
// now put out next and previous links
// if not on page 1, then do previous
if ($pageid > 1)
{
$oldpageid = $pageid - 1;
echo "<a href=\"pagescript.php?pageid=$oldpageid\">";
echo "Go Back to Page $oldpageid</a>";
}
// if rowcount less than 11, must be last page otherwise do link
if $(numrows > 10) // there will be a next page
{
$newpageid = $pageid + 1;
echo "<a href=\"pagescript.php?pageid=$newpageid\">";
echo "Go Forward to Page $newpageid</a>";
}
} // if $numrows > 0
?>
You can also get a little fancier and fix up the script so that you don't get a last page with one or two orphaned entries... you make the current page 1 or 2 entries longer.