61阅读

php数组分页-php分页类 只用传入总页数

发布时间:2017-11-20 所属栏目:方便快捷的营养早餐

一 : php分页类 只用传入总页数

PHP培训有一段时间了,消失也有一段时间了,特地封装一个分页类上传上来,自己写的,高手指点,新手学习,莫笑话!呵呵
配合CSS样式会好看点.

CODE:

DIV.page {

PADDING-RIGHT: 3px; PADDING-LEFT: 3px; PADDING-BOTTOM: 3px; MARGIN: 3px; PADDING-TOP: 3px; TEXT-ALIGN: center

}

DIV.page A {

BORDER-RIGHT: #dedfde 1px solid; PADDING-RIGHT: 6px; BACKGROUND-POSITION: 50% bottom; BORDER-TOP: #dedfde 1px solid; PADDING-LEFT: 6px; PADDING-BOTTOM: 2px; BORDER-LEFT: #dedfde 1px solid; COLOR: #0061de; MARGIN-RIGHT: 3px; PADDING-TOP: 2px; BORDER-BOTTOM: #dedfde 1px solid;TEXT-DECORATION: none
}

DIV.page A:hover {

BORDER-RIGHT: #000 1px solid; BORDER-TOP: #000 1px solid; BACKGROUND-IMAGE: none; BORDER-LEFT: #000 1px solid; COLOR: #fff; BORDER-BOTTOM: #000 1px solid; BACKGROUND-COLOR: #0061de;TEXT-DECORATION: none
}

DIV.page A:active {

BORDER-RIGHT: #000 1px solid; BORDER-TOP: #000 1px solid; BACKGROUND-IMAGE: none; BORDER-LEFT: #000 1px solid; COLOR: #fff; BORDER-BOTTOM: #000 1px solid; BACKGROUND-COLOR: #0061de

}

二 : 方便快捷的PHP长文章分页函数

PHP程序员们在编写程序代码的时候,通常都会用到文章的分页等功能的实现。每次编写不同的程序都需要编写一遍文章分页的代码,比较玛法。

  • 推荐几种PHP实现页面跳转的方法
  • 为你详解PHP页面跳转函数的具体含义
  • PHP header函数的详细用法介绍
  • 经验分享 总结PHP常用函数
  • 一劳永逸 打造简单PHP分页函数

