分页思想:1、请求页面传入分页代码PageUtil所需的三个参数(设定的每页记录数,查询记录总数,当前页面ID)
?????????????? 2、PageUtil通过传入的数据计算分页所需的其它数据
?????????????? 3、在数据呈现页面获取PageUtil所设定的基本数据,通过这些数据设定分页连接,通过DBTest操作数据库
以下仅讲述分页功能,数据库工具类不做描述。
<?php class PageUtil { //PageUtil.php public $pageSize;//每页记录数 public $numItems;//记录总数 public $currentPageID;//当前页面ID // public $queryStr;//sql语句 // public $dbUtil; public $numPages;//页面总数 public $nextPageID;//下一页ID public $prevPageID;//上一页ID public $isFistPage;//是否第一页 用以判断$nextPageID和$prevPageID public $isEndPage;//是否第二页 public $startPoint;//mysql记录起始点 public $increment;//mysql数据增量 function PageUtil($pageSize,$numItems,$currentPageID) { $this->pageSize=$pageSize; $this->numItems=$numItems; $this->initPaper($currentPageID); } function initPaper($currentPageID) { $this->numPages=$this->numItems%$this->pageSize>0 ? intval($this->numItems/$this->pageSize)+1:$this->numItems/$this->pageSize; //跳转功能设置 $currentPageID>$this->numPages时默认尾页,$currentPageID<=0时默认首页 if($currentPageID>$this->numPages) $this->currentPageID=$this->numPages; else if($currentPageID<=0) $this->currentPageID=1; else $this->currentPageID=$currentPageID; //此处主要为跳转页设置currentPage<=0时,避免startPoint<0 $this->startPoint=($this->currentPageID<=0) ? 0 : ($this->currentPageID-1)*$this->pageSize; //当记录为空避免increment<0 if($this->numItems<=0) $this->increment=0; else $this->increment=($this->numItems-($this->currentPageID-1)*$this->pageSize)>$this->pageSize ? $this->pageSize : ($this->numItems-($this->currentPageID-1)*$this->pageSize); $this->prevPageID=$this->currentPageID-1; $this->nextPageID=$this->currentPageID+1; } } //echo "开始点".$pageUtil->startPoint."步长".$pageUtil->increment."总页数".$pageUtil->numPages."下一页".$pageUtil->nextPageID."上一页".$pageUtil->prevPageID."当前页".$pageUtil->currentPageID; ?>
?页面链接
?????????????????? ?第 1 页 / 共 4 页??? 共 2 条???? <!-- 首页连接控制 -->首页? <!-- 上一页连接控制 -->上一页? <!-- 下一页连接控制 -->下一页? <!-- 尾页页连接控制 -->尾页??? 页 ? ?第 <span id="pageControl1_lblCurrPage"><?php if($numItems==0)echo 0; else echo $pageUtil->currentPageID?></span> 页 / 共 <span id="pageControl1_lblPageCount"><?php echo $pageUtil->numPages?></span> 页 共 <span id="pageControl1_lblCount"><?php echo $increment ?></span> 条 <!-- 上一页连接控制 --> <a <?php if($pageUtil->prevPageID<=0)echo 'disabled="disabled"'; else echo "href='lesson.php?courseID=$courseID&¤tPageID=$pageUtil->prevPageID' ";?>id="pageControl1_btnPrePage" >上一页</a> <input name="currentPageID" type="text" id="pageControl1_txtGoPage" <?php if($pageUtil->numPages<1 ) echo 'disabled="disabled""';?> value="<?php echo $currentPageID?>" style="width:30px;" />页 <input name="courseID" type="hidden" id="pageControl1_txtGoPage" style="width:30px;" value="<?php echo $courseID?>"/>?