国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開發(fā) > PHP > 正文

php通用分頁(yè)類代碼

2024-05-04 23:06:58
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
  1. class dividepage{//分頁(yè)類 
  2.  private $total;//要顯示的總記錄數(shù) 
  3.  private $url;//請(qǐng)求的url地址 
  4.  private $displaypg;//每頁(yè)顯示的記錄數(shù),默認(rèn)為每頁(yè)顯示10條記錄 
  5.  private $page;//當(dāng)前頁(yè)碼 
  6.  private $lastpg;//總頁(yè)數(shù),即最后一頁(yè)的頁(yè)碼 
  7.  private $prepg;//前一頁(yè) 
  8.  private $nextpg;//后一頁(yè) 
  9.  private $firstcount;//記錄條數(shù)開始的序號(hào)從0開始 
  10.  private $startd;//記錄條數(shù)開始的記錄號(hào). 
  11.  private $stopd;//記錄條數(shù)結(jié)束的記錄號(hào). 
  12. //構(gòu)造函數(shù) 
  13. public function __construct($url$total$displaypg){ 
  14.  $this->url = $url;//請(qǐng)求的url 
  15.  $this->total = $total;//總記錄數(shù) 
  16.  //if($displaypg == '') 
  17.  $this->displaypg = $displaypg;//每頁(yè)顯示的記錄數(shù) 
  18.  $this->initdividepage();//初始化分頁(yè)類 
  19.  //echo ','.$this->displaypg; 
  20. //初始化分頁(yè)類 
  21. private function initdividepage(){ 
  22.  //分析url 
  23.  $parse_url = parse_url($this->url);//將url解釋為有固定鍵值對(duì)的數(shù)組 
  24.  $url_query = $parse_url['query'];//取出url中的查詢字符串 
  25.  if($url_query){//如果有查詢字符串,則刪除查詢字串中當(dāng)前頁(yè)的查詢字段如:&page=$page或page=$page 
  26.   ereg('(^|&)page=([0-9]*)'$url_query$k); 
  27.   $this->page = $k[2];//取得當(dāng)前頁(yè)的值 
  28.   $url_query = ereg_replace("(^|&)page=$this->page"''$url_query);//刪除查詢字串中當(dāng)前頁(yè)的查詢字段如:&page=$page或page=$page 
  29.   $this->url = str_replace($parse_url['query'], $url_query$this->url);//保留其他的查詢字串, 
  30.   $this->page = $this->page ? $this->page : 1;//w如果查詢字符串中沒(méi)有當(dāng)前頁(yè)的值就設(shè)當(dāng)前頁(yè)為1 
  31.   if($url_query){//如果有其他查詢字符串,則以&page=$page形式添加翻頁(yè)查詢字串 
  32.    $this->url .= '&page'
  33.   }else{//如果沒(méi)有其他查詢字串,則以page=$page形式添加翻頁(yè)查詢字串 
  34.    $this->url .= 'page'
  35.   } 
  36.  }else{//如果沒(méi)有查詢字串,則在url后添加?page=$page形式的翻頁(yè)查詢字串 
  37.   $this->page = 1; 
  38.   $this->url .= '?page'
  39.  } 
  40.  $this->lastpg = ceil($this->total / $this->displaypg);//計(jì)算總頁(yè)數(shù),即最后一頁(yè)的頁(yè)碼 
  41.     $this->page = min($this->lastpg, $this->page);//如果當(dāng)前頁(yè)大于總頁(yè)數(shù),則當(dāng)前頁(yè)為最后一頁(yè)的頁(yè)碼 
  42.     $this->prepg = $this->page - 1;//上一頁(yè)為當(dāng)前頁(yè)減一www.111cn.net 
  43.     $this->nextpg = $this->page + 1;//(($this->page == $this->lastpg) ? $this->lastpg : ($this->page + 1));//下一頁(yè)為當(dāng)前頁(yè)加一,如果當(dāng)前頁(yè)為最后一頁(yè),則下一頁(yè)為0 
  44.     $this->firstcount = ($this->page - 1) * $this->displaypg;//計(jì)算當(dāng)前頁(yè),記錄條數(shù)開始的記錄號(hào),從0開始. 
  45.  $this->startd = $this->total ? ($this->firstcount + 1) : 0;//記錄開始號(hào)從1開始 
  46.  $this->stopd = min($this->firstcount + $this->displaypg, $this->total);//記錄結(jié)束號(hào) 
  47.  //echo $this->displaypg; 
  48.  //echo $this->nextpg.'+=+='.$this->lastpg; 
  49. public function getpageinfo(){//取得當(dāng)前頁(yè)面的基本信息,如:顯示第 1-10 條記錄,共 23 條記錄。 
  50.  return '<span class="pageinfostyle">顯示第<span class="numstyle">'.$this->startd.'-'.$this->stopd.'</span>條記錄,共<span class="numstyle">'.$this->total.'</span>條記錄。</span>'
  51. public function getcommonpagenav(){//取得通常的分頁(yè)導(dǎo)航,如:首頁(yè) 上一頁(yè) 下一頁(yè) 尾頁(yè) 
  52.  $commonnav = ''
  53.  if($this->lastpg == 1){//如果只有一頁(yè),則返回翻頁(yè)導(dǎo)航,退出,不顯示下一頁(yè),上一頁(yè)等。。。 
  54.   return $commonnav
  55.   break
  56.  } 
  57.  $commonnav = '<a href="'.$this->url.'=1" class="compagestyle">首頁(yè)</a>';//設(shè)置首頁(yè)導(dǎo)航,page=1 
  58.  if($this->prepg){ 
  59.   $commonnav .= '<a href="'.$this->url.'='.$this->prepg.'" class="compagestyle">上一頁(yè)</a>'
  60.  }else
  61.   $commonnav .= '<a class="fcompagestyle">上一頁(yè)</a>'
  62.  } 
  63.  if($this->nextpg <= $this->lastpg){ 
  64.   $commonnav .= '<a href="'.$this->url.'='.$this->nextpg.'" class="compagestyle">下一頁(yè)</a>'
  65.  }else
  66.   $commonnav .= '<a class="fcompagestyle">下一頁(yè)</a>'
  67.  } 
  68.  $commonnav .= '<a href="'.$this->url.'='.$this->lastpg.'" class="compagestyle">尾頁(yè)</a>';//顯示尾頁(yè)鏈接 
  69.  return $commonnav
  70. //取得跳轉(zhuǎn)分頁(yè)導(dǎo)航,如:第n頁(yè) 
  71. public function getjumppagenav(){ 
  72.  //<select name='topage' size='1' onchange='window.location="/test/page.php?page="+this.value'> 
  73.  $jumpnav = '<span class="pageinfostyle">到第<select name="topage" size="1" class="topage"  onchange='window.location="'.$this->url.'="+this.value'>'." "
  74.  for($i = 1; $i <= $this->lastpg; $i++){ 
  75.   if($i == $this->page){//把當(dāng)前頁(yè)的頁(yè)碼作為默認(rèn)選項(xiàng) 
  76.    $jumpnav .= '<option value="'.$i.'" selected>'.$i.'</option>'." "
  77.   }else
  78.    $jumpnav .= '<option value="'.$i.'">'.$i.'</option>'." "
  79.   } 
  80.  } 
  81.  $jumpnav .= '</select>頁(yè),共<span class="numstyle">'.$this->lastpg.'</span>頁(yè)</span>'
  82.  return $jumpnav
  83. //取得所有的分頁(yè)導(dǎo)航 
  84. public function getallpagenav(){ 
  85.  $temp =  $this->getpageinfo().$this->getcommonpagenav().$this->getjumppagenav();//開源代碼Vevb.com 
  86.  return $temp
  87. //取得當(dāng)前頁(yè)需顯示的記錄,在數(shù)據(jù)庫(kù)教程中的限定范圍,如0-9 
  88. public function getlimitstr(){ 
  89.  //echo $this->page; 
  90.  //echo $this->firstcount; 
  91.   
  92.  //echo $this->dispalypg; 
  93.  $temp = $this->firstcount.','.$this->displaypg; 
  94.  //echo $temp; 
  95.  return $temp

使用實(shí)例,代碼如下:

  1. $result=mysql_query("select * from tb_pagetest");//從數(shù)據(jù)庫(kù)中查詢所需顯示的數(shù)據(jù)  
  2. $total=mysql_num_rows($result);//查詢到的數(shù)據(jù)的總條數(shù)  
  3. $pagesize = 5;//每頁(yè)顯示的記錄條數(shù)  
  4. $url = $_server['request_uri'];//請(qǐng)求的uri  
  5.   
  6. $dividepageclass = new dividepage($url$total$pagesize); //創(chuàng)建分頁(yè)類,(類能自動(dòng)初始化)  
  7. $limitstr = $dividepageclass->getlimitstr();//取得當(dāng)前頁(yè)要顯示的記錄開始序號(hào)和每頁(yè)顯示條數(shù),如:0, 5(顯示從0開始的5條記錄)  
  8. echo $dividepageclass->getallpagenav();//顯示所有分頁(yè)導(dǎo)航條,  
  9. 如:顯示第11-13條記錄,共13條記錄。首頁(yè) 上一頁(yè) 下一頁(yè) 尾頁(yè) 到*第 1 頁(yè),共 3 頁(yè)  
  10. $sql = 'select * from tb_pagetest limit '.$limitstr;  
  11. $result=mysql_query($sql);//從數(shù)據(jù)庫(kù)中取得當(dāng)前頁(yè)要顯示的記錄集,然后顯示就ok  
  12. 如:  
  13. while($row=mysql_fetch_array($result))  
  14. echo "<hr><b>".$row[title]." | ".$row[author];

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 特克斯县| 巨野县| 沂源县| 盐源县| 登封市| 阜宁县| 巧家县| 桐乡市| 县级市| 嫩江县| 梁平县| 湛江市| 清远市| 上饶市| 辽源市| 县级市| 青浦区| 东乡族自治县| 郧西县| 徐州市| 拉萨市| 含山县| 泸水县| 乐至县| 邮箱| 肥西县| 益阳市| 田阳县| 保亭| 新竹县| 秀山| 驻马店市| 石狮市| 花垣县| 鄂托克前旗| 邵阳县| 台湾省| 东辽县| 海盐县| 正定县| 临西县|