您的位置:

php经典分页代码,php分页查询mysql

本文目录一览:

在php中经典分页代码?求救啊!

?php

/**

* ------------------------------

* 分类操作类

* ------------------------------

* @name Page.class.php

* @version 2010.04.17

* @author 张宏伟

*/

class Page

{

//总记录条数

var $totalNum;

//每页显示的条目数

var $pageSize = 10;

//总的页数

var $totalPage;

//当前页数

var $page = 1;

//查询结果数据

var $data;

/**

* ------------------------------

* 构造函数

* ------------------------------

* @param string $tbname 要操作的表名

* @param string $where 定位条件

* @param string $field 要查询的字段

* @param string $pageSize 每页显示数量

* @param string $orderBy 排序方式

*/

function Page($tbname,$where='1=1',$field='*',$pageSize=20,$orderBy='',$groupBy='')

{

!extension_loaded('mysql') exit('mysql do not exist!');

!mysql_ping() exit('mysql can not connect!');

if ($where == '') $where = '1=1';

if ($field == '') $field = '*';

//$tbname = tbname($tbname);

$this-pageSize = $pageSize;

//获取总记录条数

if($groupBy)

$sql = "SELECT count(*) as row_num FROM (SELECT count($groupBy) as row_num,$field FROM $tbname WHERE $where GROUP BY $groupBy) table1";

else

$sql = "SELECT count(*) as row_num FROM $tbname WHERE $where";

@$row_num = mysql_fetch_array(mysql_query($sql));

$this-totalNum = $row_num['row_num'];

$this-totalPage = ceil($this-totalNum / $this-pageSize);

//获得当前page

$page = (isset($_GET['page']) $_GET['page'] != '') ? ceil($_GET['page']) : 1;

$this-page = ($page $this-totalPage) ? $page : $this-totalPage;

$this-page = ($this-page 1) ? 1 : $this-page;

//计算查询的起始值

$start = ($this-page - 1) * $this-pageSize;

//查询结果

//$sql = "SELECT $field FROM $tbname WHERE $where AND id $start ORDER BY id ASC".($orderBy ? ",$orderBy" : '')." LIMIT $this-pageSize";

if($groupBy=='')

$sql = "SELECT $field FROM $tbname WHERE $where".($orderBy ? ' ORDER BY '.$orderBy : '')." LIMIT $start,$this-pageSize";

else

$sql = "SELECT $field FROM $tbname WHERE $where GROUP BY $groupBy".($orderBy ? ' ORDER BY '.$orderBy : '')." LIMIT $start,$this-pageSize";

$result = mysql_query($sql);

$data = array();

if($result)

{

while($row = mysql_fetch_assoc($result))

{

$data[] = $row;

}

}

$this-data = $data;

}

/**

* ------------------------------

* 获得查询数据

* ------------------------------

* @return array

*/

function get_data()

{

return $this-data;

}

/**

* ------------------------------

* 构造url

* ------------------------------

* @param int $page 页数

* @return string

*/

function get_url($page)

{

$arr_url = parse_url($_SERVER['REQUEST_URI']);

$php_file = $arr_url['path'];

if (!isset($arr_url['query'])) $arr_url['query'] = '';

parse_str($arr_url['query'],$arr_get);

$arr_get['page'] = $page;

$str_url = '';

foreach ($arr_get as $k = $v)

{

$str_url .= $k.'='.$v.'';

}

$str_url = substr($str_url,0,-1);

return $php_file.'?'.$str_url;

}

/**

* ------------------------------

* 获得分页的基本信息

* ------------------------------

* @return array

*/

function get_basic_info()

{

return array

(

'first' = $this-get_url(1),

'prev' = $this-get_url($this-page-1),

'next' = $this-get_url($this-page+1),

'last' = $this-get_url($this-totalPage),

'totalNum' = $this-totalNum,

'totalPage' = $this-totalPage,

'page' = $this-page

);

}

/**

* ------------------------------

* select方式页面跳转框

* ------------------------------

* @return string

*/

function button_select()

{

$str = "select onchange=location.href='".$this-get_url('')."'+this.value";

for ($i=1;$i=$this-totalPage;$i++)

{

$selected = ($this-page == $i) ? 'selected' : '';

$str .= "option value=$i $selected$i/option";

}

return $str .= '/select';

}

/**

* ------------------------------

* 普通页码条

* ------------------------------

* @param int $totalNum 是否显示总页数,0为不显示(默认显示)

* @param int $correntNum 是否显示当前页数,0为不显示(默认显示)

* @return string

*/

function button_basic($totNum=1,$correntNum=1,$firstAndLast=1)

{

extract($this-get_basic_info());

$str = '';

$str .= ($totNum ? 'total'.$totalNum.'bar    ' : '');

$str .= ($correntNum ? $page.'/'.$totalPage.'page    ' : '');

$str .= ($firstAndLast ? ($totalNum != 1 ? "a href='$first'homepage/a" : 'homepage')."    " : '');

$str .= ($page-1 0 ? "a href='$prev'reverse/a" : 'reverse')."    ";

$str .= ($page+1 = $totalPage ? "a href='$next'next page/a" : 'next page')."    ";

$str .= ($firstAndLast ? ($totalNum != 1 ? "a href='$last'last page/a" : 'last page')."    " : '');

return $str;

}

/**

* ------------------------------

* 有样式的按钮

* ------------------------------

* @param string $buttin_color redpage green

* @return string

*/

function button_style($buttin_style='redpage')

{

extract($this-get_basic_info());

$str = 'div class='.$buttin_style.'';

$str .= $page-1 0 ? "a href=$prev/a" : "span class=disabled/span";

if ($totalPage 10)

{

for ($i=1;$i=$totalPage;$i++)

{

$url = $this-get_url($i);

if ($i == $page)

{

$str .= "span class=current$i/span";

continue;

}

$str.= "a href=$url$i/a";

}

}

else

{

if ($page 7)

{

for ($i=1;$i8;$i++)

{

$url = $this-get_url($i);

if ($i == $page)

{

$str .= "span class=current$i/span";

continue;

}

$str.= "a href=$url$i/a";

}

$str .= '...';

}

elseif ($page = 7)

{

for ($i=1;$i3;$i++)

{

$url = $this-get_url($i);

$str .= "a href=$url$i/a";

}

$str .= '...';

for ($i=$page-2;$i=$page+2;$i++)

{

if ($i == $totalPage - 1) break;

$url = $this-get_url($i);

if ($i == $page)

{

$str .= "span class=current$i/span";

continue;

}

$str.= "a href=$url$i/a";

}

if ($page $totalPage - 4) $str .= '...';

}

$str .= 'a href='.$this-get_url($totalPage-1).''.($totalPage-1).'/a';

$str .= 'a href='.$this-get_url($totalPage).''.$totalPage.'/a';

}

$str .= $page $totalPage ? "a href=$next/a/div" : "span class=disabled/span/div";

return $str;

}

}

