PHP   发布时间:2022-04-09  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了推荐一款PHP+jQuery制作的列表分页的功能模块大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

做了一个列表分页功能模块,主要的文件包括分页类 page.class.PHP 和 控制 ajax 分页的ajax.js,主要功能有:

1.可以选择 3 种常见的 url 分页格式;

2.可以选择 url 分页 还是 ajax 分页

3.两种分页方式都可以自定义分页 a 标签文字

4.url 分页方式可以自定义分页偏移量;

5.url 分页方式可以选择手动跳转方式:手动输入页码跳转 或 下拉菜单选择页码跳转

列表分页功能含有但不一定全部包含的元素包括

首页下一页上一页、末页、具体页码、手动输入的跳转至第几页、下拉菜单选择跳转至第几页、信息( 共多少页、共多少条、当前是第几页 )等。

其中必须包含的元素有:上一页下一页、具体页码

先看看其他网站是怎么做的( 百度搜索、虎扑、淘宝、虾米、织梦官网 ):

1.百度搜索就是由最简单的"上一页"、"下一页"和具体页码构成。分页偏移量为前5页后4页

推荐一款PHP+jQuery制作的列表分页的功能模块

推荐一款PHP+jQuery制作的列表分页的功能模块

推荐一款PHP+jQuery制作的列表分页的功能模块

