Tue
21
Mar '06
This is not a full-featured code, but you can develop it to one.
Simple Paging feature with PHP and MySQL
Simple Paging feature with PHP and MySQL
< _?php
$db = mysql_connect(“localhost”,“root”);
mysql_select_db(“test”);
$pagesize = 2;
$res = mysql_query(“select *from test”);
$totalrecs = mysql_num_rows($res);
if (!isset($firstrec)) $firstrec = 0;
if (isset($_REQUEST[‘action’])) {
$firstrec = $_REQUEST[‘firstrec’];
switch ($_REQUEST[‘action’]) {
case “prev”:
if ($firstrec > 0)
$firstrec-= $pagesize ;
break;
case “next”:
if ($firstrec < $totalrecs-$pagesize )
$firstrec+=$pagesize;
break;
}
}
$res = mysql_query(“select *from test limit “ . $firstrec . “, “ . $pagesize);
while ($row = mysql_fetch_assoc($res)) {
echo “<br>” . $row[“id”] . ” “ . $row[“name”];
}
?>
<br /><br />
<table width=“475″ border=“1″>
<tr>
<td width=“60″><a href=“/index.php?firstrec=< ?=$firstrec ?>&action=prev”>Prev</a></td>
<td width=“327″ align=“center”>
< _?php
$res = mysql_query(“select *from test”);
for ($i=0;$i< (mysql_num_rows($res)/$pagesize);$i++) {
if ($firstrec== $i*$pagesize)
echo “<b>” . $i . ” | “ ;
else
echo “<a href=’”.$_SERVER[‘PHP_SELF’].“?firstrec=” . ($i*$pagesize) .“&action=number’>$i</a> | “ ;
}
?>
</td>
<td width=“66″><a href=“/index.php?firstrec=< ?=$firstrec ?>&action=next”>Next</a></td>
</tr>
</table>

Leave a passing comment »