?

php分页代码

?php

/**

* @author SEPH

* @desc pageft

* @date 2009-4-20

*/

//为了避免重复包含文件而造成错误,加了判断函数是否存在的条件:

if(!@function_exists(pageft)){

//定义函数pageft(),三个参数的含义为:

//$totle:信息总数;

//$displaypg:每页显示信息数,这里设置为默认是20;

//$url:分页导航中的链接,除了加入不同的查询信息“page”外的部分都与这个URL相同。

// 默认值本该设为本页URL(即$_SERVER["REQUEST_URI"]),但设置默认值的右边只能为常量,所以该默认值设为空字符串,在函数内部再设置为本页URL。

function pageft($totle,$displaypg=20,$url=''){

//定义几个全局变量:

//$page:当前页码;

//$firstcount:(数据库)查询的起始项;

//$pagenav:页面导航条代码,函数内部并没有将它输出;

//$_SERVER:读取本页URL“$_SERVER["REQUEST_URI"]”所必须。

global $page,$firstcount,$pagenav,$_SERVER;

//为使函数外部可以访问这里的“$displaypg”,将它也设为全局变量。注意一个变量重新定义为全局变量后,原值被覆盖,所以这里给它重新赋值。

$GLOBALS["displaypg"]=$displaypg;

/*if(!$page) $page=1*/;

if(@$_GET['page'])

$page = intval($_GET['page']);

else

$page = 1;

//如果$url使用默认,即空值,则赋值为本页URL:

if(!$url){ $url=$_SERVER["REQUEST_URI"];}

//URL分析:

$parse_url=parse_url($url);

$url_query=@$parse_url["query"]; //单独取出URL的查询字串

if($url_query){

//因为URL中可能包含了页码信息,我们要把它去掉,以便加入新的页码信息。

//这里用到了正则表达式,请参考“PHP中的正规表达式”()

$url_query=ereg_replace("(^|)page=$page","",$url_query);

//将处理后的URL的查询字串替换原来的URL的查询字串:

$url=str_replace($parse_url["query"],$url_query,$url);

//在URL后加page查询信息,但待赋值:

if($url_query) $url.="page"; else $url.="page";

}else {

$url.="?page";

}

//页码计算:

$lastpg=ceil($totle/$displaypg); //最后页,也是总页数

$page=min($lastpg,$page);

$prepg=$page-1; //上一页

$nextpg=($page==$lastpg ? 0 : $page+1); //下一页

$firstcount=($page-1)*$displaypg;

//开始分页导航条代码:

$pagenav="显示第 B".($totle?($firstcount+1):0)."/B-B".min($firstcount+$displaypg,$totle)."/B 条记录,共 $totle 条记录BR";

//如果只有一页则跳出函数:

if($lastpg=1) return false;

$pagenav.=" a href='$url=1' target=_self首页/a ";

if($prepg) $pagenav.=" a href='$url=$prepg' target=_self前页/a "; else $pagenav.=" 前页 ";

if($nextpg) $pagenav.=" a href='$url=$nextpg' target=_self后页/a "; else $pagenav.=" 后页 ";

$pagenav.=" a href='$url=$lastpg' target=_self尾页/a ";

//下拉跳转列表,循环列出所有页码:

$pagenav.=" 到第 select name='topage' size='1' onchange='window.location=\"$url=\"+this.value'\n";

for($i=1;$i=$lastpg;$i++){

if($i==$page) $pagenav.="option value='$i' selected$i/option\n";

else $pagenav.="option value='$i'$i/option\n";

}

$pagenav.="/select 页,共 $lastpg 页";

}

}/*没有用Get传值*/

