|
HTML clipboard
This tutorial require 1 PHP file and 1 table of mySQL database.
- page.php
- Database "tutorial" and table "name_list" with 2 fields: id(auto_increment),
name(varchar, 50) and put some records about 20 - 30 records into this table.
(directly by phpMyAdmin)
page.php
This page will show the records with page navigator on the top.
Source:
<?
// Connect database
mysql_connect("localhost","","");
mysql_select_db("tutorial");
$table="name_list";
$pagesize=5;
$result=mysql_query("select id from $table;");
$totalrecord=mysql_num_rows($result);
$totalpage=(int)($totalrecord/$pagesize);
if(($totalrecord%$pagesize)!=0){
$totalpage+=1;
}
if(isset($pageid)){
$start=$pagesize*($pageid-1);
}
else{
$pageid=1;
$start=0;
}
$result=mysql_query("select * from $table order by id asc limit $start, $pagesize;");
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?
for ($i=1; $i<=$totalpage; $i++){
if ($i==$pageid){
echo "<b>".$i."</b> | ";
}
else{
echo '<a href=".$PHP_SELF.'?pageid='.$i.'><b>" .$i.'</b></a> | ';
}
}
?>
<table border="1">
<tr>
<td>No.</td>
<td>Name</td>
</tr>
<?
while($row=mysql_fetch_assoc($result)){
?>
<tr>
<td><? echo $row['id']; ?></td>
<td><? echo $row['name']; ?></td>
</tr>
<?
}
mysql_close();
?>
</table>
</body>
</html>
|