2.虎扑话题( http://bbs.hupu.com/topic-5)的页码包括了"上一页"、"下一页"、具体页码、手动输入跳转至第几页、信息等元素,还包括首页和末页,只不过这里的首页和末页不是用文字而是用具体页码表现出来。分页偏移量前后都是4页。博客园的列表页( http://www.cnblogs.com/cate/PHP/#p12) 是相同的处理方式。

推荐一款PHP+jQuery制作的列表分页的功能模块

推荐一款PHP+jQuery制作的列表分页的功能模块

推荐一款PHP+jQuery制作的列表分页的功能模块

推荐一款PHP+jQuery制作的列表分页的功能模块

3.淘宝网宝贝列表页( 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),包含"上一页"、"下一页"、具体页码、信息、手动输入跳转至第几页 ( 还有个小小的效果,点击去第几页的输入框时会弹出确定按钮 ),也包含首页,只不过首页是用页码1代替。分页偏移量前后都是2页

推荐一款PHP+jQuery制作的列表分页的功能模块

推荐一款PHP+jQuery制作的列表分页的功能模块

推荐一款PHP+jQuery制作的列表分页的功能模块

4.虾米列表( http://www.xiami.com/collect/recommend?spm=a1z1s.2943601.6856193.30.dqFWiZ),包含"上一页"、"下一页"、具体页码、可跳转的省略页码( ... )、信息,也包括页码1显示首页分页偏移量为前2页后5页

推荐一款PHP+jQuery制作的列表分页的功能模块

推荐一款PHP+jQuery制作的列表分页的功能模块

推荐一款PHP+jQuery制作的列表分页的功能模块

最后是织梦官网文章列表页( http://www.Dedecms.com/news/list_133_11.html),包含了"首页"、"上一页"、"下一页"、具体页码、"末页"、下拉菜单选择跳转至第几页、信息。分页偏移量前后都是5页:

推荐一款PHP+jQuery制作的列表分页的功能模块

浏览至第11页时非常遗憾,宽度过宽导致版式出现问题:

推荐一款PHP+jQuery制作的列表分页的功能模块

推荐一款PHP+jQuery制作的列表分页的功能模块

这个分页功能的做法和效果是:

1.url 分页 style1:

①手动输入跳转页码的方式:

始终显示最后一页

推荐一款PHP+jQuery制作的列表分页的功能模块

"..."跳转至 当前显示的除末页的最大页码下一页,鼠标放在上面给出提示

推荐一款PHP+jQuery制作的列表分页的功能模块

前后偏移量可自定义,可相同可不同,前面的"..."跳转至最前页除首页页码的前一页

推荐一款PHP+jQuery制作的列表分页的功能模块

②下拉菜单选择跳转的方式:

推荐一款PHP+jQuery制作的列表分页的功能模块

2.url 分页 style2:

使用"首页"和"末页"代替页码"1"和最后一页页码,使用前n页、后n页代替"..."

推荐一款PHP+jQuery制作的列表分页的功能模块

为了使"前10页"和"后10页"同时出现,增加数据库的数据

推荐一款PHP+jQuery制作的列表分页的功能模块

同样有下拉菜单跳转方式

推荐一款PHP+jQuery制作的列表分页的功能模块

3.ajax 分页

出现的元素只有"首页"、"上一页"、"下一页"和"末页"。

首页时:

推荐一款PHP+jQuery制作的列表分页的功能模块

中间时:

推荐一款PHP+jQuery制作的列表分页的功能模块

末页时:

推荐一款PHP+jQuery制作的列表分页的功能模块

模块的文件结构图:

ROOT: ├─conn │ └─conn.PHP │ ├─libs -- smarty库 │ ├─templates │ │ │ ├─demo.html -- 功能页模板文件 │ │ │ ├─css │ │ ├─common.css │ │ └─style1.css │ │ │ ├─images │ │ └─loading.gif -- ajax分页时请求数据接收到之前的加载图 │ └─js │ ├─jquery-1.8.3.min.js │ └─ajax.js -- 当分页方式为ajax时模板demo.html加载的js │ ├─templates_c │ ├─init.inc.PHP -- smarty配置文件 │ ├─page.class.PHP -- 分页类 │ ├─demo.PHP │ └─ajaxpage.PHP -- ajax分页时接受请求的PHP文件

要注意的地方:

1.偏移量的显示设置,主要是什么时候 url 分页方式1,什么时候显示"..." :当前页码 - 前偏移量 - 1 > 1 时,应当显示前面的"..."; 当前页码 + 后偏移量 + 1 <>显示后面的"...";

2.选择性加载 js :当使用 ajax 方式进行分页时,才加载 ajax.js

3.外部的 js 无法解析 smarty 的标签,所以在使用外部 js 时的传值要进行处理

4.ajax 分页时,认是第一页,也就是一定首先会出现 "下一页" 和 "末页",所以 "上一页" 和 "首页" 的添加和点击函数应当包含在"下一页" 和 "末页" 的点击函数中。

主要代码

page.class.PHP:

PHP;">

class MyPageUrl{

private $totalNum;
private $perpageNum; //每页显示条数
private $pageNow; //当前页页码
private $url; //当前url

//页码显示
private $pageStyle; //页码样式,提供2种样式

private $prePage; //页码前偏移量
private $floPage; //页码后偏移量

private $skipStyle; //手动跳转,0为手动输入页码,1为下拉菜单选择页码

private $page_act; //翻页样式 0:url 1:ajax

//页码文字
//style2&style3
private $firstFonts = "首页";
private $lastFonts = "末页";

private $nextFonts = "下一页 >";
private $preFonts = "< 上一页";

//前n页,后n页
private $page_n;
private $pn = 10;
private $pn_fonts = "前10页";
private $fn = 10;
private $fn_fonts = "后10页";

//展现的页码
private $pageShow;

//构造函数
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->getPage<a href="http://code.js-code.com/tag/Now/" target="_blank" class="keywords">Now</a>($p);

$this->totalPage = $this->getTotalPage(); //总页数

$this->pageShow = "";
$this->getUrl();

}

/****定义__toString方法,把对象解析成字符串**/
public function __toString(){

return $this->pageShow;

}

/****获得当前页页码,$p用来接收$_GET['p']***/
public function getPageNow($p){

if(!isset($p)){

  $this->page<a href="http://code.js-code.com/tag/Now/" target="_blank" class="keywords">Now</a> = 1;
}else if($p>0){

  $this->page<a href="http://code.js-code.com/tag/Now/" target="_blank" class="keywords">Now</a> = $p;  
}else{

  die("page number error");
}

return $this->page<a href="http://code.js-code.com/tag/Now/" target="_blank" class="keywords">Now</a>;

}

/*****设置当前页面链接*/
public function getUrl(){

$url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

//判断是否带参数
if(strpos($url,"?") === false){ //不带参数

  return $this->url = $url."?";
}else{ //带参数

  $url = explode("?",$url);
  //参数
  $param = $url[1];

  //判断是否有多个参数
  if(strpos($param,"&") === false){ //只有<a href="http://code.js-code.com/tag/yige/" target="_blank" class="keywords">一个</a>参数

    //判断参数是否为p
    if(strpos($param,"p=") === false){ //不含参数p

      //合并url
      $url = implode("?",$url);  

      return $this->url = $url."&";

    }else{

      //把参数p去掉
      $url = $url[0];

      return $this->url = $url."?";
    }

  }else{ //多个参数

    $param = explode("&",$param);

    //遍历参数数组
    foreach($param as $k=>$v){

      if(strpos($v,"p=") === false){

        continue;
      }else{

        //当含有参数p时,把它从数组中<a href="http://code.js-code.com/tag/shanchu/" target="_blank" class="keywords">删除</a>
        unset($param[$k]);
      }
    }

      //<a href="http://code.js-code.com/tag/shanchu/" target="_blank" class="keywords">删除</a>参数p之后组合数组
      $param = implode("&",$param);
      $url[1] = $param;
      $url = implode("?",$url);

      return $this->url = $url."&";
  }
}

}

/****前偏移量处理****/
public function preOffset($preFonts){

//前偏移量的处理
if($this->page<a href="http://code.js-code.com/tag/Now/" target="_blank" class="keywords">Now</a>!=1 && ($this->page<a href="http://code.js-code.com/tag/Now/" target="_blank" class="keywords">Now</a> - $this->prePage -1 <= 1)){

  //上一页
  $this->pageShow .= "<a id=\"per_page\" class=\"pagenum\" href=\"".$this->url."p=".($this->pageNow-1)."\"&gt;".($preFonts == ""?$this->preFonts:$preFonts)."</a>";


  //<a href="http://code.js-code.com/tag/yema/" target="_blank" class="keywords">页码</a>
  for($i=1;$i<=$this->page<a href="http://code.js-code.com/tag/Now/" target="_blank" class="keywords">Now</a>-1;$i++){    

    //ajax方式<a href="http://code.js-code.com/tag/buxianshi/" target="_blank" class="keywords">不显示</a>
    if($this->page_act != 1){

      $this->pageShow .= "<a class=\"pagenum\" href=\"".$this->url."p=".$i."\"&gt;".$i."</a>";  
    }
  }

}else if($this->page<a href="http://code.js-code.com/tag/Now/" target="_blank" class="keywords">Now</a> - $this->prePage -1 > 1){ //page<a href="http://code.js-code.com/tag/Now/" target="_blank" class="keywords">Now</a>至少大于2时才会出现"1..."

  //样式1.<a href="http://code.js-code.com/tag/jiashang/" target="_blank" class="keywords">加上</a>'<a href="http://code.js-code.com/tag/shouye/" target="_blank" class="keywords">首页</a>'
  if($this->pageStyle == 2 || $this->page_act == 1){

    //<a href="http://code.js-code.com/tag/shouye/" target="_blank" class="keywords">首页</a>
    $this->pageShow .= "<a id=\"first_page\" class=\"pagenum\" href=\"".$this->url."p=1\"&gt;".$this->firstFonts."</a>";

    //style2.前n页
    if($this->page_n == 1 && $this->page_act != 1){

      if($this->page<a href="http://code.js-code.com/tag/Now/" target="_blank" class="keywords">Now</a>>$this->pn){

        $this->pageShow .= "<a id=\"pre_n_page\" class=\"pagenum\" href=\"".$this->url."p=".($this->pageNow-$this->pn)."\"&gt;".$this->pn_fonts."</a>";
      }
    }
  }


  //<a href="http://code.js-code.com/tag/shangyiye/" target="_blank" class="keywords">上一页</a>
  $this->pageShow .= "<a id=\"pre_page\" class=\"pagenum\" href=\"".$this->url."p=".($this->pageNow-1)."\"&gt;".($preFonts == ""?$this->preFonts:$preFonts)."</a>";

  //样式1.<a href="http://code.js-code.com/tag/jiashang/" target="_blank" class="keywords">加上</a>第一页'1'、'...'
  if($this->pageStyle == 1){
    $this->pageShow .= "<a class=\"pagenum\" href=\"".$this->url."\"&gt;1</a><a id=\"pre_page_2\" class=\"pagenum\" href=\"".$this->url."p=".($this->pageNow-$this->prePage-1)." \" title=\"第".($this->pageNow-$this->prePage-1)."页\"&gt;…</a>";
  }

  for($i=$this->prePage;$i>=1;$i--){    

    //当前页和'...'之<a href="http://code.js-code.com/tag/jiande/" target="_blank" class="keywords">间的</a><a href="http://code.js-code.com/tag/yema/" target="_blank" class="keywords">页码</a>,ajax方式<a href="http://code.js-code.com/tag/buxianshi/" target="_blank" class="keywords">不显示</a>
    if($this->page_act != 1){

      $this->pageShow .= "<a class=\"pagenum\" href=\"".$this->url."p=".($this->pageNow-$i)."\"&gt;".($this->pageNow-$i)."</a>";  
    }
  }
}

}

/**页码和后偏移量处理***/
public function floOffset($nextFonts){

if($this->totalPage > $this->floPage){ //总页数大于后偏移量时

  for($i=0;$i<=$this->floPage;$i++){

    $page = $this->page<a href="http://code.js-code.com/tag/Now/" target="_blank" class="keywords">Now</a>+$i;

    if($page<=$this->totalPage){

      //<a href="http://code.js-code.com/tag/yema/" target="_blank" class="keywords">页码</a>,ajax方式<a href="http://code.js-code.com/tag/buxianshi/" target="_blank" class="keywords">不显示</a>
      if($this->page_act != 1){

        $this->pageShow .= "<a class=\"pagenum\" href=\"".$this->url."p=".$page."\"&gt;".$page."</a>";
      }
    }
  }

  if($this->page<a href="http://code.js-code.com/tag/Now/" target="_blank" class="keywords">Now</a> < $this->totalPage){


    //当前页+后偏移量+1小于总页数时出现"..."
    if(($this->page<a href="http://code.js-code.com/tag/Now/" target="_blank" class="keywords">Now</a>+$this->floPage+1)<$this->totalPage){

      //样式1.<a href="http://code.js-code.com/tag/xianshi/" target="_blank" class="keywords">显示</a>'...'
      if($this->pageStyle == 1){
        $this->pageShow .= "<a id=\"flo_page_2\" class=\"pagenum\" href=\"".$this->url."p=".($page+1)."\" title=\"第".($page+1)."页\"&gt;…</a>";
      }
    }


    //当前页+后偏移量+1小于等于总页数时出现最后一页的快捷<a href="http://code.js-code.com/tag/biaoqian/" target="_blank" class="keywords">标签</a>
    if(($this->page<a href="http://code.js-code.com/tag/Now/" target="_blank" class="keywords">Now</a>+$this->floPage+1)<=$this->totalPage){

      //最后一页
      //样式1.始终出现'最后一页<a href="http://code.js-code.com/tag/yema/" target="_blank" class="keywords">页码</a>'
      if($this->pageStyle == 1){
        $this->pageShow .= "<a class=\"pagenum\" href=\"".$this->url."p=".$this->totalPage."\" title=\"总共".$this->totalPage."页\"&gt;".$this->totalPage."</a>";
      }
    }            

    $this->pageShow .= "<a id=\"flo_page\" class=\"pagenum\" href=\"".$this->url."p=".($this->pageNow+1)."\"&gt;".($nextFonts == ""?$this->nextFonts:$nextFonts)."</a>"; //当实例化对象时<a href="http://code.js-code.com/tag/yonghu/" target="_blank" class="keywords">用户</a>传递的<a href="http://code.js-code.com/tag/wenzi/" target="_blank" class="keywords">文字</a>为空时则<a href="http://code.js-code.com/tag/diaoyong/" target="_blank" class="keywords">调用</a>类预设的"<a href="http://code.js-code.com/tag/xiayiye/" target="_blank" class="keywords">下一页</a>",否则<a href="http://code.js-code.com/tag/shuchu/" target="_blank" class="keywords">输出</a><a href="http://code.js-code.com/tag/yonghu/" target="_blank" class="keywords">用户</a>传递的值

    //style2.<a href="http://code.js-code.com/tag/jiashang/" target="_blank" class="keywords">加上</a>后n页
    if($this->pageStyle == 2 && $this->page_n == 1 && $this->page_act != 1){

      if(($this->page<a href="http://code.js-code.com/tag/Now/" target="_blank" class="keywords">Now</a>+10)<$this->totalPage){

        $this->pageShow .= "<a id=\"flo_n_page\" class=\"pagenum\" href=\"".$this->url."p=".($this->pageNow+$this->fn)."\"&gt;".$this->fn_fonts."</a>";
      }
    }

    //<a href="http://code.js-code.com/tag/xianshi/" target="_blank" class="keywords">显示</a>'末页'
    if($this->pageStyle == 2){

      if(($this->page<a href="http://code.js-code.com/tag/Now/" target="_blank" class="keywords">Now</a>+$this->floPage+1)<$this->totalPage){

        $this->pageShow .= "<a id=\"last_page\" class=\"pagenum\" href=\"".$this->url."p=".$this->totalPage."\"&gt;末页</a>";
      }
    }

  }else if($this->page<a href="http://code.js-code.com/tag/Now/" target="_blank" class="keywords">Now</a> > $this->totalPage){

    die("超<a href="http://code.js-code.com/tag/chuyema/" target="_blank" class="keywords">出页码</a>范围");
  }

}else{ //总页数小于后偏移量时

  for($i=0;$i<$this->totalPage;$i++){

    $page = $this->page<a href="http://code.js-code.com/tag/Now/" target="_blank" class="keywords">Now</a>+$i;
    //<a href="http://code.js-code.com/tag/yema/" target="_blank" class="keywords">页码</a>后边界
    $this->pageShow .= "<a class=\"pagenum\" href=\"".$this->url."p=".$page."\"&gt;".$page."</a>";
  }
}

}

/****其它页面信息***/
public function getOtherInfo(){

//ajax方式<a href="http://code.js-code.com/tag/buxianshi/" target="_blank" class="keywords">不显示</a>手动<a href="http://code.js-code.com/tag/tiaozhuan/" target="_blank" class="keywords">跳转</a>信息
if($this->page_act != 1){

  $this->pageShow .= "<a href="http://code.js-code.com/tag/tiaozhuan/" target="_blank" class="keywords">跳转</a>至 ";

  //<a href="http://code.js-code.com/tag/tiaozhuan/" target="_blank" class="keywords">跳转</a>类型
  if($this->skipStyle =="" ){ //不加<a href="http://code.js-code.com/tag/tiaozhuan/" target="_blank" class="keywords">跳转</a>

    $this->pageShow .= "";
  }else if($this->skipStyle == 1){ //输入框

    $this->pageShow .= "<input id=\"skip\" type=\"text\" value=\"".$this->page<a href="http://code.js-code.com/tag/Now/" target="_blank" class="keywords">Now</a>."\"&gt;";

    $this->pageShow .= "<button id=\"go\"&gt;GO</button>";
  }else if($this->skipStyle == 2){ //下拉<a href="http://code.js-code.com/tag/caidan/" target="_blank" class="keywords">菜单</a>

    //选择下拉<a href="http://code.js-code.com/tag/caidan/" target="_blank" class="keywords">菜单</a><a href="http://code.js-code.com/tag/zidongtiaozhuan/" target="_blank" class="keywords">自动跳转</a>
    $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."\""; 

      //下拉<a href="http://code.js-code.com/tag/caidan/" target="_blank" class="keywords">菜单</a><a href="http://code.js-code.com/tag/mo/" target="_blank" class="keywords">默</a>认<a href="http://code.js-code.com/tag/xianshi/" target="_blank" class="keywords">显示</a>当前页
      if($i == $this->page<a href="http://code.js-code.com/tag/Now/" target="_blank" class="keywords">Now</a>){

        $this->pageShow .= " selected";
      }

      $this->pageShow .= "&gt;".$i."</option>";
    }

    $this->pageShow .= "</select>";
  }
}

$this->pageShow .= "<span id=\"pagenow_info\"&gt;当前第".$this->page<a href="http://code.js-code.com/tag/Now/" target="_blank" class="keywords">Now</a>."页</span>";
$this->pageShow .= "/<span id=\"totalpage_info\"&gt;共".$this->totalPage."页</span>";
$this->pageShow .= "<span id=\"totalNum_info\"&gt;共".$this->totalNum."条</span>";

}

/获取每页第一条信息/
public function getFirstRow(){

$firstRow = $this->perpageNum * ($this->page<a href="http://code.js-code.com/tag/Now/" target="_blank" class="keywords">Now</a>-1) + 1;//当前页第一条是总条数中第几条

return $firstRow;

}

/****获得总页数***/
public function getTotalPage(){

$totalPage = ceil($this->totalNum / $this->perpageNum);
return $totalPage;

}

/* ****获取上一页下一页文字*** */

public function getPreFonts($preFonts){

  return ($preFonts=="")?$this->preFonts:$preFonts;

}

public function getNextFonts($nextFonts){

  return ($nextFonts=="")?$this->nextFonts:$nextFonts;

}

}

大佬总结

以上是大佬教程为你收集整理的推荐一款PHP+jQuery制作的列表分页的功能模块全部内容,希望文章能够帮你解决推荐一款PHP+jQuery制作的列表分页的功能模块所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: