各位大侠,小弟做一个分页程序,在网上找了一段代码,但是分页确不显示。请各位不吝赐教。
这是调用页面,也是程序主页面Thumbnail.php:
<?php
include_once("conn.php");
include("PageDividing.php");
//$db = new db("localhost","root","19781020","dating");
//$db->connect();
$pagenum = 10;
$widthnum = 5;
$query = sprintf("select count(*) from `user");
$info = mysql_query($query,$conn);
$row = MySQL_fetch_row( $info );
if($row)
{
$total =$row[0];
}
//mysql_close($conn);
$pa = new pages($pagenum,$total,$widthnum);
$page = $pa->check_page($_GET[page]);
$t = ($page-1)*$pagenum;$sql ="select id,username from `user` limit {$t},{$pagenum}";
$result = mysql_query($sql,$conn);
while($arr = MySQL_fetch_row($result)){
echo $arr[1]."<br>";
}
echo $total;
echo $pa->check_page($_GET[page]);$pa->show_page();#
#$pgenum必须参数,为每页显示的记录数
#$total 必须参数,为总记录数
#$widhnum 横向排列页数。
#
#
?>
这个是分页类程序PageDividing.php:
<?phpclass pages
{
private $pagenum;//ÿҳ¼
private $total;//ܼ¼
private $page;//ǰҳ
private $countpage;//ҳ
private $widthnum = 5;//ʾҳ
private $text;
/***
*ʼ
* **/
function __construct($pagenum,$total,$widthnum)
{
$this->pagenum = $pagenum;
$this->total = $total;
$this->countpage = ceil($total/$pagenum);
$this->widthnum = $widthnum;
}
/***
* ֤pageĺϷ
* **/
public function check_page($get)
{
if(isset($get))
{
$this->page=$get;
}
else
{
$this->page=1;
}
return $this->page;
}
/***
*ҳϢ
** */
private function show_title()
{
$this->text .= " $this->countpage ҳ ǰ $this->page ҳ $this->total ¼ ";
} /***
*ҳһҳ
* **/
private function show_home()
{
if($this->page == 1)
{
$this->text .= "ҳ һҳ ";
}
else
{
$this->text .= "<a href='?page=1'>ҳ</a> <a href='?page=".($this->page-1)."'>һҳ</a> ";
}
} /***
* βҳһҳ
* **/
private function show_end()
{
if($this->page == $this->countpage && $this->countpage>=1)
{
$this->text .= "һҳ βҳ";
}
else
{
$this->text .= "<a href='?page=".($this->page+1)."'>һҳ</a> <a href='?page=".($this->countpage)."'>βҳ</a>";
}
}
/***
*ģʽ
* **/
private function width_page()
{
$n=1;
if($this->widthnum <= $this->countpage )
{
if($this->page ==1 || $this->page < $this->widthnum)
{
$n=1;
}
else if($this->page % $this->widthnum == 0)
{
if(($this->page / $this->widthnum + 1)*$this->widthnum <= $this->countpage)
{
$n = $this->page;
}
else
{
$n = $this->countpage-$this->widthnum+1;
}
}
else
{
if(floor($this->page / $this->widthnum+1)*$this->widthnum<= $this->countpage)
{
$n = floor($this->page / $this->widthnum)*$this->widthnum;
}
else
{
$n = $this->countpage-$this->widthnum+1;
}
}
}
else
{
$this->widthnum = $this->countpage;
}
for($i=0;$i<$this->widthnum;$i++)
{
$this->text .= "<a href='?page=".($i+$n)."' style='border:1px solid #006699;padding-left:5px;padding-right:5px;'> ".($i+$n)."</a> ";
} }
/***
*ҳ
* **/
public function show_page()
{
$this->show_title();
$this->show_home();
$this->width_page();
$this->show_end();
return $this->text;
}
}?>在主页面里面,根本不显示分页信息。急急急,请各位不吝赐教。
这是调用页面,也是程序主页面Thumbnail.php:
<?php
include_once("conn.php");
include("PageDividing.php");
//$db = new db("localhost","root","19781020","dating");
//$db->connect();
$pagenum = 10;
$widthnum = 5;
$query = sprintf("select count(*) from `user");
$info = mysql_query($query,$conn);
$row = MySQL_fetch_row( $info );
if($row)
{
$total =$row[0];
}
//mysql_close($conn);
$pa = new pages($pagenum,$total,$widthnum);
$page = $pa->check_page($_GET[page]);
$t = ($page-1)*$pagenum;$sql ="select id,username from `user` limit {$t},{$pagenum}";
$result = mysql_query($sql,$conn);
while($arr = MySQL_fetch_row($result)){
echo $arr[1]."<br>";
}
echo $total;
echo $pa->check_page($_GET[page]);$pa->show_page();#
#$pgenum必须参数,为每页显示的记录数
#$total 必须参数,为总记录数
#$widhnum 横向排列页数。
#
#
?>
这个是分页类程序PageDividing.php:
<?phpclass pages
{
private $pagenum;//ÿҳ¼
private $total;//ܼ¼
private $page;//ǰҳ
private $countpage;//ҳ
private $widthnum = 5;//ʾҳ
private $text;
/***
*ʼ
* **/
function __construct($pagenum,$total,$widthnum)
{
$this->pagenum = $pagenum;
$this->total = $total;
$this->countpage = ceil($total/$pagenum);
$this->widthnum = $widthnum;
}
/***
* ֤pageĺϷ
* **/
public function check_page($get)
{
if(isset($get))
{
$this->page=$get;
}
else
{
$this->page=1;
}
return $this->page;
}
/***
*ҳϢ
** */
private function show_title()
{
$this->text .= " $this->countpage ҳ ǰ $this->page ҳ $this->total ¼ ";
} /***
*ҳһҳ
* **/
private function show_home()
{
if($this->page == 1)
{
$this->text .= "ҳ һҳ ";
}
else
{
$this->text .= "<a href='?page=1'>ҳ</a> <a href='?page=".($this->page-1)."'>һҳ</a> ";
}
} /***
* βҳһҳ
* **/
private function show_end()
{
if($this->page == $this->countpage && $this->countpage>=1)
{
$this->text .= "һҳ βҳ";
}
else
{
$this->text .= "<a href='?page=".($this->page+1)."'>һҳ</a> <a href='?page=".($this->countpage)."'>βҳ</a>";
}
}
/***
*ģʽ
* **/
private function width_page()
{
$n=1;
if($this->widthnum <= $this->countpage )
{
if($this->page ==1 || $this->page < $this->widthnum)
{
$n=1;
}
else if($this->page % $this->widthnum == 0)
{
if(($this->page / $this->widthnum + 1)*$this->widthnum <= $this->countpage)
{
$n = $this->page;
}
else
{
$n = $this->countpage-$this->widthnum+1;
}
}
else
{
if(floor($this->page / $this->widthnum+1)*$this->widthnum<= $this->countpage)
{
$n = floor($this->page / $this->widthnum)*$this->widthnum;
}
else
{
$n = $this->countpage-$this->widthnum+1;
}
}
}
else
{
$this->widthnum = $this->countpage;
}
for($i=0;$i<$this->widthnum;$i++)
{
$this->text .= "<a href='?page=".($i+$n)."' style='border:1px solid #006699;padding-left:5px;padding-right:5px;'> ".($i+$n)."</a> ";
} }
/***
*ҳ
* **/
public function show_page()
{
$this->show_title();
$this->show_home();
$this->width_page();
$this->show_end();
return $this->text;
}
}?>在主页面里面,根本不显示分页信息。急急急,请各位不吝赐教。
分页函数 http://topic.csdn.net/u/20100706/14/dd3bd6a1-576a-46b9-a127-dabea8bb89d8_2.html#r_67529121
下面这个不是很好用吗?//分页函数
function fenye($fenyeurl,$page,$totalnum,$perpage,$rewrite=0){
$page = max($page,1);
$totalpage = ceil($totalnum/$perpage);
$totalpage = $totalpage < 1 ? 1 :$totalpage;
$rangepage = 6; if($page <= $rangepage){
$startpage = 1;
}else{
if(($page + $rangepage) > $totalpage && ($totalpage - $rangepage*2) > 0){
$startpage = $totalpage - $rangepage*2 + 1;
}else{
$startpage = $totalpage <= $rangepage*2 ? 1:($page - $rangepage);
}
} $fileext = $rewrite ? '.html':''; $html = '<ul>';
$html .= '<li><a href="'.$fenyeurl.'1'.$fileext.'">«</a></li>';
$html .= $page > 1 ? '<li><a href="'.$fenyeurl.($page-1).$fileext.'">[Prev]</a></li>':'';
for($i = $startpage;$i < ($startpage + $rangepage*2);$i++){
$html .= '<li><a href="'.$fenyeurl.$i.$fileext.'"'.($page == $i ? ' class="curr"':'').'>'.$i.'</a></li>';
if($i == $totalpage) break;
}
$html .= $page < $totalpage ? '<li><a href="'.$fenyeurl.($page+1).$fileext.'">[Next]</a></li>':'';
$html .= '<li><a href="'.$fenyeurl.$totalpage.$fileext.'">»</a></li>';
$html .= '</ul>'; return $html;
}
即:echo $pa->show_page();
我的这个函数支持 .html 结尾的也支持 list.php?id=&kw=&fromdate=&todate=&page=
这样的格式的分页。因为对于分页,他需要提供的参数就是分页网址,动态分页的网址一般都是page=这样的参数,.html结尾的是我重写的格式的,page ,当前页码只需要 通过 $page = max(intval($_GET['page']),1); 就可以获得的。
另外一个函数,获取分页URL,不管任何分页URL,都可以分离出来(但包括重写的)//获取分页URL ,默认的分页param 是page,可以自己设定
function getfenyeUrl($param='page')
{
unset($_GET[$param]);
$_GET[$param] = $querystr = $comma = '';
foreach($_GET as $key => $val)
{
$querystr .= $comma."$key=$val";
$comma = '&';
}
return '?'.$querystr;
}