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;

}

}

?

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/194429.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-02 14:38
下一篇 2024-12-02 14:38

相關推薦

  • Python周杰倫代碼用法介紹

    本文將從多個方面對Python周杰倫代碼進行詳細的闡述。 一、代碼介紹 from urllib.request import urlopen from bs4 import Bea…

    編程 2025-04-29
  • PHP和Python哪個好找工作?

    PHP和Python都是非常流行的編程語言,它們被廣泛應用於不同領域的開發中。但是,在考慮擇業方向的時候,很多人都會有一個問題:PHP和Python哪個好找工作?這篇文章將從多個方…

    編程 2025-04-29
  • Python字元串寬度不限制怎麼打代碼

    本文將為大家詳細介紹Python字元串寬度不限制時如何打代碼的幾個方面。 一、保持代碼風格的統一 在Python字元串寬度不限制的情況下,我們可以寫出很長很長的一行代碼。但是,為了…

    編程 2025-04-29
  • Python基礎代碼用法介紹

    本文將從多個方面對Python基礎代碼進行解析和詳細闡述,力求讓讀者深刻理解Python基礎代碼。通過本文的學習,相信大家對Python的學習和應用會更加輕鬆和高效。 一、變數和數…

    編程 2025-04-29
  • Python滿天星代碼:讓編程變得更加簡單

    本文將從多個方面詳細闡述Python滿天星代碼,為大家介紹它的優點以及如何在編程中使用。無論是剛剛接觸編程還是資深程序員,都能從中獲得一定的收穫。 一、簡介 Python滿天星代碼…

    編程 2025-04-29
  • 倉庫管理系統代碼設計Python

    這篇文章將詳細探討如何設計一個基於Python的倉庫管理系統。 一、基本需求 在著手設計之前,我們首先需要確定倉庫管理系統的基本需求。 我們可以將需求分為以下幾個方面: 1、庫存管…

    編程 2025-04-29
  • 寫代碼新手教程

    本文將從語言選擇、學習方法、編碼規範以及常見問題解答等多個方面,為編程新手提供實用、簡明的教程。 一、語言選擇 作為編程新手,選擇一門編程語言是很關鍵的一步。以下是幾個有代表性的編…

    編程 2025-04-29
  • Python實現簡易心形代碼

    在這個文章中,我們將會介紹如何用Python語言編寫一個非常簡單的代碼來生成一個心形圖案。我們將會從安裝Python開始介紹,逐步深入了解如何實現這一任務。 一、安裝Python …

    編程 2025-04-29
  • 怎麼寫不影響Python運行的長段代碼

    在Python編程的過程中,我們不可避免地需要編寫一些長段代碼,包括函數、類、複雜的控制語句等等。在編寫這些代碼時,我們需要考慮代碼可讀性、易用性以及對Python運行性能的影響。…

    編程 2025-04-29
  • Python愛心代碼動態

    本文將從多個方面詳細闡述Python愛心代碼動態,包括實現基本原理、應用場景、代碼示例等。 一、實現基本原理 Python愛心代碼動態使用turtle模塊實現。在繪製一個心形的基礎…

    編程 2025-04-29

發表回復

登錄後才能評論