//if their is no results then return message saying so
if (mysql_num_rows($rs) > 0)
{
//read the get vars, to find current page
if (!empty($_GET['page']) && is_numeric($_GET['page'])) $mypage = $_GET['page']; else $mypage =1;
//read the get vars, to get results per-page
if (empty($_GET['perpage']) || !is_numeric($_GET['perpage'])) $perpage=10; else $perpage = $_GET['perpage'];
//store the number of results
$numres = mysql_num_rows($rs);
//divide number of results by number per page
$numpages = $numres / $perpage;
//this is the bit i need help with to improve
$bit = explode(".",$numpages);
// it needs to ignore everything after the decimal place (thus always round down)
$numpages = $bit[0];
//if it wasn't dead on a page then add an extra page for the remaining results
if ($numres % $perpage != 0) $numpages++;
//if they gave us a invalid page act as page 1
if ($mypage > $numpages) $mypage =1;
//stop is the last record number
//calculate = current page * per page
$stop = $mypage * $perpage;
//if its the last page then the end is the last record
if ($mypage == $numpages) $stop = $numres;
//if the page is 1 then record 0 is the start
switch ($mypage)
{
case 1:
$start = 0;
break;
case 2:
//the start is current page + (per page -2)
$start = ($mypage + ($perpage -2));
break;
default:
//start is (current page * per page) - per page
$start = ($mypage * $perpage)-$perpage;
break;
}
echo "Showing records $start to $stop <br>\n";
//previous page is page -1
$prev = $mypage - 1;
//if previous page = 0 then its the current page
if ($prev == 0 ) $prev = 1;
//if its the last page then next = current else next = current + 1
if ($mypage == $numpages) $next = $numpages; else $next = $mypage + 1;
//store the perpage value
$qstring = "&perpage=".$perpage;
$numcol = sizeof($showfields);
//create the table structure
?>
<table border="0" cellpadding="0" cellspacing="0" style="border:thin solid #CDADFF;" width="92%">
<?php
echo "\t<tr>";
for ($c=0;$c<$numcol;$c++)
//loop to create the header
{
echo "\n\t\t<td bgcolor=\"#DDCDFF\"><strong>$showfields[$c]</strong></td>";
}
echo "\n\t</tr>\n";
//loop through and display results
for ($e = $start;$e < $stop;$e++)
{
//fetch a row from the rs
$row = mysql_fetch_array($rs,MYSQL_ASSOC);
if ($e % 2 == 0) $bg = "#DFDFFF"; else $bg = "#FCFCFF";
echo "\r\t<tr bgcolor=\"$bg\">";
for ($c=0;$c<$numcol;$c++)
//loop to create the rows data
{
echo "\n\t\t<td style=\"font-size:small;\"> ".$row[$showfields[$c]]."</td>";
}
echo "\r\t</tr>";
}
echo "\n\t<tr>\n\t\t<td colspan=\"".($numcol-1)."\" bgcolor=\"#CDADFF\" style=\"font-size:small;\">";
?>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="get">
<form name="form1">Show
<select name="perpage">
<option value="10">10</option>
<option value="25">25</option>
<option value="50">50</option>
<option value="100">100</option>
</select> Results per page
<input type="Submit" name="Mode" value="Go">
</form>
?>
</table><?php
}
else
{
echo "The Supplied Recordset is empty\n <br>\n";
}
}
//test by giving a rs
$user = "root";
$pass = "**********";
$host = "localhost";
//connect to mysql or show error
$mydb = mysql_connect($host,$user,$pass) or die("Could not connect to database server<br><br>\n<h2>Technical Information</h2>".mysql_error());
//select the database
mysql_select_db("website",$mydb) or die(mysql_error());
$rs = mysql_query("select * from articles");
$fd = array("articlename","articledesc","articledate");
page_recordset($rs,$fd)
?>