今天我们就为大家写了一段PHP长文章分页函数,方便大家的代码编写:

  1. <?php
  2. functionff_page($content,$page)
  3. {
  4. global$expert_id;
  5. $PageLength=2000;//每页字数
  6. $CLength=strlen($content);
  7. $PageCount=floor(($CLength
    /$PageLength))+1;//计算页数
  8. $PageArray=array();
  9. $Seperator=array("\n",
    "\r","。","!","?",";
    ",",","”","’");//分隔符号
  10. //echo"页数:".$PageCount."<br>";
  11. //echo"长度:".$CLength."<br><br><br>";
  12. //strpos()函数返回字符串在
    另一个字符串中第一次出现的位置
  13. if($CLength<$PageLength)
  14. {
  15. echo$content;
  16. }else{
  17. $PageArray[0]=0;
  18. $Pos=0;
  19. $i=0;
  20. //第一页
  21. for($j=0;$j<sizeof($Seperator);$j++)
  22. {
  23. //echo$Seperator[$j];
  24. $Pos=strpos($content,
    $Seperator[$j],$PageArray[$i]+1900);
  25. while($Pos>0&&$Pos
    <($i+1)*$PageLength&&
    $Pos>$i*$PageLength)
  26. {
  27. $PageArray[$i]=$Pos;
  28. $Pos=strpos($Pos+$PageLength,
    $content,$Seperator[$j]);
  29. }
  30. if($PageArray[$i]>0)
  31. {
  32. $j=$j+sizeof($Seperator)+1;
  33. }
  34. }
  35. //---
  36. for($i=1;$i<$PageCount-1;$i++){
  37. for($j=0;$j<sizeof($Seperator);$j++)
  38. {
  39. //echo$Seperator[$j];
  40. $Pos=strpos($content,$Seperator
    [$j],$PageArray[$i-1]+1900);
  41. while($Pos>0&&$Pos<
    ($i+1)*$PageLength&&$Pos>
    $i*$PageLength)
  42. {
  43. $PageArray[$i]=$Pos;
  44. $Pos=strpos($Pos+$PageLength,
    $content,$Seperator[$j]);
  45. }
  46. if($PageArray[$i]>0)
  47. {
  48. $j=$j+sizeof($Seperator)+1;
  49. }
  50. }
  51. }
  52. //--PHP长文章分页函数最后一页
  53. $PageArray[$PageCount-1]=$CLength;
  54. //$page=2;
  55. if($page==1)
  56. {
  57. $output=substr($content,0,
    $PageArray[$page-1]+2);
  58. }
  59. if($page>1&&$page<=$PageCount)
  60. {
  61. $output=substr($content,$PageArray
    [$page-2]+2,$PageArray[$page-1]-$
    PageArray[$page-2]);
  62. $output="(上接第".($page-1)."页)\n".$output;
  63. }
  64. echostr_replace("\n","<br>&nbsp;
    &nbsp;&nbsp;",$output);
  65. //if($page==$PageCount)
  66. //return$output=substr($content,
    $PageArray[$page-2]+2,$PageArray[$page-1]-$PageArray[$page-2]);
  67. if($PageCount>1)
  68. {
  69. echo"<br><br><br><center>";
  70. echo"<fontcolor='ff0000'>".$page."</font>/".$PageCount."页&nbsp;";
  71. if($page>1)
  72. echo"<ahref=$PHP_SELF?expert_id=$expert_id&page_t=".($page-1).">上一页</a>";
  73. else
  74. echo"上一页";
  75. for($i=1;$i<=$PageCount;$i++)
  76. {
  77. echo"<ahref=$PHP_SELF?expert_id=$expert_id&page_t=".$i.">[".$i."]</a>";
  78. }
  79. if($page<$PageCount)
  80. echo"<ahref=$PHP_SELF?expert_id=$expert_id&page_t=".($page+1).">下一页</a>";
  81. else
  82. echo"下一页";
  83. echo"</center>";
  84. }
  85. }
  86. }?>

PHP长文章分页函数的使用

  1. <?php
  2. $content1=''测试文字,
    尽量长一些,www.corange.cn";
  3. $current=$_REQUEST['page_t'];
  4. $result=ff_page
    ($content1,$current);
  5. echo$result;
  6. ?>

PHP长文章分页函数的使用与实现的代码示例如上所示。

三 : PHP实现对数组分页处理实例详解

本文实例讲述了PHP实现对数组分页处理方法。分享给大家供大家参考,具体如下:

最近用到了用数组数据分页,这里整理了一下,具体代码如下:

 <?php class PaginationArray{  public $pageArray=array(); //数组  public $pageSize=10; //每页显示记录数  public $current= 1; //当前页  private $total=0; //总页数  private $prev=0; //上一页  private $next=0; //下一页  public $argumetsOther='';//设置参数  function __construct($array=array(),$pageSize=10,$current=1){  $this->pageArray=$array;  $this->pageSize=$pageSize;  $this->current=$current; }  /*通过数组进行初始化  * * 数组为关联数组,参数索引为pageArray,pageSize,current  * */  function setArguments($arr){  if (is_array($arr)){  $this->pageArray=$arr['pageArray'];  $this->pageSize=$arr['pageSize'];  $this->current=$arr['current'];  }else{  return ;  }  }  //返回链接  function page(){  $_return=array();  /*calculator*/  $this->total=ceil(Count($this->pageArray)/$this->pageSize);  $this->prev=(($this->current-1)<= 0 ? "1":($this->current-1));  $this->next=(($this->current+1)>=$this->total ? $this->total:$this->current+1);  $current=($this->current>($this->total)?($this->total):$this->current);  $start=($this->current-1)*$this->pageSize;  $arrleng=count($this->pageArray);  for($i=$start;$i<($start+$this->pageSize);$i++){  if($i >= $arrleng)break;  array_push($_return,$this->pageArray[$i]);  }  $pagearray["source"]=$_return;  $pagearray["links"]=$this->linkStyle(2);  return $pagearray;  }  //链接的样式  private function linkStyle($number=1){  $linkStyle='';  switch ($number){  case 1:  $linkStyle="<a href="?page=1">first</a> <a href="?page={$this->prev}">prev</a> <a href="?page={$this->next}">next</a> <a href="?page={$this->total}">end</a>";  break;  case 2:  $linkStyle="<a href="?page=1&{$this->argumetsOther}">首页</a> <a href="?page={$this->prev}&{$this->argumetsOther}">上一页</a> <a href="?page={$this->next}&{$this->argumetsOther}">下一页</a> <a href="?page={$this->total}&{$this->argumetsOther}">尾页</a>";  break;  }  return $linkStyle;  } } //调用的实例 /* header('Content-Type: text/html;charset=utf-8'); $array=array("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20"); $page= isset($_GET['page'])? $_GET['page'] : 1 ; $arrayPage = new PaginationArray($array,"5",$page); $r = $arrayPage->page(); foreach($r["source"] as $s){  echo $s.'<br />'; } echo $r["links"]; */ ?> 

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php+mysql数据库操作入门教程》、《php+mysqli数据库程序设计技巧总结》、《php面向对象程序设计入门教程》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《PHP网络编程技巧总结》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

本文标题:php数组分页-php分页类 只用传入总页数
本文地址: http://www.61k.com/1075926.html

61阅读| 精彩专题| 最新文章| 热门文章| 苏ICP备13036349号-1