一、分頁類設計Page.php

創(chuàng)新互聯(lián)堅持“要么做到,要么別承諾”的工作理念,服務領域包括:成都網(wǎng)站設計、成都網(wǎng)站建設、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務,滿足客戶于互聯(lián)網(wǎng)時代的河北網(wǎng)站設計、移動媒體設計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡建設合作伙伴!
<?php
class Page {
public $pageSize=6;//每頁顯示記錄數(shù)
public $rowCount;//記錄總數(shù)數(shù)
public $pageNow=1; //當前頁
public $pageCount=1; //總頁數(shù)
public $navigate; //導航
public $url; //地址
public $firstRow; //每頁顯示的第一條記錄
public $rollPage; //分欄每頁顯示頁數(shù)
public $startNum; //【1】
public $endNum;
public function setNav(){
$navigate="";
//本導航的起始頁
if($this->startNum>1){
$jump=$this->startNum-$this->rollPage;
//$nav=$this->url."/pageNow/$jump";
$navigate.= "<a href='{$this->url}?pageNow=$jump'><<</a> ";
}
for($start=$this->startNum;$start<=$this->endNum;$start++)
{
$navigate.="<a href='{$this->url}?pageNow=$start'>[{$start}]</a>";
}
if($this->endNum<$this->pageCount)
{
$jump=$this->startNum+$this->rollPage;
$navigate.= "<a href='{$this->url}?pageNow=$jump'>>></a> ";
}
$navigate.="第".$this->pageNow."頁/共".$this->pageCount."頁";
$this->navigate=$navigate;
}
public function setPage($url,$pageNow,$rollPage=4,$pageSize=6,){
$this->url=$url;
$this->pageNow=$pageNow;
$this->pageSize=$pageSize;
$this->rollPage=$rollPage;
}
public function setNavigate(){
$this->pageCount=ceil($this->rowCount/($this->pageSize+0.0));
$this->firstRow=($this->pageNow-1)*$this->pageSize;
$this->startNum=floor(($this->pageNow-1)/$this->rollPage)*$this->rollPage+1;
$this->endNum=$this->startNum+$this->rollPage-1;
if($this->endNum>$this->pageCount)
{
$this->endNum=$this->pageCount;
}
if($this->rowCount==0){
$this->pageNow=1;
$this->rollPage=1;
$this->firstRow=1;
$this->pageCount=1;
}
$this->setNav(); //字符串存儲導航
}
}
?>
二、SqlHelper.class.php定義分頁顯示方法
public function excute_dql_page($sql1,$sql2,&$page){
//數(shù)據(jù)表信息分頁
//sql1數(shù)據(jù),sql2求行數(shù)
$result=MySQL_query($sql1,$this->conn)or die(mysql_errno());
$arr=array();
while($row=mysql_fetch_assoc($result)){
$arr[]=$row;
}
mysql_free_result($result);
$result=mysql_query($sql2,$this->conn) or die(mysql_errno());
if($row=mysql_fetch_row($result)){
$page->rowCount=$row[0];
//實現(xiàn)導航條
$page->setNavigate();
}
mysql_free_result($result);
//數(shù)組存儲記錄集
return $arr;
}
三、UserService.class.php調(diào)用分頁
<?php
require_once 'SQLHelper.class.php';
require_once 'User.class.php';
class UserService{
public function getUserListByPage(&$page){
$sqlHelper=new SQLHelper();
$startNum=($page->pageNow-1)*$page->pageSize;
$sql1="select id,name from users limit $startNum,$page->pageSize";
$sql2="select count(*) from users";
$res=$sqlHelper->excute_dql_page($sql1, $sql2, $page);
$sqlHelper->close_connect();
}
}
四、使用分頁(userList.php)
<?php
header("content-type:text/html;charset=utf-8");
require_once 'SQLHelper.class.php';
require_once 'UserService.class.php';
echo"用戶信息表<br/>";
echo"<table border=1 width=600px>
<tr><th>id</th><th>name</th><th>刪除</th><th>修改</th></tr>";
//創(chuàng)建page并設置屬性值
$page=new Page();
$page->url="userList.php";
$page->pageNow=1;
if(isset($_GET['pageNow'])){
$page->pageNow=$_GET['pageNow'];
}
//創(chuàng)建Service負責產(chǎn)生一個記錄集(分頁后的)
$page->setPage($url,$pageNow);
$userService=new UserService();
$userService->getUserListByPage($fenyePage);
foreach ($page->res_array as $row){
$id=$row['id'];
echo"<tr><td>{$row['id']}</td><td>{$row['name']}</td><td><a href='userProcess.php?action=delete&id=$id'>刪除</a></td><td><a href='updateUserUI.php?id=$id'>修改</a></td></tr>";
//print_r($row['id']);
}
echo"</table>";
echo $fenyePage->navigate;
?>
<form action="userList.php" method="get">
到第 <input type="text" size="1" name="pageNow" />頁<input type="submit" value="確定"/>
</form>
新聞名稱:PHP分頁設計
標題URL:http://www.chinadenli.net/article34/gpdese.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、做網(wǎng)站、虛擬主機、微信公眾號、用戶體驗、云服務器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)