php分页类
php分页类
php分页类
<?php /*Created By RexLee 分页类 **PHP file Page.php 2012-8-13 */ header('charset:utf-8'); class Page { public $quote_url='#?=';//当前引用页,请将你的当前引用页面填充到#处,例如getPage.php public $tail_num=5;//分页栏显示页面链接数 public $haveTail=false;//是否开启尾部导航 private $_2DArray; private $length; private $first_page=1; private $last_page; //public $help="示例:<br />\$page=new Page(\$_2DArray));<br />\$page->\$quote_url='myPage.php?=';<br />\$page->\$tail_num=10;<br />\$page->auto(11, 6);<br />关于\$_2DArray是一个二维数组,可以是:<pre style=\"font-style:italic;font-weight:bold\"> Array([0] => Array([0] => 1,[Id] => 1,[1] => e,[word] => e) , [1] => Array([0] => 2,[Id] => 2,[1] => x,[word] => x))</pre>也可以是:<pre style=\"font-style:italic; font-weight:bold\"> Array([0] => Array([0] => 1,[1] => e) , [1] => Array([0] => 2,[1] => x))</pre>不符合规则的数组将会报错。若是MySQL数据库中取值转为数组,建议与我写的MySQL数据库配合使用。<br />说明:<br />可设定参数:<br /><pre style=\"font-style:italic;font-weight:bold\">public \$quote_url='#?=';//当前引用页,请将你的当前引用页面填充到#处,例如getPage.php\npublic \$tail_num=5;//分页栏显示页面链接数</pre>\n\$page->auto(\$p, \$rpp)<br />其中\$p是所要获取的页码,即获取第\$p页,\$rpp(即record per page)是每页显示数据条数。<br><em style=\"font-size:12px;font-style:italic;font-weight:bold;float:right;\">李俊[RexLee] 开发</em>"; public function __construct($_2DArray){ $this->_2DArray=$_2DArray; $this->get_length(); } private function get_length(){ $i=0; foreach ($this->_2DArray as $v){ $i++; } $this->length = $i; } private function get($star,$end) { echo '<table id="Page_table">'; echo '<caption id="Page_cap"></caption>'; for ($i=$star-1 ;$i<$end; $i++) { $row = $this->_2DArray[$i]; echo '<tr>'; $j=0; foreach ($row as $value) { $j++; if($j%2==0) echo "<td>{$value}</td>"; } echo '</tr>'; }; echo '</table>'; } private function get_single($star,$end) { echo '<table id="Page_table">'; echo '<caption id="Page_cap"></caption>'; for ($i=$star-1 ;$i<$end; $i++) { $row = $this->_2DArray[$i]; echo '<tr>'; $j=0; foreach ($row as $value) {//单值,没有重复的 echo "<td>{$value}</td>"; } echo '</tr>'; }; echo '</table>'; } private function draw_tail($current_page){//画表尾分页 $half=ceil($this->tail_num/2);//分页移动中点 $begin=$current_page-$half;//$begin开始点 if ($begin<=0) {$begin=1;} $over=$begin+$this->tail_num-1;//结束点 $back=$current_page-1;//前一页 $next=$current_page+1;//后一页 //画分页span echo '<span>'; //BackPage if ($current_page==$this->first_page) { echo "<a id=\"Page_back\"></a> "; }else{ echo "<a id=\"Page_back\" href=\"{$this->quote_url}{$back}\"><<</a> "; } for ($i=$begin; $i <= $over; $i++) { if ($i>$this->last_page) { ;//页码大于最后一页页码,不予显示 }else{ if ($i!=$current_page) { echo "<a href=\"{$this->quote_url}{$i}\">"; } if ($i==$current_page) { echo '<a id="Page_current">',$i,'</a>'; }else{ echo $i; } if ($i!=$current_page) { echo '</a>'; } echo ' '; } } //NextPage if ($current_page==$this->last_page) { echo "<a id=\"Page_next\"></a>"; }else{ echo "<a id=\"Page_next\" href=\"{$this->quote_url}{$next}\">>></a>"; } echo '</span>'; } public function auto($p,$rpp) {//$p is page. $rpp is record per page 分页方法 //参数检测 $p=floor($p);//整数化 if ($p<=0) {$p=1;}//正数化 //计算$start,$end $start=1+($p-1)*$rpp; $end=$p*$rpp; //$total_page总页数 $total_page=ceil($this->length/$rpp); //最后一页设定 $this->last_page=$total_page; //超页码约束 if($p>$this->last_page){ $p=$this->last_page; $start=1+($p-1)*$rpp; $end=$p*$rpp; } //画表 if (current(current(array_count_values(current($this->_2DArray))))==1) {//single $this->get_single($start, $end); }else if (current(array_count_values(current($this->_2DArray)))==2){//double $this->get($start, $end); }else { echo '请重整你的数组...'; } //画分页 if ($this->haveTail) { $this->draw_tail($p); } } }; ///////////////////////////Example/////////////////////////////////////////////////// // $search=new MySQL('localhost', 'root', 'lijun'); // $search->opendb('demo', 'utf8'); // $arr=$search->to2DArray($search->query('select * from test')); // $page=new Page($arr); // $page->auto(1, 6); //echo $page->help;//查看帮助信息 ?>
2. [文件] getPage.php ~ 588B 下载(28) 跳至 [1] [2] [全屏预览]
<?php /*Created By RexLee **PHP file getPage.php 2012-8-14 */ include 'Class/SQL.php'; include 'Class/Page.php'; if($_GET["page"]){ $p=$_GET["page"]; }else{ $p=1; } ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf8"> </head> <body> <?php $sql=new MySQL("localhost", "root", "lijun"); $sql->opendb("demo", "utf8"); $_2DArray=$sql->to2DArray($sql->query("select*from words")); $page=new Page($_2DArray); $page->quote_url="getPage.php?page="; $page->haveTail=true; $page->tail_num=7; $page->auto($p, 10) ?> </body> </html>
- 上一篇:php数据库操作类
- 下一篇:通过淘宝IP接口查询客户端地址
精彩图集
精彩文章