?

如何整理PHP分页代码,使其实现经典分页样式

如何整理PHP分页代码,使其实现经典分页样式

?php

require_once("conn.php");

$ind_cont_sel_sql="select * from `board` order by `sn` desc";

$ind_cont_sel_query = mysql_query($ind_cont_sel_sql);

$page_info_num = 3;

$page_def = 1;

if (isset($_GET['pagenum'])){

$page_def = $_GET['pagenum'];

}

$sql_page_num = ($page_def - 1)*$page_info_num;

$sql_query_limit=$ind_cont_sel_sql." LIMIT ".$sql_page_num.",".$page_info_num;

$sql_query_limit_info=mysql_query($sql_query_limit);

$all_info_num = mysql_num_rows($ind_cont_sel_query);

$all_page_num = ceil($all_info_num/$page_info_num);

?

table width="100%" border="0"

?php

$i=0;

while($ind_cont_sel_row=mysql_fetch_assoc($sql_query_limit_info)){ $i++; ?

tr

td colspan="2"div class="post-utility"ph1u?php echo $sql_page_num+$i;?/u/h1/p/div/td

/tr

tr

td width="80px"ptitle/p/tdtd?php echo nl2br($ind_cont_sel_row["title"]);?/td

/tr

tr

td width="80px"pname/p/tdtd?php echo nl2br($ind_cont_sel_row["name"]);?/td

/tr

tr

td width="80px"ptime/p/tdtd?php echo nl2br($ind_cont_sel_row["time"]);?/td

/tr

tr

td width="80px"pcontent/p/tdtd?php echo nl2br($ind_cont_sel_row["content"]);?/td

/tr

trtd colspan="2"hr/td/tr

?php } ?

