php自動(dòng)適應(yīng)范圍的分頁(yè)代碼
<?php
function page($page,$total,$phpfile,$pagesize=10,$pagelen=7){
$pagecode = '';//定義變量,存放分頁(yè)生成的HTML
$page = intval($page);//避免非數(shù)字頁(yè)碼
$total = intval($total);//保證總記錄數(shù)值類型正確
if(!$total) return array();//總記錄數(shù)為零返回空數(shù)組
$pages = ceil($total/$pagesize);//計(jì)算總分頁(yè)
//處理頁(yè)碼合法性
if($page<1) $page = 1;
if($page>$pages) $page = $pages;
//計(jì)算查詢偏移量
$offset = $pagesize*($page-1);
//頁(yè)碼范圍計(jì)算
$init = 1;//起始頁(yè)碼數(shù)
$max = $pages;//結(jié)束頁(yè)碼數(shù)
$pagelen = ($pagelen%2)?$pagelen:$pagelen+1;//頁(yè)碼個(gè)數(shù)
$pageoffset = ($pagelen-1)/2;//頁(yè)碼個(gè)數(shù)左右偏移量
//生成html
$pagecode='<div class="page">';
$pagecode.="<span>$page/$pages</span>";//第幾頁(yè),共幾頁(yè)
//如果是第一頁(yè),則不顯示第一頁(yè)和上一頁(yè)的連接
if($page!=1){
$pagecode.="<a href=\"{$phpfile}?page=1\"><<</a>";//第一頁(yè)
$pagecode.="<a href=\"{$phpfile}?page=".($page-1)."\"><</a>";//上一頁(yè)
}
//分頁(yè)數(shù)大于頁(yè)碼個(gè)數(shù)時(shí)可以偏移
if($pages>$pagelen){
//如果當(dāng)前頁(yè)小于等于左偏移
if($page<=$pageoffset){
$init=1;
$max = $pagelen;
}else{//如果當(dāng)前頁(yè)大于左偏移
//如果當(dāng)前頁(yè)碼右偏移超出最大分頁(yè)數(shù)
if($page+$pageoffset>=$pages+1){
$init = $pages-$pagelen+1;
}else{
//左右偏移都存在時(shí)的計(jì)算
$init = $page-$pageoffset;
$max = $page+$pageoffset;
}
}
}
//生成html
for($i=$init;$i<=$max;$i++){
if($i==$page){
$pagecode.='<span>'.$i.'</span>';
} else {
$pagecode.="<a href=\"{$phpfile}?page={$i}\">$i</a>";
}
}
if($page!=$pages){
$pagecode.="<a href=\"{$phpfile}?page=".($page+1)."\">></a>";//下一頁(yè)
$pagecode.="<a href=\"{$phpfile}?page={$pages}\">>></a>";//最后一頁(yè)
}
$pagecode.="<input type=\"text\" size=\"3\" onkeydown=\"if(event.keyCode==13) {window.location.href='{$phpfile}?page='+this.value; return false;}\" /></div>";
return array('pagecode'=>$pagecode,'sqllimit'=>' limit '.$offset.','.$pagesize);
}
?>
加了頁(yè)碼跳轉(zhuǎn)文本框
以下是新手使用說明
<?php
$phpfile = 'index.php';//頁(yè)面文件名
$page= isset($_GET['page'])?$_GET['page']:1;//默認(rèn)頁(yè)碼
$db = mysql_connect('localhost','test','test');//鏈接數(shù)據(jù)庫(kù)
mysql_select_db('test',$db);//選擇數(shù)據(jù)庫(kù)
$counts = mysql_num_rows(mysql_query('select `id` from `test`',$db));//獲取需要的數(shù)據(jù)總條數(shù)
$sql='select `id`,`title` from `test`';//定義查詢語句SQL
$getpageinfo = page($page,$counts,$phpfile);//調(diào)用函數(shù),生成分頁(yè)HTML 和 SQL LIMIT 子句
$sql.=$getpageinfo['sqllimit'];//組合完整的SQL語句
$data = $row = array();//初始化數(shù)組
$result = mysql_query($sql,$db);//獲取結(jié)果集
//將數(shù)據(jù)裝入$data數(shù)組
while($row = mysql_fetch_array($result)){
$data[]=$row;
}
?>
<?php
echo $getpageinfo['pagecode'];//顯示分頁(yè)的html代碼
?>
======================
補(bǔ)貼css
<style type="text/css">
body{font-family:Tahoma;}
.page{padding:2px;font-weight:bolder;font-size:12px;}
.page a{border:1px solid #ccc;padding:0 5px 0 5px;margin:2px;text-decoration:none;color:#333;}
.page span{padding:0 5px 0 5px;margin:2px;background:#09f;color:#fff;border:1px solid #09c;}
</style>
相關(guān)文章
PHP實(shí)現(xiàn)將MySQL重復(fù)ID二維數(shù)組重組為三維數(shù)組的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)將MySQL重復(fù)ID二維數(shù)組重組為三維數(shù)組的方法,涉及php針對(duì)數(shù)組的遍歷與轉(zhuǎn)換操作相關(guān)技巧,需要的朋友可以參考下2016-08-08解析PHP中數(shù)組元素升序、降序以及重新排序的函數(shù)
本篇文章是對(duì)PHP中數(shù)組元素升序、降序以及重新排序的函數(shù)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06使用PHP實(shí)現(xiàn)實(shí)時(shí)數(shù)據(jù)可視化功能的示例詳解
實(shí)時(shí)數(shù)據(jù)可視化功能,是指在Web應(yīng)用程序開發(fā)中,將服務(wù)器發(fā)送的實(shí)時(shí)數(shù)據(jù),本文將介紹如何使用PHP和前端框架實(shí)現(xiàn)實(shí)時(shí)數(shù)據(jù)可視化功能,需要的可以參考一下2023-07-07yii框架源碼分析之創(chuàng)建controller代碼
我們可以看到有時(shí)會(huì)使用protected目錄下的controller,有時(shí)會(huì)使用module中controller,具體是如何處理的呢,請(qǐng)看如下的分析2011-06-06PHP生成隨機(jī)字符串實(shí)例代碼(字母+數(shù)字)
這篇文章主要給大家介紹了關(guān)于PHP生成隨機(jī)字符串(字母+數(shù)字)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用PHP具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09php實(shí)現(xiàn)快速對(duì)二維數(shù)組某一列進(jìn)行組裝的方法小結(jié)
這篇文章主要介紹了php實(shí)現(xiàn)快速對(duì)二維數(shù)組某一列進(jìn)行組裝的方法,涉及PHP數(shù)組遍歷、轉(zhuǎn)換、拆分等相關(guān)操作技巧,需要的朋友可以參考下2019-12-12phpmyadmin安裝時(shí)提示:Warning: require_once(./libraries/common.inc
common.inc.php這個(gè)文件是存在的,而且系統(tǒng)中php也不裝在C盤根目錄下,怎么會(huì)有這樣的提示呢?經(jīng)過一陣調(diào)試終于發(fā)現(xiàn)解決方法2011-08-08