php分頁(yè)代碼簡(jiǎn)單實(shí)現(xiàn)
1、首先獲取數(shù)據(jù)的總條數(shù);
2、然后在用總條數(shù)除以每頁(yè)的條數(shù),得出的到總頁(yè)數(shù);
//模擬總條數(shù) $total = 84; //每頁(yè)的數(shù)量 $count = 10; //計(jì)算頁(yè)數(shù) $page = $total / $count; echo $page;
輸出結(jié)果:8.4
3、再將總頁(yè)數(shù)使用“ceil()”函數(shù)轉(zhuǎn)為整數(shù),“ceil()”函數(shù)意思就是對(duì)小數(shù)向上取整;
<?php //模擬總條數(shù) $total = 84; //每頁(yè)的數(shù)量 $count = 10; //計(jì)算頁(yè)數(shù) $page = $total / $count; //向上取整 $page = ceil($page); echo $page;
輸出結(jié)果:9
4、最后根據(jù)總頁(yè)數(shù)渲染翻頁(yè)鏈接。
// 翻頁(yè)鏈接 for ($i = 0; $i < $page; $i ++) { echo "<a href=index.php?page=" . ($i + 1) . ">" . ($i + 1) . "</a>"; }