trtd colspan="2" height="10px" align="center"

a href="?pagenum=1"page/a

?

if ($_GET["pagenum"]!=1){ ?

a href="?pagenum=? echo $_GET["pagenum"]-1; ?"up/a

?

}else{

echo "";

} ?

?

for($page_num=($_GET["pagenum"]-2);$page_num$_GET["pagenum"];$page_num++){

if ($page_num=0){

echo "";

}else{?

a href="?pagenum=?php echo $page_num;?"?php echo $page_num;?/a

?

}}

?

?php echo $_GET["pagenum"];?

!--a href="?pagenum=?php echo $_GET["pagenum"];?"?php echo $_GET["pagenum"];?/a--

?

for($page_num=$_GET["pagenum"]+1;$page_num=($_GET["pagenum"]+2);$page_num++){

if ($page_num$all_page_num){

echo "";

}else{

?

a href="?pagenum=?php echo $page_num;?"?php echo $page_num;?/a

?

}

}

?

?

if ($_GET["pagenum"]$all_page_num){ ?

a href="?pagenum=? echo $_GET["pagenum"]+1; ?"next/a

?

}else{

echo "";

} ?

a href="?pagenum=?php echo $all_page_num;?"last/a

/td

/tr

/table

求php分页代码

////////////////////////////////分页/////////////////////////////

$pageListNum=20;//每页显示10条

$totalPage=0;//总页数

$page=isset($page)?(int)$page:1;//当前页

$start=($page-1)*$pageListNum;//起始编号

$totalPage=ceil($pageNum/$pageListNum);

require_once(WEB_CLASS.'/page_class.php');

$page=new PageClass($page,$totalPage);

$showpage=$page-showPage();

echo $showpage;

///////////////page_class.php////////////////////////

?php

header("Content-Type:text/html;charset=utf8");

/**

* 分页类

* URL有多个参数也能分页,还能自定义分页样式

* php=5.0

* @version 0.1.1

* @copyright 2006-2010

* @package class

*/

