推薦一款PHP+jQuery制作的列表分頁(yè)的功能模塊
做了一個(gè)列表分頁(yè)的功能模塊,主要的文件包括分頁(yè)類 page.class.php 和 控制 ajax 分頁(yè)的ajax.js,主要功能有:
1.可以選擇 3 種常見的 url 分頁(yè)格式;
2.可以選擇 url 分頁(yè) 還是 ajax 分頁(yè);
3.兩種分頁(yè)方式都可以自定義分頁(yè) a 標(biāo)簽的文字;
4.url 分頁(yè)方式可以自定義分頁(yè)偏移量;
5.url 分頁(yè)方式可以選擇手動(dòng)跳轉(zhuǎn)方式:手動(dòng)輸入頁(yè)碼跳轉(zhuǎn) 或 下拉菜單選擇頁(yè)碼跳轉(zhuǎn)。
列表分頁(yè)功能含有但不一定全部包含的元素包括:
首頁(yè)、下一頁(yè)、上一頁(yè)、末頁(yè)、具體頁(yè)碼、手動(dòng)輸入的跳轉(zhuǎn)至第幾頁(yè)、下拉菜單選擇跳轉(zhuǎn)至第幾頁(yè)、信息( 共多少頁(yè)、共多少條、當(dāng)前是第幾頁(yè) )等。
其中必須包含的元素有:上一頁(yè)、下一頁(yè)、具體頁(yè)碼。
先看看其他網(wǎng)站是怎么做的( 百度搜索、虎撲、淘寶、蝦米、織夢(mèng)官網(wǎng) ):
1.百度搜索就是由最簡(jiǎn)單的"上一頁(yè)"、"下一頁(yè)"和具體頁(yè)碼構(gòu)成。分頁(yè)偏移量為前5頁(yè)后4頁(yè)



