最新消息:商业不相信道德判断,只相信事实与实力!

强悍分页类

SEO/M天地 admin 1488浏览 0评论

在php网络数据库编程时,不可避免的要考虑到数据库记录结果的显示问题,为了呈现美观页面并加快页面的载入速度,就需要对数据库记录进行分页显示。

现把与php完美结合的mysql数据库记录的分页显示实例拿出来与大家共享。
‘;//下一页
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 ‘‘.$this->m_snext.’‘;
}

/**
* 获取显示“上一页”的代码
*
* @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 ‘‘.$this->pre_page.’‘;
}

/**
* 获取显示“首页”的代码
*
* @return string
*/
function first_page($style=”)
{
if($this->m_current==1){
return ‘‘.$this->m_sfirst.’‘;
}
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){
return ‘‘.$this->m_slast.’‘;
}
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(‘‘.$i.’‘);
}else
{
break;
}
$return.=”/n”;
}
unset($begin);
return $return;
}
/**
* 获取显示跳转按钮的代码
*
* @return string
*/
function select()
{
$return=”
‘;
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;
}

/**
* 获取分页显示文字,比如说默认情况下_get_text(‘1‘)将返回[1]
*
* @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 ‘‘.$text.’‘;

}
/**
* 出错处理方式
*/
function error($function,$errormsg)
{
die(‘Error in file ‘.__FILE__.’ ,Function ‘.$function.'() :’.$errormsg);
}
}
?>

转载请注明:李坤锋 » 强悍分页类

您必须 登录 才能发表评论!

网友最新评论 (1)

  1. 真实用 :razz:

    kunfeng072012-05-11 10:19