class PageClass{

private $url;

private $cpage;

private $totalPage;

private $tpl;

/**

* PageClass的构造函数

* 模板说明:{index}表示首页 {pagelist}链接列表 {option}下拉列表框 {next}下一页 {pre}上一页 {cur}当前页 {index=首页}表示首页的链接文字为首页,即=号后为链接文字,不过这对{pagelist}{option}无效

* @param string $cpage 当前页

* @param string $tatolPage 总页数

* @param string $tpl 模板.

* @param string $url 要分页的url 默认为当前页

* @return PageClass

*/

function __construct($cpage,$totalPage,$tpl='',$url=''){

$this-cpage=$cpage;

$this-totalPage=$totalPage;

if(strlen($tpl)==0){

$this-tpl="{cur=当前页}{index=首页} {pre=上一页} {next=下一页} {end=最后页} {option}"; //中文分页

}else{

$this-tpl=$tpl;

}

if(strlen($url)==0){

$this-url=$_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"];

}else{

$this-url=$url;

}

}

/**

* 函数showPage,返回生成的分页HTML

* @return string

*/

function showPage(){

//显示分页

$urlOption=array();//url的后缀如:?page=1typeid=1

$parse_url=parse_url($this-url);

$urlMain='http://'.$parse_url['path'];

if($parse_url['query']){

//url有参数

$urlArr=split('',$parse_url['query']);

if(is_array($urlArr)){

foreach($urlArr as $key=$value){

$c=split('=',$value);

if($c[0]==page){

}else{

array_push($urlOption,$c[0].'='.$c[1]);

}

}

}

}else{

//url没有参数

//if($this-cpage$this-totalPage){

// array_push($urlOption,"page=2");

//}

}

if(is_array($urlOption)){

$urlOptionStr_t=implode('',$urlOption);

}

if(strlen($urlOptionStr_t)0){

$urlOptionStr.=''.$urlOptionStr_t;

}

$tplcontent=$this-tpl;//分页模板

$showPage=$tplcontent;

//首页

if (preg_match_all('/\{index=([^}]*+)\}/', $tplcontent, $matches)){

$t_tpl=$matches[0][0]; //模板内容

$t_word=$matches[1][0]; //分页字段

$indexStr='a href="'.$urlMain.'?page=1'.$urlOptionStr.'"'.$t_word.'/a';

$showPage=str_replace($t_tpl,$indexStr,$showPage);

}

//当前页

if (preg_match_all('/\{cur=([^}]*+)\}/', $tplcontent, $matches)){

$t_tpl=$matches[0][0];

$t_word=$matches[1][0];

$curStr=$t_word.$this-cpage.'/'.$this-totalPage;

$showPage=str_replace($t_tpl,$curStr,$showPage);

}

//末页

if (preg_match_all('/\{end=([^}]*+)\}/', $tplcontent, $matches)){

$t_tpl=$matches[0][0];

$t_word=$matches[1][0];

$endPage='a href="'.$urlMain.'?page='.$this-totalPage.$urlOptionStr.'"'.$t_word.'/a';

$showPage=str_replace($t_tpl,$endPage,$showPage);

}

//上一页

if (preg_match_all('/\{pre=([^}]*+)\}/', $tplcontent, $matches)){

$t_tpl=$matches[0][0];

$t_word=$matches[1][0];

if($this-cpage1){

$prePage='a href="'.$urlMain.'?page='.($this-cpage-1).$urlOptionStr.'"'.$t_word.'/a';

}else{

$prePage=$t_word;

}

$showPage=str_replace($t_tpl,$prePage,$showPage);

}

//下一页

if (preg_match_all('/\{next=([^}]*+)\}/',$tplcontent, $matches)){

$t_tpl=$matches[0][0];

$t_word=$matches[1][0];

if($this-cpage$this-totalPage $this-totalPage1){

$nextPage=' a href="'.$urlMain.'?page='.($this-cpage+1).$urlOptionStr.'"'.$t_word.'/a';

}else{

$nextPage=$t_word;

}

$showPage=str_replace($t_tpl,$nextPage,$showPage);

}

//链接列表

if (preg_match("{pagelist}",$tplcontent)){

for($i=1;$i$this-totalPage+1;$i++){

$linkPage.=' a href="'.$urlMain.'?page='.$i.$urlOptionStr.'"'.$i.'/a';

}

$showPage=str_replace('{pagelist}',$linkPage,$showPage);

}

//下拉框分页

if (preg_match("{option}",$tplcontent)){

$optionPage='select onchange="javascript:window.location='."'".$urlMain."?page='+this.options[this.selectedIndex].value+"."'$urlOptionStr'".';"';

for($i=1;$i$this-totalPage+1;$i++){

if($i==$this-cpage){

$optionPage.="option selected='selected' value='$i'第".$i."页/option\n";

}else{

$optionPage.="option value='$i'第".$i."页/option\n";

}

}

$optionPage.='/select';

$showPage=str_replace('{option}',$optionPage,$showPage);

}

return $showPage;

}

}

?