下面是一個簡單的PHP連接MySQL數據庫進行數據分頁顯示的模版.可以按注釋說明信息進行修改,裡面的sql語句是可以自己改的.
注意分析和觀察裡面相關分頁部分的代碼的書寫和實現的方式.
<?PHP
$link = mysql_connect('localhost', 'root', '') or dIE('MySQL database connect error');
MySQL_select_db('your database') or dIE('the selected database is not exist');
?>
//這裡插入你的Html代碼,
<?PHP
$sql = 'select count(*) count from your_table';
$result = mysql_query($sql) or dIE(mysql_errno().": ".MySQL_error()."n");
$rs=MySQL_fetch_object($result);
$recountCount = $rs->count;
$show = 20;
$totalPage = ceil($recountCount/$show);
$page = (isset($_GET['page']) && $_GET['page']>=0)? $_GET['page']: 0;
$isLast = ($page==($totalPage-1))? true: false;
$hasNoPre = ($page==0)? true: false;
$hasNoNext = ($page==$totalPage-1)? true: false;
$isFirst = ($page==0)? true:false;
$start = $page*$show;
MySQL_free_result($result);
?>
//這裡插入你的Html代碼,
<?
$sql = "select * from your_table limit $start,$show";
$result = mysql_query($sql) or dIE(mysql_errno().": ".MySQL_error()."n");
while($rs=MySQL_fetch_object($result)){
//這個循環裡的Html代碼自己更具實際情況修改
echo $rs->art_id;
echo "<br>";
}
MySQL_free_result($result);
?>
<?
$str = "共 $recountCount 條記錄,當前第 ".($page+1)."/$totalPage 頁";
$str .= $isFirst? "首頁" : "<a href="?page=0">首頁</a>";
$str .= $hasNoPre? "上一頁" : "<a href="?page=".($page-1)."">上一頁</a>";
$str .= $hasNoNext? "下一頁" : "<a href="?page=".($page+1)."">下一頁</a>";
$str .= $isLast? "尾頁" : "<a href="?page=".($totalPage-1)."">尾頁</a>";
echo $str;
?>