求一个类似这样的分页代码 首页 上一页 1 2 3 4 5 6 7 8 9 10 下一页 末页 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 哈,敲了幾十行程式碼,還封裝在一個函數裏,也才幾十分...程式員,真的很不好混啊.../*** @desc 分頁計算顯示,呼叫該函數的頁面應以GET方式接收page的變數值* @param $num 記錄總數目* @param $perpage 每頁顯示的記錄數* @param $curpage 當前顯示的頁數* @param $mpurl 調用該函數的頁面URL* @param $shownum 顯示出的頁目數量* @param $offset 當前頁目的左偏移量* @return 分頁排列*/function showpages($num,$perpage,$curpage,$mpurl,$shownum=5,$offset = 2){ $showpage = ''; if($num <= $perpage){ $showpage = "<strong>1</strong>"; }else{ //處理鏈接 $mpurl .= strpos($mpurl,'?')?'&':'?'; $totalpage = ceil($num/$perpage); //計算顯示頁數 if($shownum > $totalpage){ $from = 1; $to = $totalpage; }else{ $from = $curpage - $offset; $to = $from + $shownum - 1; if($from < 1){ $to = $curpage + 1 - $from; $from = 1; if($to - $from < $shownum) { $to = $shownum; } } elseif($to > $totalpage) { $from = $totalpage - $shownum + 1; $to = $totalpage; } } //處理顯示 $pagearr = array(); if($curpage - $offset > 1 && $shownum < $totalpage){ $pagearr[]= "<a href=\"{$mpurl}page=1\">首頁</a>"; } if($curpage > 1){ $pagearr[] = "<a href=\"{$mpurl}page=".($curpage-1)."\">上一頁</a>"; } for($i = $from; $i <= $to; $i++){ if($curpage == $i){ $pagearr[] = "<strong>$i</strong>"; }else{ $pagearr[] = "<a href=\"{$mpurl}page=$i\">$i</a>"; } } if($curpage < $totalpage){ $pagearr[] = "<a href=\"{$mpurl}page=".($curpage+1)."\">下一頁</a>"; } if($to < $totalpage){ $pagearr[]= "<a href=\"{$mpurl}page=$totalpage\">末頁</a>"; } $showpage = implode(' ',$pagearr); } return $showpage;} class Page extends Think { // 起始行数 public $firstRow ; // 列表每页显示行数 public $listRows ; // 页数跳转时要带的参数 public $parameter ; // 分页总页面数 protected $totalPages ; // 总行数 protected $totalRows ; // 当前页数 protected $nowPage ; // 分页的栏的总页数 protected $coolPages ; // 分页栏每页显示的页数 protected $rollPage ; // 分页显示定制 protected $config = array('header'=>'条记录','prev'=>'上一页','next'=>'下一页','first'=>'第一页','last'=>'最后一页','theme'=>' %totalRow% %header% %nowPage%/%totalPage% 页 %upPage% %downPage% %first% %prePage% %linkPage% %nextPage% %end%'); /** +---------------------------------------------------------- * 架构函数 +---------------------------------------------------------- * @access public +---------------------------------------------------------- * @param array $totalRows 总的记录数 * @param array $listRows 每页显示记录数 * @param array $parameter 分页跳转的参数 +---------------------------------------------------------- */ public function __construct($totalRows,$listRows,$parameter='') { $this->totalRows = $totalRows; $this->parameter = $parameter; $this->rollPage = C('PAGE_ROLLPAGE'); $this->listRows = !empty($listRows)?$listRows:C('PAGE_LISTROWS'); $this->totalPages = ceil($this->totalRows/$this->listRows); //总页数 $this->coolPages = ceil($this->totalPages/$this->rollPage); $this->nowPage = !empty($_GET[C('VAR_PAGE')])?$_GET[C('VAR_PAGE')]:1; if(!empty($this->totalPages) && $this->nowPage>$this->totalPages) { $this->nowPage = $this->totalPages; } $this->firstRow = $this->listRows*($this->nowPage-1); } public function setConfig($name,$value) { if(isset($this->config[$name])) { $this->config[$name] = $value; } } /** +---------------------------------------------------------- * 分页显示输出 +---------------------------------------------------------- * @access public +---------------------------------------------------------- */ public function show() { if(0 == $this->totalRows) return ''; $p = C('VAR_PAGE'); $nowCoolPage = ceil($this->nowPage/$this->rollPage); $url = $_SERVER['REQUEST_URI'].(strpos($_SERVER['REQUEST_URI'],'?')?'':"?").$this->parameter; $parse = parse_url($url); if(isset($parse['query'])) { parse_str($parse['query'],$params); unset($params[$p]); $url = $parse['path'].'?'.http_build_query($params); } //上下翻页字符串 $upRow = $this->nowPage-1; $downRow = $this->nowPage+1; if ($upRow>0){ $upPage="<a href='".$url."&".$p."=$upRow'>".$this->config['prev']."</a>"; }else{ $upPage=""; } if ($downRow <= $this->totalPages){ $downPage="<a href='".$url."&".$p."=$downRow'>".$this->config['next']."</a>"; }else{ $downPage=""; } // << < > >> if($nowCoolPage == 1){ $theFirst = ""; $prePage = ""; }else{ $preRow = $this->nowPage-$this->rollPage; $prePage = "<a href='".$url."&".$p."=$preRow' >上".$this->rollPage."页</a>"; $theFirst = "<a href='".$url."&".$p."=1' >".$this->config['first']."</a>"; } if($nowCoolPage == $this->coolPages){ $nextPage = ""; $theEnd=""; }else{ $nextRow = $this->nowPage+$this->rollPage; $theEndRow = $this->totalPages; $nextPage = "<a href='".$url."&".$p."=$nextRow' >下".$this->rollPage."页</a>"; $theEnd = "<a href='".$url."&".$p."=$theEndRow' >".$this->config['last']."</a>"; } // 1 2 3 4 5 $linkPage = ""; for($i=1;$i<=$this->rollPage;$i++){ $page=($nowCoolPage-1)*$this->rollPage+$i; if($page!=$this->nowPage){ if($page<=$this->totalPages){ $linkPage .= " <a href='".$url."&".$p."=$page'> ".$page." </a>"; }else{ break; } }else{ if($this->totalPages != 1){ $linkPage .= " <span class='current'>".$page."</span>"; } } } $pageStr = str_replace( array('%header%','%nowPage%','%totalRow%','%totalPage%','%upPage%','%downPage%','%first%','%prePage%','%linkPage%','%nextPage%','%end%'), array($this->config['header'],$this->nowPage,$this->totalRows,$this->totalPages,$upPage,$downPage,$theFirst,$prePage,$linkPage,$nextPage,$theEnd),$this->config['theme']); return $pageStr; }}$count为总数,$listrows为每一页的显示条目。 $p = new Page($count,$listRows); //分页类做好了。 $page = $p->show(); //模板输出 $this->assign('page',$page);一个框架里面的分页类.用法也就这么简单.自己看吧 page.Class.php<?php/** *-------------------------翻页类----------------------* */class PageClass{ private $myde_count; //总记录数 var $myde_size; //每页记录数 private $myde_page; //当前页 private $myde_page_count; //总页数 private $page_url; //页面url private $page_i; //起始页 private $page_ub; //结束页 var $page_limit; function __construct($myde_count=0, $myde_size=1, $myde_page=1,$page_url)//构造函数 { $this -> myde_count = $this -> numeric($myde_count); $this -> myde_size = 3; //$this -> numeric($myde_size); $this -> myde_page = $this -> numeric($myde_page); $this -> page_limit = ($this -> myde_page * $this -> myde_size) - $this -> myde_size; $this -> page_url = $page_url; if($this -> myde_page < 1) $this -> myde_page =1; if($this -> myde_count < 0) $this -> myde_page =0; $this -> myde_page_count = ceil($this -> myde_count/$this -> myde_size); if($this -> myde_page_count < 1) $this -> myde_page_count = 1; if($this -> myde_page > $this -> myde_page_count) $this -> myde_page = $this -> myde_page_count; $this -> page_i = $this -> myde_page - 2; $this -> page_ub = $this -> myde_page + 2; if($this -> page_i < 1){ $this -> page_ub = $this -> page_ub + (1 - $this -> page_i); $this -> page_i = 1; } if($this -> page_ub > $this -> myde_page_count){ $this -> page_i = $this -> page_i - ($this -> page_ub - $this -> myde_page_count); $this -> page_ub = $this -> myde_page_count; if($this -> page_i < 1) $this -> page_i = 1; } } private function numeric($id) //判断是否为数字 { if (strlen($id)){ if (!ereg("^[0-9]+$",$id)){ $id = 1; }else{ $id = substr($id,0,11); } }else{ $id = 1; } return $id; } private function page_replace($page) //地址替换 { return str_replace("{page}", $page, $this -> page_url); } private function myde_home() //首页 { if($this -> myde_page != 1){ return " <li class=\"page_a\"><a href=\"".$this -> page_replace(1)."\" title=\"首页\" >首页</a></li>\n"; }else{ return " <li>首页</li>\n"; } } private function myde_prev() //上一页 { if($this -> myde_page != 1){ return " <li class=\"page_a\"><a href=\"".$this -> page_replace($this->myde_page-1) ."\" title=\"上一页\" >上一页</a></li>\n"; }else{ return " <li>上一页</li>\n"; } } private function myde_next() //下一页 { if($this -> myde_page != $this -> myde_page_count){ return " <li class=\"page_a\"><a href=\"".$this -> page_replace($this->myde_page+1) ."\" title=\"下一页\" >下一页</a></li>\n"; }else{ return " <li>下一页</li>\n"; } } private function myde_last() //尾页 { if($this -> myde_page != $this -> myde_page_count){ return " <li class=\"page_a\"><a href=\"".$this -> page_replace($this -> myde_page_count)."\" title=\"尾页\" >尾页</a></li>\n"; }else{ return " <li>尾页</li>\n"; } } function myde_write($id='page') //输出 { $str = "<div id=\"".$id."\" class=\"pages\">\n <ul>\n "; $str .= " <li>总记录:<span>".$this -> myde_count."</span></li>\n"; $str .= " <li><span>".$this -> myde_page."</span>/<span>".$this -> myde_page_count."</span></li>\n"; $str .= $this -> myde_home(); $str .= $this -> myde_prev(); for($page_for_i = $this -> page_i;$page_for_i <= $this -> page_ub; $page_for_i++){ if($this -> myde_page == $page_for_i){ $str .= " <li class=\"on\">".$page_for_i."</li>\n"; } else{ $str .= " <li class=\"page_a\"><a href=\"".$this -> page_replace($page_for_i)."\" title=\"第".$page_for_i."页\">"; $str .= $page_for_i . "</a></li>\n"; } } $str .= $this -> myde_next(); $str .= $this -> myde_last(); $str .= " <li class=\"pages_input\"><input type=\"text\" value=\"".$this -> myde_page."\""; $str .= " onkeydown=\"javascript: if(event.keyCode==13){ location='"; $str .= $this -> page_replace("'+this.value+'")."';return false;}\""; $str .= " title=\"输入您想要到达的页码\" /></li>\n"; $str .= " </ul>\n <div class=\"page_clear\"></div>\n</div>"; return $str; }}/*-------------------------实例--------------------------------*$page = new PageClass(1000,5,$_GET['page'],'?page={page}');//用于动态$page = new PageClass(1000,5,$_GET['page'],'list-{page}.html');//用于静态或者伪静态$page -> myde_write();//显示*/?> index.php<?phpinclude 'page.Class.php';$page = $_GET['page'];?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>PHP分页程序</title><style type="text/css">* { padding:0; margin:0;}body { font: 14px Arial, Helvetica, sans-serif; overflow-x: hidden; overflow-y:auto; color:#444444;}#main { width:600px; margin:20px auto;}#main table{ margin-top:8px; width:100%; border-collapse:collapse; border-spacing:0px; BACKGROUND-COLOR: #EFEFEF; border:0px;}#main table td{ line-height:20px; border:1px solid #FFF; padding:5px;}#main table thead td { background:#C6C6C6; FONT-SIZE:15px; text-align:center; line-height:23px; font-weight:bold;}/** *---------------分页样式----------------- */.pages { font-family:Arial, Helvetica, sans-serif; font-size:12px;}.pages li { display:inline; float: left; padding:0px 5px; height:21px; color:#666; line-height:21px; margin-right: 0.3em; border: 1px solid #E0E0E0; background:#FFF;}.pages li span { color:#0044DD; background:#FFF;}.pages li.page_a { padding:0; border:0;}.pages li.page_a a { FLOAT: left; display: block; padding:0px 5px; color:#0044DD; border: 1px solid #E0E0E0;}.pages li.page_a a:hover { display: block; color:red; border: 1px solid #A0A0A0;}.pages li.pages_input { padding:0; border: 1px solid #A0A0A0;}.pages li.pages_input input { width:18px; font-size:14px; border:0px; padding:0px 3px; margin:0px 3px; text-align:center;}.pages .on { padding:0px 5px; color: red; font-weight:bold;}.pages .page_clear { clear:both;}/** *------------------分页样式--------------------- */</style></head><body><div id="main"> <a href="?action=">新闻列表</a> <a href="?action=add">添加新闻</a> <?php$mysql = new mysql_Class('localhost','ManageSoft','ELbaY6aLevB6AWUa');$mysql -> select_db('myde520');switch($_GET['action']){ case 'add': ?> <form action="?action=act_add" method="post"> <table border="0" cellpadding="0" cellspacing="0" class="table02"> <thead> <tr> <td colspan="2"><div align="center">添加新闻</div></td> </tr> </thead> <tr> <td><div align="right">新闻标题:</div></td> <td><input name="title" type="text" class="input" id="title" value="" size="40" /></td> </tr> <tr> <td><div align="right">时间:</div></td> <td><input name="date" type="text" class="input" id="date" value="<?php echo date("Y-m-d");?>" size="40" /> </td> </tr> <tr> <td><div align="right">内容:</div></td> <td> <textarea name="content" id="content" cols="50" rows="8"></textarea> </td> </tr> <tr> <td colspan="2"><div align="center"> <input type="submit" name="button" id="button" value="添加新闻" /> <input type="reset" name="button2" id="button2" value="重置" /> </div></td> </tr> </table> </form> <?php break; case 'act_add': $sql = "INSERT INTO `news` (`title`,`content`,`date`) VALUES('".$_POST['title']; $sql .= "','".$_POST['content']."','".$_POST['date']."')"; $mysql -> query($sql); msg('添加成功!','?action='); break; case 'edit': $sql = "select * from `news` where id=".$_GET['id']; $query = $mysql -> query($sql); $row = $mysql -> fetch_array($query); if($row){ ?> <form action="?action=act_edit" method="post"> <table border="0" cellpadding="0" cellspacing="0" class="table02"> <thead> <tr> <td colspan="2"><div align="center">修改新闻</div></td> </tr> </thead> <tr> <td><div align="right">新闻标题:</div></td> <td><input name="title" type="text" class="input" id="title" value="<?php echo $row['title'];?>" size="40" /></td> </tr> <tr> <td><div align="right">时间:</div></td> <td><input name="date" type="text" class="input" id="date" value="<?php echo $row['date'];?>" size="40" /> </td> </tr> <tr> <td><div align="right">内容:</div></td> <td> <textarea name="content" id="content" cols="50" rows="8"><?php echo $row['content'];?></textarea> </td> </tr> <tr> <td colspan="2"><div align="center"> <input type="submit" name="button" id="button" value="修改新闻" /> <input type="hidden" id="id" name="id" value="<?php echo $_GET['id'];?>" /> <input type="reset" name="button2" id="button2" value="重置" /> </div></td> </tr> </table> </form> <?php }else{ msg('要修改的记录不存在!','?action='); } break; case 'act_edit': $sql = "select id from `news` where id=".$_POST['id']; $query = $mysql -> query($sql); $row = $mysql -> fetch_array($query); if($row){ $sql = "update `news` set `title`='".$_POST['title']."'"; $sql .= ",`content`='".$_POST['content']."',`date`='".$_POST['date']."' where `id`=".$_POST['id']; $mysql -> query($sql); msg('修改成功!','?action='); } break; case 'del': $sql = "select * from `news` where `id`=".$_GET['id']; $query = $mysql -> query($sql); $row = $mysql -> fetch_array($query); if($row){ $mysql -> query("delete from `news` where `id`=".$_GET['id']); msg('删除成功!','?action='); }else{ msg('记录不存在!','?action='); } break; case '': $count = $mysql -> num_rows($mysql -> query("select id from `news`")); $pages = new PageClass($count,1,$_GET['page'],'index.php?page={page}');//分别为(记录总数,每页显示多少条记录,当前面,显示方式) //特别说明:如果是伪静态分页或者纯静态分页,那么显示方式需要改,比如改成index-{page}.html $sql = "select * from `news` order by "; $sql .= "id Desc limit ".$pages -> page_limit.",".$pages -> myde_size; $result = $mysql -> query($sql); ?> <table class="table"> <thead> <tr> <td>新闻标题</td> <td width="60"><div align="center">时间</div></td> <td width="80"><div align="center">操作</div></td> </tr> </thead> <?php while($row = $mysql -> fetch_array($result)){ ?> <tr> <td><a href="?action=edit&id=<?php echo $row['id'];?>"><?php echo $row['title'];?></a></td> <td><div align="center"><?php echo $row['date'];?></div></td> <td><div align="center"><a href="?action=edit&id=<?php echo $row['id'];?>">修改</a> <a href="?action=del&id=<?php echo $row['id'];?>">删除 </a></div></td> </tr> <?php } ?> </table> <br /> <?php echo $pages -> myde_write(); break;}?></div></body></html><?phpfunction msg($msg,$url){ echo "<script type=\"text/javascript\">alert('$msg');window.location.href='$url';</script>";}/** *-------------------------数据库操作类-----------------------------**/class mySql_Class{ function __construct($host, $user, $pass) { @mysql_connect($host,$user,$pass) or die("数据库连接失败!"); mysql_query("SET NAMES 'gbk'"); } function select_db($db)//连接表 { return @mysql_select_db($db); } function query($sql)//执行SQL语句 { return @mysql_query($sql); } function fetch_array($fetch_array) { return @mysql_fetch_array($fetch_array, MYSQL_ASSOC); } function num_rows($sql) { return @mysql_num_rows($sql); } }?> 其实分页是一件很简单的事,千万别把它想难了,那只不过是一个简单的数数问题,只要你把它数清楚了并用语言表达出来就行了。下面这个小程序很简单,加到页面上就能用,希望能对初学者掌握分页的思路有帮助,<?php$q=20; //每页显示的记录数,$rows=mysql_num_rows($result); //记录总条数$m=Intval($rows/$q); //取整if($rows%$q>0) $m=$m+1; //取余,$m为总页数$p=$_GET['p']; //$p是用户点击的页数if($p=="")$p=1;echo"<a href='1.php?p=1'> 首页 </a>"; echo"<a href='1.php?p=".max(($p-1),1)."'> 上一页 </a>"; for($n=1;$n<11 and $n<=$m;$n++) echo"<a href='1.php?p=$n'> $n </a>";echo"<a href='1.php?p=".min(($p+1),$m)."'> 下一页 </a>"; echo"<a href='1.php?p=$m'> 末页 </a>"; if($result!=""){ //第$p页显示第 ($p-1)*$q+1 -> $p*$q 条}?> 看看你们写的,非要把html参杂在里边 php有没有一个函数可以查看该文件可以调用的函数定义 在线等分割字段的问题,高手进 php+oracle的问题 换IP了,报错! phpdesigner 怎么配置自动换行 正则求助 addslashes的还原 求救!!!! php做网页登陆页面,传递数据,变量无效,求解 mysql的运行问题?在任务栏上没有”三角灯“,如何解决? 正则匹配求助 php 图片列表展示 XAMPP里面,更改不了magic_quotes_gpc
程式員,真的很不好混啊...
/**
* @desc 分頁計算顯示,呼叫該函數的頁面應以GET方式接收page的變數值
* @param $num 記錄總數目
* @param $perpage 每頁顯示的記錄數
* @param $curpage 當前顯示的頁數
* @param $mpurl 調用該函數的頁面URL
* @param $shownum 顯示出的頁目數量
* @param $offset 當前頁目的左偏移量
* @return 分頁排列
*/
function showpages($num,$perpage,$curpage,$mpurl,$shownum=5,$offset = 2){
$showpage = '';
if($num <= $perpage){
$showpage = "<strong>1</strong>";
}else{
//處理鏈接
$mpurl .= strpos($mpurl,'?')?'&':'?';
$totalpage = ceil($num/$perpage);
//計算顯示頁數
if($shownum > $totalpage){
$from = 1;
$to = $totalpage;
}else{
$from = $curpage - $offset;
$to = $from + $shownum - 1;
if($from < 1){
$to = $curpage + 1 - $from;
$from = 1;
if($to - $from < $shownum) {
$to = $shownum;
}
} elseif($to > $totalpage) {
$from = $totalpage - $shownum + 1;
$to = $totalpage;
}
}
//處理顯示
$pagearr = array();
if($curpage - $offset > 1 && $shownum < $totalpage){
$pagearr[]= "<a href=\"{$mpurl}page=1\">首頁</a>";
}
if($curpage > 1){
$pagearr[] = "<a href=\"{$mpurl}page=".($curpage-1)."\">上一頁</a>";
}
for($i = $from; $i <= $to; $i++){
if($curpage == $i){
$pagearr[] = "<strong>$i</strong>";
}else{
$pagearr[] = "<a href=\"{$mpurl}page=$i\">$i</a>";
}
}
if($curpage < $totalpage){
$pagearr[] = "<a href=\"{$mpurl}page=".($curpage+1)."\">下一頁</a>";
}
if($to < $totalpage){
$pagearr[]= "<a href=\"{$mpurl}page=$totalpage\">末頁</a>";
}
$showpage = implode(' ',$pagearr);
}
return $showpage;
}
// 起始行数
public $firstRow ;
// 列表每页显示行数
public $listRows ;
// 页数跳转时要带的参数
public $parameter ;
// 分页总页面数
protected $totalPages ;
// 总行数
protected $totalRows ;
// 当前页数
protected $nowPage ;
// 分页的栏的总页数
protected $coolPages ;
// 分页栏每页显示的页数
protected $rollPage ;
// 分页显示定制
protected $config = array('header'=>'条记录','prev'=>'上一页','next'=>'下一页','first'=>'第一页','last'=>'最后一页','theme'=>' %totalRow% %header% %nowPage%/%totalPage% 页 %upPage% %downPage% %first% %prePage% %linkPage% %nextPage% %end%'); /**
+----------------------------------------------------------
* 架构函数
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param array $totalRows 总的记录数
* @param array $listRows 每页显示记录数
* @param array $parameter 分页跳转的参数
+----------------------------------------------------------
*/
public function __construct($totalRows,$listRows,$parameter='') {
$this->totalRows = $totalRows;
$this->parameter = $parameter;
$this->rollPage = C('PAGE_ROLLPAGE');
$this->listRows = !empty($listRows)?$listRows:C('PAGE_LISTROWS');
$this->totalPages = ceil($this->totalRows/$this->listRows); //总页数
$this->coolPages = ceil($this->totalPages/$this->rollPage);
$this->nowPage = !empty($_GET[C('VAR_PAGE')])?$_GET[C('VAR_PAGE')]:1;
if(!empty($this->totalPages) && $this->nowPage>$this->totalPages) {
$this->nowPage = $this->totalPages;
}
$this->firstRow = $this->listRows*($this->nowPage-1);
} public function setConfig($name,$value) {
if(isset($this->config[$name])) {
$this->config[$name] = $value;
}
} /**
+----------------------------------------------------------
* 分页显示输出
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
*/
public function show() {
if(0 == $this->totalRows) return '';
$p = C('VAR_PAGE');
$nowCoolPage = ceil($this->nowPage/$this->rollPage);
$url = $_SERVER['REQUEST_URI'].(strpos($_SERVER['REQUEST_URI'],'?')?'':"?").$this->parameter;
$parse = parse_url($url);
if(isset($parse['query'])) {
parse_str($parse['query'],$params);
unset($params[$p]);
$url = $parse['path'].'?'.http_build_query($params);
}
//上下翻页字符串
$upRow = $this->nowPage-1;
$downRow = $this->nowPage+1;
if ($upRow>0){
$upPage="<a href='".$url."&".$p."=$upRow'>".$this->config['prev']."</a>";
}else{
$upPage="";
} if ($downRow <= $this->totalPages){
$downPage="<a href='".$url."&".$p."=$downRow'>".$this->config['next']."</a>";
}else{
$downPage="";
}
// << < > >>
if($nowCoolPage == 1){
$theFirst = "";
$prePage = "";
}else{
$preRow = $this->nowPage-$this->rollPage;
$prePage = "<a href='".$url."&".$p."=$preRow' >上".$this->rollPage."页</a>";
$theFirst = "<a href='".$url."&".$p."=1' >".$this->config['first']."</a>";
}
if($nowCoolPage == $this->coolPages){
$nextPage = "";
$theEnd="";
}else{
$nextRow = $this->nowPage+$this->rollPage;
$theEndRow = $this->totalPages;
$nextPage = "<a href='".$url."&".$p."=$nextRow' >下".$this->rollPage."页</a>";
$theEnd = "<a href='".$url."&".$p."=$theEndRow' >".$this->config['last']."</a>";
}
// 1 2 3 4 5
$linkPage = "";
for($i=1;$i<=$this->rollPage;$i++){
$page=($nowCoolPage-1)*$this->rollPage+$i;
if($page!=$this->nowPage){
if($page<=$this->totalPages){
$linkPage .= " <a href='".$url."&".$p."=$page'> ".$page." </a>";
}else{
break;
}
}else{
if($this->totalPages != 1){
$linkPage .= " <span class='current'>".$page."</span>";
}
}
}
$pageStr = str_replace(
array('%header%','%nowPage%','%totalRow%','%totalPage%','%upPage%','%downPage%','%first%','%prePage%','%linkPage%','%nextPage%','%end%'),
array($this->config['header'],$this->nowPage,$this->totalRows,$this->totalPages,$upPage,$downPage,$theFirst,$prePage,$linkPage,$nextPage,$theEnd),$this->config['theme']);
return $pageStr;
}}
$count为总数,$listrows为每一页的显示条目。
$p = new Page($count,$listRows);
//分页类做好了。
$page = $p->show();
//模板输出
$this->assign('page',$page);
一个框架里面的分页类.用法也就这么简单.自己看吧
/**
*-------------------------翻页类----------------------*
*/
class PageClass
{
private $myde_count; //总记录数
var $myde_size; //每页记录数
private $myde_page; //当前页
private $myde_page_count; //总页数
private $page_url; //页面url
private $page_i; //起始页
private $page_ub; //结束页
var $page_limit;
function __construct($myde_count=0, $myde_size=1, $myde_page=1,$page_url)//构造函数
{
$this -> myde_count = $this -> numeric($myde_count);
$this -> myde_size = 3; //$this -> numeric($myde_size);
$this -> myde_page = $this -> numeric($myde_page);
$this -> page_limit = ($this -> myde_page * $this -> myde_size) - $this -> myde_size;
$this -> page_url = $page_url;
if($this -> myde_page < 1) $this -> myde_page =1;
if($this -> myde_count < 0) $this -> myde_page =0;
$this -> myde_page_count = ceil($this -> myde_count/$this -> myde_size);
if($this -> myde_page_count < 1) $this -> myde_page_count = 1;
if($this -> myde_page > $this -> myde_page_count) $this -> myde_page = $this -> myde_page_count;
$this -> page_i = $this -> myde_page - 2;
$this -> page_ub = $this -> myde_page + 2;
if($this -> page_i < 1){
$this -> page_ub = $this -> page_ub + (1 - $this -> page_i);
$this -> page_i = 1;
}
if($this -> page_ub > $this -> myde_page_count){
$this -> page_i = $this -> page_i - ($this -> page_ub - $this -> myde_page_count);
$this -> page_ub = $this -> myde_page_count;
if($this -> page_i < 1) $this -> page_i = 1;
}
}
private function numeric($id) //判断是否为数字
{
if (strlen($id)){
if (!ereg("^[0-9]+$",$id)){
$id = 1;
}else{
$id = substr($id,0,11);
}
}else{
$id = 1;
}
return $id;
}
private function page_replace($page) //地址替换
{
return str_replace("{page}", $page, $this -> page_url);
}
private function myde_home() //首页
{
if($this -> myde_page != 1){
return " <li class=\"page_a\"><a href=\"".$this -> page_replace(1)."\" title=\"首页\" >首页</a></li>\n";
}else{
return " <li>首页</li>\n";
}
}
private function myde_prev() //上一页
{
if($this -> myde_page != 1){
return " <li class=\"page_a\"><a href=\"".$this -> page_replace($this->myde_page-1) ."\" title=\"上一页\" >上一页</a></li>\n";
}else{
return " <li>上一页</li>\n";
}
}
private function myde_next() //下一页
{
if($this -> myde_page != $this -> myde_page_count){
return " <li class=\"page_a\"><a href=\"".$this -> page_replace($this->myde_page+1) ."\" title=\"下一页\" >下一页</a></li>\n";
}else{
return " <li>下一页</li>\n";
}
}
private function myde_last() //尾页
{
if($this -> myde_page != $this -> myde_page_count){
return " <li class=\"page_a\"><a href=\"".$this -> page_replace($this -> myde_page_count)."\" title=\"尾页\" >尾页</a></li>\n";
}else{
return " <li>尾页</li>\n";
}
}
function myde_write($id='page') //输出
{
$str = "<div id=\"".$id."\" class=\"pages\">\n <ul>\n ";
$str .= " <li>总记录:<span>".$this -> myde_count."</span></li>\n";
$str .= " <li><span>".$this -> myde_page."</span>/<span>".$this -> myde_page_count."</span></li>\n";
$str .= $this -> myde_home();
$str .= $this -> myde_prev();
for($page_for_i = $this -> page_i;$page_for_i <= $this -> page_ub; $page_for_i++){
if($this -> myde_page == $page_for_i){
$str .= " <li class=\"on\">".$page_for_i."</li>\n";
}
else{
$str .= " <li class=\"page_a\"><a href=\"".$this -> page_replace($page_for_i)."\" title=\"第".$page_for_i."页\">";
$str .= $page_for_i . "</a></li>\n";
}
}
$str .= $this -> myde_next();
$str .= $this -> myde_last();
$str .= " <li class=\"pages_input\"><input type=\"text\" value=\"".$this -> myde_page."\"";
$str .= " onkeydown=\"javascript: if(event.keyCode==13){ location='";
$str .= $this -> page_replace("'+this.value+'")."';return false;}\"";
$str .= " title=\"输入您想要到达的页码\" /></li>\n";
$str .= " </ul>\n <div class=\"page_clear\"></div>\n</div>";
return $str;
}
}
/*-------------------------实例--------------------------------*
$page = new PageClass(1000,5,$_GET['page'],'?page={page}');//用于动态
$page = new PageClass(1000,5,$_GET['page'],'list-{page}.html');//用于静态或者伪静态
$page -> myde_write();//显示
*/
?>
include 'page.Class.php';
$page = $_GET['page'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>PHP分页程序</title>
<style type="text/css">
* {
padding:0;
margin:0;
}
body {
font: 14px Arial, Helvetica, sans-serif;
overflow-x: hidden;
overflow-y:auto;
color:#444444;
}
#main {
width:600px;
margin:20px auto;
}
#main table{
margin-top:8px;
width:100%;
border-collapse:collapse;
border-spacing:0px;
BACKGROUND-COLOR: #EFEFEF;
border:0px;
}
#main table td{
line-height:20px;
border:1px solid #FFF;
padding:5px;
}
#main table thead td {
background:#C6C6C6;
FONT-SIZE:15px;
text-align:center;
line-height:23px;
font-weight:bold;
}/**
*---------------分页样式-----------------
*/.pages {
font-family:Arial, Helvetica, sans-serif;
font-size:12px;
}
.pages li {
display:inline;
float: left;
padding:0px 5px;
height:21px;
color:#666;
line-height:21px;
margin-right: 0.3em;
border: 1px solid #E0E0E0;
background:#FFF;
}
.pages li span {
color:#0044DD;
background:#FFF;
}
.pages li.page_a {
padding:0;
border:0;
}
.pages li.page_a a {
FLOAT: left;
display: block;
padding:0px 5px;
color:#0044DD;
border: 1px solid #E0E0E0;
}
.pages li.page_a a:hover {
display: block;
color:red;
border: 1px solid #A0A0A0;
}
.pages li.pages_input {
padding:0;
border: 1px solid #A0A0A0;
}
.pages li.pages_input input {
width:18px;
font-size:14px;
border:0px;
padding:0px 3px;
margin:0px 3px;
text-align:center;
}
.pages .on {
padding:0px 5px;
color: red;
font-weight:bold;
}
.pages .page_clear {
clear:both;
}
/**
*------------------分页样式---------------------
*/
</style>
</head><body>
<div id="main"> <a href="?action=">新闻列表</a> <a href="?action=add">添加新闻</a>
<?php$mysql = new mysql_Class('localhost','ManageSoft','ELbaY6aLevB6AWUa');$mysql -> select_db('myde520');switch($_GET['action']){
case 'add':
?>
<form action="?action=act_add" method="post">
<table border="0" cellpadding="0" cellspacing="0" class="table02">
<thead>
<tr>
<td colspan="2"><div align="center">添加新闻</div></td>
</tr>
</thead>
<tr>
<td><div align="right">新闻标题:</div></td>
<td><input name="title" type="text" class="input" id="title" value="" size="40" /></td>
</tr>
<tr>
<td><div align="right">时间:</div></td>
<td><input name="date" type="text" class="input" id="date" value="<?php echo date("Y-m-d");?>" size="40" />
</td>
</tr>
<tr>
<td><div align="right">内容:</div></td>
<td>
<textarea name="content" id="content" cols="50" rows="8"></textarea>
</td>
</tr>
<tr>
<td colspan="2"><div align="center">
<input type="submit" name="button" id="button" value="添加新闻" />
<input type="reset" name="button2" id="button2" value="重置" />
</div></td>
</tr>
</table>
</form>
<?php
break;
case 'act_add':
$sql = "INSERT INTO `news` (`title`,`content`,`date`) VALUES('".$_POST['title'];
$sql .= "','".$_POST['content']."','".$_POST['date']."')";
$mysql -> query($sql);
msg('添加成功!','?action=');
break;
case 'edit':
$sql = "select * from `news` where id=".$_GET['id'];
$query = $mysql -> query($sql);
$row = $mysql -> fetch_array($query);
if($row){
?>
<form action="?action=act_edit" method="post">
<table border="0" cellpadding="0" cellspacing="0" class="table02">
<thead>
<tr>
<td colspan="2"><div align="center">修改新闻</div></td>
</tr>
</thead>
<tr>
<td><div align="right">新闻标题:</div></td>
<td><input name="title" type="text" class="input" id="title" value="<?php echo $row['title'];?>" size="40" /></td>
</tr>
<tr>
<td><div align="right">时间:</div></td>
<td><input name="date" type="text" class="input" id="date" value="<?php echo $row['date'];?>" size="40" />
</td>
</tr>
<tr>
<td><div align="right">内容:</div></td>
<td>
<textarea name="content" id="content" cols="50" rows="8"><?php echo $row['content'];?></textarea>
</td>
</tr>
<tr>
<td colspan="2"><div align="center">
<input type="submit" name="button" id="button" value="修改新闻" />
<input type="hidden" id="id" name="id" value="<?php echo $_GET['id'];?>" />
<input type="reset" name="button2" id="button2" value="重置" />
</div></td>
</tr>
</table>
</form>
<?php
}else{
msg('要修改的记录不存在!','?action=');
}
break;
case 'act_edit':
$sql = "select id from `news` where id=".$_POST['id'];
$query = $mysql -> query($sql);
$row = $mysql -> fetch_array($query);
if($row){
$sql = "update `news` set `title`='".$_POST['title']."'";
$sql .= ",`content`='".$_POST['content']."',`date`='".$_POST['date']."' where `id`=".$_POST['id'];
$mysql -> query($sql);
msg('修改成功!','?action=');
}
break;
case 'del':
$sql = "select * from `news` where `id`=".$_GET['id'];
$query = $mysql -> query($sql);
$row = $mysql -> fetch_array($query);
if($row){
$mysql -> query("delete from `news` where `id`=".$_GET['id']);
msg('删除成功!','?action=');
}else{
msg('记录不存在!','?action=');
}
break;
case '':
$count = $mysql -> num_rows($mysql -> query("select id from `news`"));
$pages = new PageClass($count,1,$_GET['page'],'index.php?page={page}');//分别为(记录总数,每页显示多少条记录,当前面,显示方式)
//特别说明:如果是伪静态分页或者纯静态分页,那么显示方式需要改,比如改成index-{page}.html
$sql = "select * from `news` order by ";
$sql .= "id Desc limit ".$pages -> page_limit.",".$pages -> myde_size;
$result = $mysql -> query($sql);
?>
<table class="table">
<thead>
<tr>
<td>新闻标题</td>
<td width="60"><div align="center">时间</div></td>
<td width="80"><div align="center">操作</div></td>
</tr>
</thead>
<?php
while($row = $mysql -> fetch_array($result)){
?>
<tr>
<td><a href="?action=edit&id=<?php echo $row['id'];?>"><?php echo $row['title'];?></a></td>
<td><div align="center"><?php echo $row['date'];?></div></td>
<td><div align="center"><a href="?action=edit&id=<?php echo $row['id'];?>">修改</a> <a href="?action=del&id=<?php echo $row['id'];?>">删除 </a></div></td>
</tr>
<?php
}
?>
</table>
<br />
<?php
echo $pages -> myde_write();
break;}
?>
</div>
</body>
</html>
<?php
function msg($msg,$url)
{
echo "<script type=\"text/javascript\">alert('$msg');window.location.href='$url';</script>";
}/**
*-------------------------数据库操作类-----------------------------*
*/
class mySql_Class
{
function __construct($host, $user, $pass)
{
@mysql_connect($host,$user,$pass) or die("数据库连接失败!");
mysql_query("SET NAMES 'gbk'");
}
function select_db($db)//连接表
{
return @mysql_select_db($db);
}
function query($sql)//执行SQL语句
{
return @mysql_query($sql);
}
function fetch_array($fetch_array)
{
return @mysql_fetch_array($fetch_array, MYSQL_ASSOC);
}
function num_rows($sql)
{
return @mysql_num_rows($sql);
}
}
?>
$q=20; //每页显示的记录数,
$rows=mysql_num_rows($result); //记录总条数
$m=Intval($rows/$q); //取整
if($rows%$q>0) $m=$m+1; //取余,$m为总页数$p=$_GET['p']; //$p是用户点击的页数
if($p=="")$p=1;echo"<a href='1.php?p=1'> 首页 </a>";
echo"<a href='1.php?p=".max(($p-1),1)."'> 上一页 </a>";
for($n=1;$n<11 and $n<=$m;$n++)
echo"<a href='1.php?p=$n'> $n </a>";
echo"<a href='1.php?p=".min(($p+1),$m)."'> 下一页 </a>";
echo"<a href='1.php?p=$m'> 末页 </a>"; if($result!=""){
//第$p页显示第 ($p-1)*$q+1 -> $p*$q 条
}
?>