php code for Page Navigator
Posted On at by milan
// Connect database
mysql_connect("localhost","","");
mysql_select_db("tutorial");
$table="name_list"; // Set database table name.
$pagesize=5; // Set number of records to view on this page.
// Get only one column(id) from table.
$result=mysql_query("select id from $table;");
// Count the total of records by mysql_num_rows() function and set it to $totalrecord.
$totalrecord=mysql_num_rows($result);
// How many pages will be. Set it to $totalpage.
$totalpage=(int)($totalrecord/$pagesize);
if(($totalrecord%$pagesize)!=0){
$totalpage+=1;
}
/* If this page get $pageid variable, set $pageid and starting record as $start.
If not, $pageid set to 1 and $start set at 0 (first record in table) */
if(isset($pageid)){
$start=$pagesize*($pageid-1);
}
else{
$pageid=1;
$start=0;
}
// Select records from table with limit statement and put them to $result.
$result=mysql_query("select * from $table order by id asc limit $start, $pagesize;");
?>
// This is the Page Navigator.
for ($i=1; $i<=$totalpage; $i++){
if ($i==$pageid){
echo "".$i." | ";
}
else{
echo '' .$i.' | ';
}
}
?>
| No. | Name |
PHP Free Code