2.虎撲話題( http://bbs.hupu.com/topic-5)的頁(yè)碼包括了"上一頁(yè)"、"下一頁(yè)"、具體頁(yè)碼、手動(dòng)輸入跳轉(zhuǎn)至第幾頁(yè)、信息等元素,還包括首頁(yè)和末頁(yè),只不過(guò)這里的首頁(yè)和末頁(yè)不是用文字而是用具體頁(yè)碼表現(xiàn)出來(lái)。分頁(yè)偏移量前后都是4頁(yè)。博客園的列表頁(yè)( http://www.cnblogs.com/cate/php/#p12) 是相同的處理方式。




3.淘寶網(wǎng)寶貝列表頁(yè)( http://s.taobao.com/list?spm=a217v.7289245.1997888733.7.4JHYae&seller_type=taobao&sort=sale-desc&cat=50029216&sd=0&tid=0&olu=yes&isnew=2&navid=city&smc=1&_input_charset=utf-8&tab=all&app=list&s=0&auction_tag[]=12034),包含"上一頁(yè)"、"下一頁(yè)"、具體頁(yè)碼、信息、手動(dòng)輸入跳轉(zhuǎn)至第幾頁(yè) ( 還有個(gè)小小的效果,點(diǎn)擊去第幾頁(yè)的輸入框時(shí)會(huì)彈出確定按鈕 ),也包含首頁(yè),只不過(guò)首頁(yè)是用頁(yè)碼1代替。分頁(yè)偏移量前后都是2頁(yè)



4.蝦米列表( http://www.xiami.com/collect/recommend?spm=a1z1s.2943601.6856193.30.dqFWiZ),包含"上一頁(yè)"、"下一頁(yè)"、具體頁(yè)碼、可跳轉(zhuǎn)的省略頁(yè)碼( ... )、信息,也包括以頁(yè)碼1顯示的首頁(yè)。分頁(yè)偏移量為前2頁(yè)后5頁(yè)



最后是織夢(mèng)官網(wǎng)文章列表頁(yè)( http://www.dedecms.com/news/list_133_11.html),包含了"首頁(yè)"、"上一頁(yè)"、"下一頁(yè)"、具體頁(yè)碼、"末頁(yè)"、下拉菜單選擇跳轉(zhuǎn)至第幾頁(yè)、信息。分頁(yè)偏移量前后都是5頁(yè):

瀏覽至第11頁(yè)時(shí)非常遺憾,寬度過(guò)寬導(dǎo)致版式出現(xiàn)問(wèn)題:


這個(gè)分頁(yè)功能的做法和效果是:
1.url 分頁(yè) style1:
①手動(dòng)輸入跳轉(zhuǎn)頁(yè)碼的方式:
始終顯示最后一頁(yè)

"..."跳轉(zhuǎn)至 當(dāng)前顯示的除末頁(yè)的最大頁(yè)碼的下一頁(yè),鼠標(biāo)放在上面給出提示

前后偏移量可自定義,可相同可不同,前面的"..."跳轉(zhuǎn)至最前頁(yè)除首頁(yè)的頁(yè)碼的前一頁(yè)

②下拉菜單選擇跳轉(zhuǎn)的方式:

2.url 分頁(yè) style2:
使用"首頁(yè)"和"末頁(yè)"代替頁(yè)碼"1"和最后一頁(yè)頁(yè)碼,使用前n頁(yè)、后n頁(yè)代替"..."

為了使"前10頁(yè)"和"后10頁(yè)"同時(shí)出現(xiàn),增加了數(shù)據(jù)庫(kù)的數(shù)據(jù)

同樣有下拉菜單跳轉(zhuǎn)方式

3.ajax 分頁(yè):
出現(xiàn)的元素只有"首頁(yè)"、"上一頁(yè)"、"下一頁(yè)"和"末頁(yè)"。
首頁(yè)時(shí):

中間時(shí):

末頁(yè)時(shí):

模塊的文件結(jié)構(gòu)圖:
ROOT:
├─conn
│ └─conn.php
│
├─libs -- smarty庫(kù)
│
├─templates
│ │
│ ├─demo.html -- 功能頁(yè)模板文件
│ │
│ ├─css
│ │ ├─common.css
│ │ └─style1.css
│ │
│ ├─images
│ │ └─loading.gif -- ajax分頁(yè)時(shí)請(qǐng)求數(shù)據(jù)接收到之前的加載圖
│ └─js
│ ├─jquery-1.8.3.min.js
│ └─ajax.js -- 當(dāng)分頁(yè)方式為ajax時(shí)模板demo.html加載的js
│
├─templates_c
│
├─init.inc.php -- smarty配置文件
│
├─page.class.php -- 分頁(yè)類
│
├─demo.php
│
└─ajaxpage.php -- ajax分頁(yè)時(shí)接受請(qǐng)求的php文件
要注意的地方:
1.偏移量的顯示設(shè)置,主要是什么時(shí)候 url 分頁(yè)方式1,什么時(shí)候顯示"..." :當(dāng)前頁(yè)碼 - 前偏移量 - 1 > 1 時(shí),應(yīng)當(dāng)顯示前面的"..."; 當(dāng)前頁(yè)碼 + 后偏移量 + 1 < 總頁(yè)數(shù)時(shí),應(yīng)當(dāng)顯示后面的"...";
2.選擇性加載 js :當(dāng)使用 ajax 方式進(jìn)行分頁(yè)時(shí),才加載 ajax.js
3.外部的 js 無(wú)法解析 smarty 的標(biāo)簽,所以在使用外部 js 時(shí)的傳值要進(jìn)行處理
4.ajax 分頁(yè)時(shí),默認(rèn)是第一頁(yè),也就是一定首先會(huì)出現(xiàn) "下一頁(yè)" 和 "末頁(yè)",所以 "上一頁(yè)" 和 "首頁(yè)" 的添加和點(diǎn)擊函數(shù)應(yīng)當(dāng)包含在"下一頁(yè)" 和 "末頁(yè)" 的點(diǎn)擊函數(shù)中。
主要代碼:
page.class.php:
<?php
class MyPageUrl{
private $totalNum;
private $perpageNum; //每頁(yè)顯示條數(shù)
private $pageNow; //當(dāng)前頁(yè)頁(yè)碼
private $url; //當(dāng)前url
//頁(yè)碼顯示
private $pageStyle; //頁(yè)碼樣式,提供2種樣式
private $prePage; //頁(yè)碼前偏移量
private $floPage; //頁(yè)碼后偏移量
private $skipStyle; //手動(dòng)跳轉(zhuǎn),0為手動(dòng)輸入頁(yè)碼,1為下拉菜單選擇頁(yè)碼
private $page_act; //翻頁(yè)樣式 0:url 1:ajax
//頁(yè)碼文字
//style2&style3
private $firstFonts = "首頁(yè)";
private $lastFonts = "末頁(yè)";
private $nextFonts = "下一頁(yè) >";
private $preFonts = "< 上一頁(yè)";
//前n頁(yè),后n頁(yè)
private $page_n;
private $pn = 10;
private $pn_fonts = "前10頁(yè)";
private $fn = 10;
private $fn_fonts = "后10頁(yè)";
//展現(xiàn)的頁(yè)碼
private $pageShow;
//構(gòu)造函數(shù)
function __construct($totalNum,$perpageNum,$prePage,$preFonts,$floPage,$nextFonts,$p,$skipStyle,$pageStyle,$page_n,$page_act){
$this->totalNum = $totalNum;
$this->perpageNum = $perpageNum;
$this->prePage = $prePage;
$this->floPage = $floPage;
$this->skipStyle = $skipStyle;
$this->pageStyle = $pageStyle;
$this->page_n = $page_n;
$this->page_act = $page_act;
$this->getPageNow($p);
$this->totalPage = $this->getTotalPage(); //總頁(yè)數(shù)
$this->pageShow = "";
$this->getUrl();
}
/************定義__toString方法,把對(duì)象解析成字符串******/
public function __toString(){
return $this->pageShow;
}
/************獲得當(dāng)前頁(yè)頁(yè)碼,$p用來(lái)接收$_GET['p']*******/
public function getPageNow($p){
if(!isset($p)){
$this->pageNow = 1;
}else if($p>0){
$this->pageNow = $p;
}else{
die("page number error");
}
return $this->pageNow;
}
/***********************設(shè)置當(dāng)前頁(yè)面鏈接***************/
public function getUrl(){
$url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
//判斷是否帶參數(shù)
if(strpos($url,"?") === false){ //不帶參數(shù)
return $this->url = $url."?";
}else{ //帶參數(shù)
$url = explode("?",$url);
//參數(shù)
$param = $url[1];
//判斷是否有多個(gè)參數(shù)
if(strpos($param,"&") === false){ //只有一個(gè)參數(shù)
//判斷參數(shù)是否為p
if(strpos($param,"p=") === false){ //不含參數(shù)p
//合并url
$url = implode("?",$url);
return $this->url = $url."&";
}else{
//把參數(shù)p去掉
$url = $url[0];
return $this->url = $url."?";
}
}else{ //多個(gè)參數(shù)
$param = explode("&",$param);
//遍歷參數(shù)數(shù)組
foreach($param as $k=>$v){
if(strpos($v,"p=") === false){
continue;
}else{
//當(dāng)含有參數(shù)p時(shí),把它從數(shù)組中刪除
unset($param[$k]);
}
}
//刪除參數(shù)p之后組合數(shù)組
$param = implode("&",$param);
$url[1] = $param;
$url = implode("?",$url);
return $this->url = $url."&";
}
}
}
/************************前偏移量處理********************/
public function preOffset($preFonts){
//前偏移量的處理
if($this->pageNow!=1 && ($this->pageNow - $this->prePage -1 <= 1)){
//上一頁(yè)
$this->pageShow .= "<a id=\"per_page\" class=\"pagenum\" href=\"".$this->url."p=".($this->pageNow-1)."\">".($preFonts == ""?$this->preFonts:$preFonts)."</a>";
//頁(yè)碼
for($i=1;$i<=$this->pageNow-1;$i++){
//ajax方式不顯示
if($this->page_act != 1){
$this->pageShow .= "<a class=\"pagenum\" href=\"".$this->url."p=".$i."\">".$i."</a>";
}
}
}else if($this->pageNow - $this->prePage -1 > 1){ //pageNow至少大于2時(shí)才會(huì)出現(xiàn)"1..."
//樣式1.加上'首頁(yè)'
if($this->pageStyle == 2 || $this->page_act == 1){
//首頁(yè)
$this->pageShow .= "<a id=\"first_page\" class=\"pagenum\" href=\"".$this->url."p=1\">".$this->firstFonts."</a>";
//style2.前n頁(yè)
if($this->page_n == 1 && $this->page_act != 1){
if($this->pageNow>$this->pn){
$this->pageShow .= "<a id=\"pre_n_page\" class=\"pagenum\" href=\"".$this->url."p=".($this->pageNow-$this->pn)."\">".$this->pn_fonts."</a>";
}
}
}
//上一頁(yè)
$this->pageShow .= "<a id=\"pre_page\" class=\"pagenum\" href=\"".$this->url."p=".($this->pageNow-1)."\">".($preFonts == ""?$this->preFonts:$preFonts)."</a>";
//樣式1.加上第一頁(yè)'1'、'...'
if($this->pageStyle == 1){
$this->pageShow .= "<a class=\"pagenum\" href=\"".$this->url."\">1</a><a id=\"pre_page_2\" class=\"pagenum\" href=\"".$this->url."p=".($this->pageNow-$this->prePage-1)." \" title=\"第".($this->pageNow-$this->prePage-1)."頁(yè)\">…</a>";
}
for($i=$this->prePage;$i>=1;$i--){
//當(dāng)前頁(yè)和'...'之間的頁(yè)碼,ajax方式不顯示
if($this->page_act != 1){
$this->pageShow .= "<a class=\"pagenum\" href=\"".$this->url."p=".($this->pageNow-$i)."\">".($this->pageNow-$i)."</a>";
}
}
}
}
/**********************頁(yè)碼和后偏移量處理***************************/
public function floOffset($nextFonts){
if($this->totalPage > $this->floPage){ //總頁(yè)數(shù)大于后偏移量時(shí)
for($i=0;$i<=$this->floPage;$i++){
$page = $this->pageNow+$i;
if($page<=$this->totalPage){
//頁(yè)碼,ajax方式不顯示
if($this->page_act != 1){
$this->pageShow .= "<a class=\"pagenum\" href=\"".$this->url."p=".$page."\">".$page."</a>";
}
}
}
if($this->pageNow < $this->totalPage){
//當(dāng)前頁(yè)+后偏移量+1小于總頁(yè)數(shù)時(shí)出現(xiàn)"..."
if(($this->pageNow+$this->floPage+1)<$this->totalPage){
//樣式1.顯示'...'
if($this->pageStyle == 1){
$this->pageShow .= "<a id=\"flo_page_2\" class=\"pagenum\" href=\"".$this->url."p=".($page+1)."\" title=\"第".($page+1)."頁(yè)\">…</a>";
}
}
//當(dāng)前頁(yè)+后偏移量+1小于等于總頁(yè)數(shù)時(shí)出現(xiàn)最后一頁(yè)的快捷標(biāo)簽
if(($this->pageNow+$this->floPage+1)<=$this->totalPage){
//最后一頁(yè)
//樣式1.始終出現(xiàn)'最后一頁(yè)頁(yè)碼'
if($this->pageStyle == 1){
$this->pageShow .= "<a class=\"pagenum\" href=\"".$this->url."p=".$this->totalPage."\" title=\"總共".$this->totalPage."頁(yè)\">".$this->totalPage."</a>";
}
}
$this->pageShow .= "<a id=\"flo_page\" class=\"pagenum\" href=\"".$this->url."p=".($this->pageNow+1)."\">".($nextFonts == ""?$this->nextFonts:$nextFonts)."</a>"; //當(dāng)實(shí)例化對(duì)象時(shí)用戶傳遞的文字為空時(shí)則調(diào)用類預(yù)設(shè)的"下一頁(yè)",否則輸出用戶傳遞的值
//style2.加上后n頁(yè)
if($this->pageStyle == 2 && $this->page_n == 1 && $this->page_act != 1){
if(($this->pageNow+10)<$this->totalPage){
$this->pageShow .= "<a id=\"flo_n_page\" class=\"pagenum\" href=\"".$this->url."p=".($this->pageNow+$this->fn)."\">".$this->fn_fonts."</a>";
}
}
//顯示'末頁(yè)'
if($this->pageStyle == 2){
if(($this->pageNow+$this->floPage+1)<$this->totalPage){
$this->pageShow .= "<a id=\"last_page\" class=\"pagenum\" href=\"".$this->url."p=".$this->totalPage."\">末頁(yè)</a>";
}
}
}else if($this->pageNow > $this->totalPage){
die("超出頁(yè)碼范圍");
}
}else{ //總頁(yè)數(shù)小于后偏移量時(shí)
for($i=0;$i<$this->totalPage;$i++){
$page = $this->pageNow+$i;
//頁(yè)碼后邊界
$this->pageShow .= "<a class=\"pagenum\" href=\"".$this->url."p=".$page."\">".$page."</a>";
}
}
}
/********************其它頁(yè)面信息***********************/
public function getOtherInfo(){
//ajax方式不顯示手動(dòng)跳轉(zhuǎn)信息
if($this->page_act != 1){
$this->pageShow .= " 跳轉(zhuǎn)至 ";
//跳轉(zhuǎn)類型
if($this->skipStyle =="" ){ //不加跳轉(zhuǎn)
$this->pageShow .= "";
}else if($this->skipStyle == 1){ //輸入框
$this->pageShow .= "<input id=\"skip\" type=\"text\" value=\"".$this->pageNow."\">";
$this->pageShow .= "<button id=\"go\">GO</button>";
}else if($this->skipStyle == 2){ //下拉菜單
//選擇下拉菜單自動(dòng)跳轉(zhuǎn)
$this->pageShow .= "<select id=\"select_page\" onchange=\"location.href=this.options[this.selectedIndex].value;\" >";
for($i=1;$i<=$this->totalPage;$i++){
$this->pageShow .= "<option value=\"".$this->url."p=".$i."\"";
//下拉菜單默認(rèn)顯示當(dāng)前頁(yè)
if($i == $this->pageNow){
$this->pageShow .= " selected";
}
$this->pageShow .= ">".$i."</option>";
}
$this->pageShow .= "</select>";
}
}
$this->pageShow .= "<span id=\"pagenow_info\"> 當(dāng)前第".$this->pageNow."頁(yè)</span>";
$this->pageShow .= "/<span id=\"totalpage_info\">共".$this->totalPage."頁(yè)</span>";
$this->pageShow .= " <span id=\"totalNum_info\">共".$this->totalNum."條</span>";
}
/*****************獲取每頁(yè)第一條信息*****************/
public function getFirstRow(){
$firstRow = $this->perpageNum * ($this->pageNow-1) + 1;//當(dāng)前頁(yè)第一條是總條數(shù)中第幾條
return $firstRow;
}
/********************獲得總頁(yè)數(shù)***********************/
public function getTotalPage(){
$totalPage = ceil($this->totalNum / $this->perpageNum);
return $totalPage;
}
/* ****************獲取上一頁(yè)、下一頁(yè)文字*************** */
public function getPreFonts($preFonts){
return ($preFonts=="")?$this->preFonts:$preFonts;
}
public function getNextFonts($nextFonts){
return ($nextFonts=="")?$this->nextFonts:$nextFonts;
}
}
demo.php:
<?php
require 'init.inc.php';
require 'page.class.php';
require 'conn/conn.php';
$perpageNum = 10; //每頁(yè)數(shù)據(jù)條數(shù)
$perPage = 4; //前分頁(yè)偏移量
$floPage = 4; //后分頁(yè)偏移量
$preFonts = ""; //"前一頁(yè)"文字內(nèi)容
$nextFonts = ""; //"下一頁(yè)"文字內(nèi)容
$page_n = 1; //樣式2下是否加"前n頁(yè)"、后n頁(yè),0為不加,1為加
$skipStyle = 1; //跳轉(zhuǎn)類型,可選1、2
$pageStyle = 1; //樣式類型,可選1、2、3( 樣式3只包含"上一頁(yè)"、"下一頁(yè)"和頁(yè)碼 )
$page_act = 0; //0:url 和 1:ajax
if($page_act == 1){
//ajax方式分頁(yè)時(shí)強(qiáng)制使用第二種樣式
$pageStyle = 2;
}
$p = isset($_GET['p'])?$_GET['p']:1; //當(dāng)前頁(yè)碼
//在page.class.php中定義__toString方法,把對(duì)象$mypage解析成字符串輸出
//參數(shù)分別是:總條數(shù)、每頁(yè)條數(shù)、前偏移量、"上一頁(yè)"文字內(nèi)容(默認(rèn)為""時(shí)顯示"上一頁(yè)")、后偏移量、"下一頁(yè)"文字內(nèi)容(默認(rèn)為""時(shí)顯示"下一頁(yè)")、當(dāng)前地址欄頁(yè)碼數(shù)、手動(dòng)跳轉(zhuǎn)樣式、頁(yè)碼顯示樣式、樣式2是否加前n頁(yè)后n頁(yè)、分頁(yè)方式(url/ajax)
//獲得總條數(shù)
//輸出列表
$sql_all = "select title from ips_archives";
//總條數(shù)
$totalNum = $conne->getRowsNum($sql_all);
//實(shí)例化
$mypageurl = new MyPageUrl($totalNum,$perpageNum,$perPage,$preFonts,$floPage,$nextFonts,$p,$skipStyle,$pageStyle,$page_n,$page_act);
//每頁(yè)第一條
$firstRow = $mypageurl->getFirstRow();
//總條數(shù)
$totalPage = $mypageurl->getTotalPage();
//輸出列表
$sql = "select title from ips_archives order by pubdate desc limit ".$firstRow.",".$perpageNum;
//取出數(shù)據(jù)(二維數(shù)組)
$rowsArray = $conne->getRowsArray($sql);
//顯示頁(yè)碼
$pageShow = $mypageurl->preOffset($preFonts).$mypageurl->floOffset($nextFonts).$mypageurl->getOtherInfo();
$smarty->assign("Template_Dir",Template_Dir);
$smarty->assign("page_act",$page_act); //傳遞分頁(yè)方式
$smarty->assign("pageNow",$p); //傳遞當(dāng)前頁(yè)
$smarty->assign("perpageNum",$perpageNum); //傳遞每頁(yè)幾條數(shù)據(jù)
$smarty->assign("totalPage",$totalPage); //傳遞總頁(yè)數(shù)
$smarty->assign("preFonts",$mypageurl->getPreFonts($preFonts)); //傳遞上一頁(yè)文字信息
$smarty->assign("rowsArray",$rowsArray);
$smarty->assign("mypage",$mypageurl);
$smarty->display("demo.html");
使用方法在demo.php的注釋里
demo.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>PHP分頁(yè)類</title>
<link href="<{$Template_Dir}>/css/common.css" rel="stylesheet" type="text/css">
<link href="<{$Template_Dir}>/css/style1.css" rel="stylesheet" type="text/css">
<script id="jq" src="<{$Template_Dir}>/js/jquery-1.8.3.min.js"></script>
</head>
<body>
<div id="list">
<ul id="newsul">
<{foreach $rowsArray as $val}>
<li><{$val['title']}></li>
<{/foreach}>
</ul>
</div>
<div id="page"><{$mypage}></div>
<input id="pageNow" type="hidden" value="<{$pageNow}>">
<!--分頁(yè)方式-->
<input id="page_act" type="hidden" value="<{$page_act}>">
<!--每頁(yè)幾條數(shù)據(jù)-->
<input id="perpageNum" type="hidden" value="<{$perpageNum}>">
<!--總頁(yè)數(shù)-->
<input id="totalPage" type="hidden" value="<{$totalPage}>">
<!--//把smarty的變量傳遞給外部js-->
<input id="Template_Dir" type="hidden" value="<{$Template_Dir}>">
<input id="preFonts" type="hidden" value="<{$preFonts}>">
</body>
<script>
$(function(){
//遍歷a
$(".pagenum").each(function(){
if($(this).text() == $("#pageNow").val()){
$(this).addClass("selected");
}
});
//如果存在跳轉(zhuǎn)輸入框
if($("#skip").length>0){
$("#skip").keydown(function(){
if(event.keyCode == 13){ //回車
self.location="demo.php?p="+$(this).val();
}
});
}
//點(diǎn)擊"GO"按鈕跳轉(zhuǎn)
if($("#go").length>0){
$("#go").click(function(){
self.location="demo.php?p="+$("#skip").val();
});
}
//如果分頁(yè)方式是ajax,則加載外部ajax.js
if($("#page_act").val() == 1){
//把smarty的變量傳遞給外部js
$Template_Dir = $("#Template_Dir").val();
$preFonts = $("#preFonts").val();
$insertAjax = $("<script src=\"<{$Template_Dir}>/js/ajax.js\"><\/script>");
$insertAjax.insertAfter($("#jq"));
}
//最后一行row去掉border-bottom
$("#list ul").children("li:last").css("border-bottom",0);
});
</script>
</html>
ajaxpage.php:
<?php
require 'conn/conn.php';
if(isset($_POST['pageNow']) && !empty($_POST['pageNow'])){
$pageNow = $_POST['pageNow'];
}
//每頁(yè)幾條數(shù)據(jù)
if(isset($_POST['perpageNum']) && !empty($_POST['perpageNum'])){
$perpageNum = $_POST['perpageNum'];
}
//當(dāng)前頁(yè)第一條數(shù)據(jù)
$firstRow = $perpageNum * ($pageNow-1) + 1;
$sql = "select title from ips_archives order by pubdate desc limit ".$firstRow.",".$perpageNum;
$rowsArray = $conne->getRowsArray($sql);
//把二維數(shù)組轉(zhuǎn)換成json格式
echo json_encode($rowsArray);
ajax.js:
//刪除原先的li,插入gif
function ajaxpre(){
//刪除原先的title
$("#newsul li").remove();
//插入gif圖
$loading = $("<img class=\"loading\" src=\""+$Template_Dir+"/images/loading.gif\">");
$loading.appendTo($("#newsul"));
}
//隱藏翻頁(yè)信息
function infoAct(){
//當(dāng)前頁(yè)到達(dá)尾頁(yè)時(shí),"下一頁(yè)"和"末頁(yè)"
if(parseInt($("#pageNow").val()) == parseInt($("#totalPage").val())){
$("#flo_page").hide();
$("#last_page").hide();
$("#pre_page").show();
$("#first_page").show();
}else if(parseInt($("#pageNow").val()) == 1){ //當(dāng)前頁(yè)到達(dá)時(shí)隱藏"首頁(yè)"和"上一頁(yè)"
$("#pre_page").hide();
$("#first_page").hide();
$("#flo_page").show();
$("#last_page").show();
}else{
if($("#pre_page").is(":hidden") || $("#pre_page").length == 0){
$("#pre_page").show();
}
if($("#first_page").is(":hidden") || $("#first_page").length == 0){
$("#first_page").show();
}
if($("#flo_page").is(":hidden") || $("#flo_page") == 0){
$("#flo_page").show();
}
if($("#last_page").is(":hidden") || $("#last_page").length == 0){
$("#last_page").show();
}
}
}
//點(diǎn)擊"下一頁(yè)"、"末頁(yè)"時(shí)出現(xiàn)"首頁(yè)"和"上一頁(yè)"
function showPage(){
//首頁(yè)
$firstPage = $("<a id=\"first_page\" class=\"pagenum\">首頁(yè)</a>");
if($("#first_page").length == 0){
$firstPage.insertBefore($("#flo_page"));
}
//上一頁(yè)
$pre_page = $("<a id=\"pre_page\" class=\"pagenum\">"+$preFonts+"</a>");
if($("#pre_page").length == 0){
$pre_page.insertBefore($("#flo_page"));
}
}
//ajax請(qǐng)求數(shù)據(jù)
function ajaxpost(){
$.post("ajaxpage.php",{
pageNow : parseInt($("#pageNow").val()),
perpageNum : parseInt($("#perpageNum").val())
},function(data,textStatus){
//接收json數(shù)據(jù)
var dataObj=eval("("+data+")"); //轉(zhuǎn)換為json對(duì)象
//刪除gif
$(".loading").remove();
$.each(dataObj,function(idx,item){
$li_new = $("<li>"+item.title+"</li>");
$li_new.appendTo($("#newsul"));
})
$("#list ul").children("li:last").css("border-bottom",0);
});
}
//初始值=1
apagenow = parseInt($("#pageNow").val());
//ajax "首頁(yè)" 因?yàn)?首頁(yè)"和"上一頁(yè)"一開始是不出現(xiàn)的,所以只有在"下一頁(yè)"和"末頁(yè)"的的點(diǎn)擊函數(shù)中調(diào)用"首頁(yè)"和"上一頁(yè)"函數(shù)
function firstPageAct(){
if($("#first_page").is(":visible")){
$("#first_page").click(function(){
//刪除更新前的
ajaxpre();
//pageNow設(shè)為1
$("#pageNow").val(1);
apagenow = parseInt($("#pageNow").val());
//修改頁(yè)碼信息
$("#pagenow_info").html(" 當(dāng)前第1頁(yè)");
//ajax請(qǐng)求數(shù)據(jù)
ajaxpost();
//到達(dá)"首頁(yè)"之后隱藏"首頁(yè)"和"上一頁(yè)"
infoAct();
});
}
}
//ajax "上一頁(yè)"
function prePageAct(){
if($("#pre_page").is(":visible")){
$("#pre_page").click(function(){
//刪除更新前的
ajaxpre();
//每點(diǎn)擊"下一次",隱藏域值-1
if(parseInt(apagenow) != 1){
apagenow = parseInt(apagenow) - parseInt(1);
}
$("#pageNow").val(apagenow);
//隱藏域的頁(yè)碼值大于1時(shí)
if(parseInt($("#pageNow").val()) > parseInt(1)){
//修改頁(yè)碼信息
$("#pagenow_info").html(" 當(dāng)前第"+$("#pageNow").val()+"頁(yè)");
}
//ajax請(qǐng)求數(shù)據(jù)
ajaxpost();
//第一頁(yè)時(shí)隱藏"首頁(yè)"和"下一頁(yè)"
infoAct();
});
}
}
//ajax "下一頁(yè)"
if($("#flo_page").length>0){
//去掉a的href屬性
$("#flo_page").removeAttr("href");
$("#flo_page").click(function(){
ajaxpre();
//每點(diǎn)擊"下一次",隱藏域值+1
apagenow = parseInt(apagenow) + parseInt(1);
$("#pageNow").val(apagenow);
//隱藏域的頁(yè)碼值小于總頁(yè)碼時(shí)
if(parseInt($("#pageNow").val()) <= parseInt($("#totalPage").val())){
//修改頁(yè)碼信息
$("#pagenow_info").html(" 當(dāng)前第"+$("#pageNow").val()+"頁(yè)");
//ajax請(qǐng)求數(shù)據(jù)
ajaxpost();
}
//點(diǎn)擊"下一頁(yè)"之后出現(xiàn)"首頁(yè)"
if($("#first_page").is(":hidden") || $("#first_page").length == 0){
//出現(xiàn)"首頁(yè)"和"下一頁(yè)"
showPage();
firstPageAct();
prePageAct();
}
//隱藏"下一頁(yè)"和"末頁(yè)"
infoAct();
return false; //取消點(diǎn)擊翻頁(yè)
});
}
//ajax "末頁(yè)"
if($("#last_page").length>0){
//去掉a的href屬性
$("#last_page").removeAttr("href");
$("#last_page").click(function(){
ajaxpre();
//修改隱藏域當(dāng)前頁(yè)信息
apagenow = parseInt($("#totalPage").val());
$("#pageNow").val(apagenow);
//修改頁(yè)碼信息
$("#pagenow_info").html(" 當(dāng)前第"+$("#totalPage").val()+"頁(yè)");
//ajax請(qǐng)求數(shù)據(jù)
ajaxpost();
//點(diǎn)擊"末頁(yè)"之后出現(xiàn)"首頁(yè)"
if($("#first_page").length == 0){
showPage();
firstPageAct();
prePageAct();
}
infoAct();
return false;
});
}
//取消a標(biāo)簽跳轉(zhuǎn)
$("#first_page").click(function(){
return false;
});
$("#pre_page").click(function(){
return false;
});
common.css:
a{ font-size:14px; font-family:Tahoma; color:#444; text-decoration:none; cursor:pointer;}
ul{ list-style-type:none;}
/* ***************************列表樣式******************** */
#list{
width:1000px;
margin-bottom:20px;
border:1px solid #95071b;
}
#list ul{
margin:5px 20px;
padding:0px;
}
#list li{
line-height:24px;
border-bottom:1px solid #95071b;
}
/* ****************************跳轉(zhuǎn)樣式******************** */
#skip{
width:36px;
text-align:center;
}
/* ****************************ajax************************* */
.loading{
position:absolute;
top:20%;
left:35%;
}
style1.css:
#page a.pagenum{
margin-left:3px;
margin-right:3px;
padding:1px 7px;
border:1px solid #ccc;
}
#page a.pagenum:hover{
background-color:#95071b;
color:#fff;
}
.selected{
background-color:#95071b;
color:#fff;
}
init.inc.php:
<?php
/**
file:init.inc.php Smarty對(duì)象的實(shí)例化及初始化文件
*/
/* *********************Smarty設(shè)置*********************** */
//根目錄路徑方式,用于Smarty設(shè)置
define("ROOT",str_replace("\\","/",dirname(__FILE__))."/");
require ROOT.'libs/Smarty.class.php';
$smarty = new Smarty();
//Smarty3設(shè)置默認(rèn)路徑
$smarty ->setTemplateDir(ROOT.'templates/')
->setCompileDir(ROOT.'templates_c/')
->setPluginsDir(ROOT.'plugins/')
->setCacheDir(ROOT.'cache/')
->setConfigDir(ROOT.'configs');
$smarty->caching = false;
$smarty->cache_lifetime = 60*60*24; //模版緩存有效時(shí)間為1天
$smarty->left_delimiter = '<{';
$smarty->right_delimiter = '}>';
/***********************************************************/
//根目錄url方式
$PHP_SELF=$_SERVER['PHP_SELF'];
$ROOT_URL='http://'.$_SERVER['HTTP_HOST'].substr($PHP_SELF,0,strrpos($PHP_SELF,'/')+1);
define(ROOT_URL,$ROOT_URL);
//模版目錄url方式
define("Template_Dir",$ROOT_URL.'templates');
代碼下載地址:https://github.com/dee0912/PageClass
- php jquery 實(shí)現(xiàn)新聞標(biāo)簽分類與無(wú)刷新分頁(yè)
- FCKEditor+jQuery+PHP實(shí)現(xiàn)分頁(yè)代碼
- 使用PHP+JQuery+Ajax分頁(yè)的實(shí)現(xiàn)
- ThinkPHP分頁(yè)實(shí)例
- 仿dedecms下拉分頁(yè)樣式修改的thinkphp分頁(yè)類實(shí)例
- ThinkPHP有變量的where條件分頁(yè)實(shí)例
- PHP分頁(yè)類集錦
- php 使用array函數(shù)實(shí)現(xiàn)分頁(yè)
- php簡(jiǎn)單分頁(yè)類實(shí)現(xiàn)方法
- PHP讀取txt文本文件并分頁(yè)顯示的方法
- 分享php分頁(yè)的功能模塊
- PHP實(shí)現(xiàn)簡(jiǎn)單數(shù)字分頁(yè)效果
- 分享PHP函數(shù)實(shí)現(xiàn)數(shù)字與文字分頁(yè)代碼
- PHP實(shí)現(xiàn)仿Google分頁(yè)效果的分頁(yè)函數(shù)
- jQuery+Ajax+PHP+Mysql實(shí)現(xiàn)分頁(yè)顯示數(shù)據(jù)實(shí)例講解
- PHP+jQuery+Ajax實(shí)現(xiàn)分頁(yè)效果 jPaginate插件的應(yīng)用
相關(guān)文章
PHP 文本文章分頁(yè)代碼 按標(biāo)記或長(zhǎng)度(不涉及數(shù)據(jù)庫(kù))
PHP文本分頁(yè),按標(biāo)記或者長(zhǎng)度分頁(yè),非傳統(tǒng)的數(shù)據(jù)庫(kù)分頁(yè)。廢話不多說(shuō),客觀如有意可直接看代碼2012-06-06
Zend Framework實(shí)現(xiàn)具有基本功能的留言本(附demo源碼下載)
這篇文章主要介紹了Zend Framework實(shí)現(xiàn)具有基本功能的留言本,結(jié)合實(shí)例形式較為詳細(xì)的分析了Zend Framework實(shí)現(xiàn)留言本的具體步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-03-03
Yii數(shù)據(jù)模型中rules類驗(yàn)證器用法分析
這篇文章主要介紹了Yii數(shù)據(jù)模型中rules類驗(yàn)證器用法,結(jié)合實(shí)例形式分析了rules類驗(yàn)證器的簡(jiǎn)單使用方法,代碼注釋中包含了相關(guān)方法的使用說(shuō)明,需要的朋友可以參考下2016-07-07
smarty模板引擎從php中獲取數(shù)據(jù)的方法
這篇文章主要介紹了smarty模板引擎從php中獲取數(shù)據(jù)的方法,涉及smarty變量與php代碼的混編技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-01-01
thinkPHP+mysql+ajax實(shí)現(xiàn)的仿百度一下即時(shí)搜索效果詳解
這篇文章主要介紹了thinkPHP+mysql+ajax實(shí)現(xiàn)的仿百度一下即時(shí)搜索效果,結(jié)合完整實(shí)例形式詳細(xì)分析了thinkPHP+mysql+ajax實(shí)現(xiàn)的仿百度一下即時(shí)搜索效果具體數(shù)據(jù)表、控制器、前臺(tái)視圖與樣式相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-07-07
Yii2實(shí)現(xiàn)ActiveForm ajax提交
這篇文章主要 為大家詳細(xì)介紹了Yii2實(shí)現(xiàn)ActiveForm ajax提交的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05

