一个好用的PHP分页类【最后更新: 2009-02-14】
<?php #********************************************************* #文件名称: page.class.php #功能描述: 修改而来的分页类 #程序制作:荒野无灯(修改) #联系网址:http://netgeek.cn/contact.html #联系qq :admin#netgeek.cn #联系邮箱:admin#netgeek.cn #官方网站: http://netgeek.cn/memory/php/discuz/2008/12/03/discuz-61-bg-music-plugin.asp #copyright (c) 2008 netgeek.cn all rights reserved. #最后更新: 2009-02-14 #********************************************************* class page { /** * config ,public */ var $m_var="page";//page标签(%$$#^32767*&%2)我型我塑www.5x54.com,用来控制url页。比如说xxx.php?PB_page=2中的PB_page var $m_snext='>';//下一页 var $m_spre='<';//上一页 var $m_sfirst='首页';//首页 var $m_slast='尾页';//尾页 var $m_sprebar='<<';//上一分页条 var $m_snextbar='>>';//下一分页条 var $m_sleft='['; var $m_sright=']'; /** * private * */ var $m_barnum=10;//(默认每个分页条显示10个页码数)。 var $m_totalpage=0;//总页数 var $m_current=1;//当前页码 var $m_url="";//url地址头 var $m_offset=0; //var $m_perpage=10;//默认每页显示 记录数 为10 /** * constructor构造函数 * * @param array $array['total']//总记录数,$array['perpage'],$array['current'],$array['url'],$array['barnum']...$arr['var'] */ function page($arr) { if(is_array($arr)){ if(!array_key_exists('total',$arr))$this->error(__FUNCTION__,'need a param of total'); $total=intval($arr['total']); $perpage=(array_key_exists('perpage',$arr))?intval($arr['perpage']):10; $current=(array_key_exists('current',$arr))?intval($arr['current']):''; $url=(array_key_exists('url',$arr))?$arr['url']:''; }else { $total=($arr<0)?0:intval($arr); $perpage=10; $current=''; $url=''; } if((!is_int($total))||($total<0)) $this->error(__FUNCTION__,$total.' is not a positive integer!'); if((!is_int($perpage))||($perpage<=0)) $this->error(__FUNCTION__,$perpage.' is not a positive integer!'); if(!empty($arr['var'])) $this->m_var=$arr['var'];//设置pagename $this->_set_current($current);//设置当前页$current $this->_set_url($url);//设置链接地址 $this->m_totalpage=ceil($total/$perpage);//总页数 $this->m_offset=($this->m_current-1)*$perpage; $this->m_barnum=($arr['barnum']>5)?$arr['barnum']:10; $this->m_perpage=$perpage;//20081102 } function next_page($style='') { if($this->m_current<$this->m_totalpage){ return $this->_get_link($this->_get_url($this->m_current+1),$this->m_snext,$style); } return '<span class="'.$style.'"> </span>'; } /** * 获取显示“上一页”的代码 * * @param string $style * @return string */ function pre_page($style='') { if($this->m_current>1){ return $this->_get_link($this->_get_url($this->m_current-1),$this->m_spre,$style); } return '<span class="'.$style.'"> </span>'; } /** * 获取显示“首页”的代码 * * @return string */ function first_page($style='') { if($this->m_current==1){ return '<span class="'.$style.'"> </span>'; } return $this->_get_link($this->_get_url(1),$this->m_sfirst,$style); } /** * 获取显示“尾页”的代码 * * @return string */ function last_page($style='') { if($this->m_current==$this->m_totalpage ||0==$this->m_totalpage){ return '<span class="'.$style.'"> </span>'; } return $this->_get_link($this->_get_url($this->m_totalpage),$this->m_slast,$style); } function nowbar($style='',$current_style='') { $plus=ceil($this->m_barnum/2); if($this->m_barnum-$plus+$this->m_current>$this->m_totalpage) $plus=($this->m_barnum-$this->m_totalpage+$this->m_current); $begin=$this->m_current-$plus+1; $begin=($begin>=1)?$begin:1; $return=''; for($i=$begin;$i<$begin+$this->m_barnum;$i++) { if($i<=$this->m_totalpage){ if($i!=$this->m_current) $return.=$this->_add_bracket($this->_get_link($this->_get_url($i),$i,$style)); else $return.=$this->_add_bracket('<span class="'.$current_style.'"><font color=red>'.$i.'</font></span>'); }else { break; } $return.="\n"; } unset($begin); return $return; } /** * 获取显示跳转按钮的代码 * * @return string */ function select() { if(0==$this->m_totalpage) $return=' '; else { $return="<script language=\"javascript\" type=\"text/javascript\"> function changeurl(page_args) { document.location.href=page_args; } </script> <select name=\"page_select\" onChange=changeurl(this.options[this.selectedIndex].value) id=\"page_select\">"; for($i=1;$i<=$this->m_totalpage;$i++) { if($i==$this->m_current){ $return.='<option value="'.$this->_get_url($i).'" selected>'.$i.'</option>'; }else { $return.='<option value="'.$this->_get_url($i).'">'.$i.'</option>'; } } unset($i); $return.='</select>'; } return $return; } /** * 获取mysql 语句中limit需要的值 * * @return string */ function offset() { return $this->m_offset; } /** * 控制分页显示风格(你可以增加相应的风格) * * @param int $mode * @return string */ function show($mode=1) { switch ($mode) { case '1': $this->m_snext='下一页'; $this->m_spre='上一页'; return $this->first_page().' '.$this->pre_page().$this->nowbar().$this->next_page().' '.$this->last_page().$this->select(); break; case '2': $this->m_snext='下一页'; $this->m_spre='上一页'; $this->m_sfirst='首页'; $this->m_slast='尾页'; return $this->first_page().$this->pre_page().'[第'.$this->m_current.' 页]'.$this->next_page().$this->last_page().'第'.$this->select().' 页'; break; case '3': $this->m_snext='下一页'; $this->m_spre='上一页'; $this->m_sfirst='首页'; $this->m_slast='尾页'; return $this->first_page().$this->pre_page().$this->next_page().$this->last_page(); break; case '4': $this->m_snext='下一页'; $this->m_spre='上一页'; return $this->pre_page().$this->nowbar().$this->next_page(); break; case '5': return $this->m_sprebar.$this->pre_page().$this->nowbar().$this->next_page().$this->m_snextbar; break; } } /*----------------private function (私有方法)-----------------------------------------------------------*/ /** * 设置url头地址 * @param: String $url * @return boolean */ function _set_url($url="") { if(!empty($url)) { //手动设置 $this->m_url=$url.((stristr($url,'?'))?'&':'?').$this->m_var."="; }else //自动获取 { if(empty($_SERVER['QUERY_STRING'])) { //不存在QUERY_STRING时 $this->m_url=$_SERVER['REQUEST_URI']."?".$this->m_var."="; }else { // if(stristr($_SERVER['QUERY_STRING'],$this->m_var.'=')) { //地址存在页面参数 $this->m_url=str_replace($this->m_var.'='.$this->m_current,'',$_SERVER['REQUEST_URI']); $last=$this->m_url[strlen($this->m_url)-1]; $this->m_url.=($last=='?'||$last=='&')?$this->m_var."=":'&'.$this->m_var."="; }else { $this->m_url=$_SERVER['REQUEST_URI'].'&'.$this->m_var.'='; // $this->m_url='&'.$this->m_var.'='; }//end if }//end if $this->m_url=eregi("\?&",$this->m_url)?str_replace('?&','?',$this->m_url):$this->m_url; }//end if } /** * 设置当前页面 * */ function _set_current($nowindex) { if(empty($nowindex)){ //系统获取 if(isset($_GET[$this->m_var])){ $this->m_current=intval($_GET[$this->m_var]); } }else{ //手动设置 $this->m_current=intval($nowindex); } } /** * 为指定的页面返回地址值 * * @param int $pageno * @return string $url */ function _get_url($pageno=1) { return $this->m_url.$pageno; } /** * 获取分页显示文字(%$$#^32767*&%2)我型我塑www.5x54.com,比如说默认情况下_get_text('<a href="">1</a>')将返回[<a href="">1</a>] * * @param String $str * @return string $url */ function _add_bracket($str) { return $this->m_sleft.$str.$this->m_sright; } /** * 获取链接地址 */ function _get_link($url,$text,$style='') { $style=(empty($style))?'':'class="'.$style.'"'; return '<a '.$style.' href="'.$url.'">'.$text.'</a>'; } /** * 出错处理方式 */ function error($function,$errormsg) { die('Error in file <b>'.__FILE__.'</b> ,Function <b>'.$function.'()</b> :'.$errormsg); } } ?>
$total=$db->query("select * from music_info where uid='$discuz_uid'"); $total=$db->num_rows($total); $page=new page(array('total'=>$total,'perpage'=>'10','var'=>'pid','barnum'=>'8')); $query=$db->query("select * from music_info order by mid desc limit $page->m_offset,$page->m_perpage"); /////////////new added //page $j=0; while($m=$db->fetch_array($query)) { $m['url']=urldecode($m['url']); echo "<tr><td width=10%><a target=_BLANK href='space.php?uid={$m['uid']}'>{$m['uid']}</a></td><td width=10%><input type=\"text\" id=\"song_name_$j\" value=\"{$m['song_name']}\" ></td><td width=10%><input type=\"text\" id=\"singer_$j\" value=\"{$m['singer']}\" ></td><td width=40%><input type=\"text\" id=\"url_$j\" size=70 value=\"{$m['url']}\"></td><td width=30%><input type=checkbox name='del[]' value=\"{$m['mid']}\"><input type=\"hidden\" id=\"edit_mid_$j\" value=\"{$m['mid']}\" > <a href=\"javascript:process($j);\">改</a> <a target=_BLANK href='./my_music_box.php?mid={$m['mid']}'>Play</a></td>"; $j+=1; } echo "共有歌曲{$total}首. 总共{$page->m_totalpage}页 "; echo $page->show(); echo '<br/>';
收藏此文章 打印 更新 编辑此文章 『关闭窗口』