代码比较多,万请耐心看完,我总觉得有问题,可是又不知从哪里下手,请各位高人指导下,不胜感激:谢谢!class Pager
{
public $page_size; //每页记录数
public $record_count; //记录总数
public $curren_record; //当前记录
public $current_page; //当前页
public $url; //网页链接地址
public $url_parameters; //url参数
public $url_prefix; //page=?
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
public $page_count; //总页数
public $url_method; //提交方法,post 或 get
public $show_short; //短格式,将不显示 记总数,当前页数信息,每页等信息
public $show_format; //数字,或文字
public $error; //错误信息
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
private $page_text = array( //文字显示方法
0 => array('首页','上一页','下一页','末页'),
1 => array('First','Pre','Next','Last'),
2 => array('|<','<<','>>','>|'),
3 => array('|◀','◀','▶','▶|')
);
public $page_text_type; //文本显示类型对应 $page_text 的 0,1,2
public $page_text_style; // $page_text 的 css样式
private $page_msg = array( //当前记录 提示信息
0 => array('当前记录:','当前页数:'),
1 => array('记录:','页数:'),
2 => array('Records: ','Pages: '),
3 => array('RECORD: ','PAGE: ')
);
public $page_msg_type; // $page_msg 的 类型,对应$page_msg 的0,1
public $page_msg_style; // $page_msg 的 css 样式
public $page_go_type; //一种是select,一种是input,值分别为2,1
public $page_number_style; //$page_number 的css样式
public $page_number_length; //是要显示的页数,类如:1...3,4,5,6,7,8,9...10,当前页是6,$page_number_length 是 3
private $page_number_pad = array(
0 => array('left' => '','right' => ''),
1 => array('left' => '[','right' => ']'),
2 => array('left' => '(','right' => ')'),
3 => array('left' => '|','right' => '|')
);
public $page_number_pad_type; //边界填充类型,对应 $page_number_pad 的0,1,2
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
function __construct()
{}
function __destruct()
{}
function pager()
{}
function set_record_count()
{
if($this->record_count == '')
{
$this->show_error('Please set the count of record.');
}
}
function set_page_count()
{
if(empty($this->page_size) || $this->page_size <= 0)
{
$this->show_error('page size is less than 0.');
}
else if(empty($this->record_count) || $this->record_count <= 0)
{
$this->set_record_count();
}
else
{
$cot = $this->record_count % $this->page_size;
if($cot == 0)
{
$this->page_count = $this->record_count / $this->page_size;
}
else
{
$this->page_count = ($this->record_count / $this->page_size) + 1;
}
}
}
function set_url()
{
$http = $this->url . '?';
$cot = count($this->url_parameters);
$keys = array_keys($this->url_parameters);
$values = array_values($this->url_parameters);
for($i = 0; $i < $cot; $i++)
{
$http .= $keys[$i] . '=' . $values[$i] . '&';
}
$http .= $this->url_prefix . '=';
$this->url = $http;
}
function set_current_page()
{
$page = strtolower($this->url_method) == "post" ? trim($_POST[$this->url_prefix]) : trim($_GET[$this->url_prefix]);
if($page != '')
{
if(!preg_match("/\d*/i",$page))
{
$page = 1;
}
else
{
if($page > $this->page_count || $page <= 0)
{
$page = 1;
}
}
}
else
{
$page = 1;
}
$this->current_page = $page;
}
function set_current_record()
{
if($this->current_record == '')
{
$this->current_record = 0;
}
}
function set_page_default()
{
if($this->page_text_type == '' || !in_array($this->page_text_type,array_keys($this->page_text)))
{
$this->page_text_type = 0;
}
if($this->page_msg_type == '' || !in_array($this->page_msg_type,array_keys($this->page_msg)))
{
$this->page_msg_type = 0;
}
}
function init()
{
$this->url_prefix = ($this->url_prefix != '' ? $this->url_prefix : 'page');
if(!is_array($this->url_parameters) || $this->url_parameters == '')
{
$this->url_parameters = array('class' => 'pager');
}
$this->url = ($this->url != '' ? $this->url : $_SERVER['PHP_SELF']);
if($this->page_text_style == '')
{
$this->page_text_style = "font-family:Arial;color:#222222;font-size:12px;";
}
if($this->page_msg_style == '')
{
$this->page_msg_style = "font-family:Arial;color:#222222;font-size:12px;";
}
if($this->page_number_style == '')
{
$this->page_number_style = $this->page_text_style;
}
$this->set_record_count();
$this->set_page_count();
$this->set_current_page();
$this->set_page_default();
$this->set_url();
}
function get_current_msg()
{
$str = '';
$str = '<font style="'.$this->page_msg_style.'">';
$str .= $this->page_msg[$this->page_msg_type][0] . $this->current_record . '/' . $this->record_count;
$str .= ' ';
$str .= $this->page_msg[$this->page_msg_type][1] . $this->current_page . '/' . $this->page_count;
$str .= '</font>';
return $str;
}
function get_page_msg()
{
$str = '';
if($this->current_page != 1)
{
$str .= '<a href="'.$this->url.'1" style="'.$this->page_text_style.'">'.
$this->page_text[$this->page_text_type][0].'</a>';
}
else
{
$str .= '<font style="'.$this->page_text_style.'">'.
$this->page_text[$this->page_text_type][0].'</font>';
}
$str .= ' ';
if($this->current_page > 1 && $this->current_Page < $this->page_count)
{
$str .= '<a href="'.$this->url.($this->current_page-1).'" style="'.$this->page_text_style.'">'.
$this->page_text[$this->page_text_type][1].'</a>';
}
else
{
$str .= '<font style="'.$this->page_text_style.'">'.
$this->page_text[$this->page_text_type][1].'</font>';
}
$str .= ' ';
if($this->current_page < $this->page_count)
{
$str .= '<a href="'.$this->url.($this->current_page+1).'" style="'.$this->page_text_style.'">'.
$this->page_text[$this->page_text_type][2].'</a>';
}
else
{
$str .= '<font style="'.$this->page_text_style.'">'.
$this->page_text[$this->page_text_type][2].'</font>';
}
$str .= ' ';
if($this->current_page != $this->page_count)
{
$str .= '<a href="'.$this->url.$this->page_count.'" style="'.$this->page_text_style.'">'.
$this->page_text[$this->page_text_type][3].'</a>';
}
else
{
$str .= '<font style="'.$this->page_text_style.'">'.
$this->page_text[$this->page_text_type][3].'</font>';
}
return $str;
}
}
{
public $page_size; //每页记录数
public $record_count; //记录总数
public $curren_record; //当前记录
public $current_page; //当前页
public $url; //网页链接地址
public $url_parameters; //url参数
public $url_prefix; //page=?
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
public $page_count; //总页数
public $url_method; //提交方法,post 或 get
public $show_short; //短格式,将不显示 记总数,当前页数信息,每页等信息
public $show_format; //数字,或文字
public $error; //错误信息
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
private $page_text = array( //文字显示方法
0 => array('首页','上一页','下一页','末页'),
1 => array('First','Pre','Next','Last'),
2 => array('|<','<<','>>','>|'),
3 => array('|◀','◀','▶','▶|')
);
public $page_text_type; //文本显示类型对应 $page_text 的 0,1,2
public $page_text_style; // $page_text 的 css样式
private $page_msg = array( //当前记录 提示信息
0 => array('当前记录:','当前页数:'),
1 => array('记录:','页数:'),
2 => array('Records: ','Pages: '),
3 => array('RECORD: ','PAGE: ')
);
public $page_msg_type; // $page_msg 的 类型,对应$page_msg 的0,1
public $page_msg_style; // $page_msg 的 css 样式
public $page_go_type; //一种是select,一种是input,值分别为2,1
public $page_number_style; //$page_number 的css样式
public $page_number_length; //是要显示的页数,类如:1...3,4,5,6,7,8,9...10,当前页是6,$page_number_length 是 3
private $page_number_pad = array(
0 => array('left' => '','right' => ''),
1 => array('left' => '[','right' => ']'),
2 => array('left' => '(','right' => ')'),
3 => array('left' => '|','right' => '|')
);
public $page_number_pad_type; //边界填充类型,对应 $page_number_pad 的0,1,2
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
function __construct()
{}
function __destruct()
{}
function pager()
{}
function set_record_count()
{
if($this->record_count == '')
{
$this->show_error('Please set the count of record.');
}
}
function set_page_count()
{
if(empty($this->page_size) || $this->page_size <= 0)
{
$this->show_error('page size is less than 0.');
}
else if(empty($this->record_count) || $this->record_count <= 0)
{
$this->set_record_count();
}
else
{
$cot = $this->record_count % $this->page_size;
if($cot == 0)
{
$this->page_count = $this->record_count / $this->page_size;
}
else
{
$this->page_count = ($this->record_count / $this->page_size) + 1;
}
}
}
function set_url()
{
$http = $this->url . '?';
$cot = count($this->url_parameters);
$keys = array_keys($this->url_parameters);
$values = array_values($this->url_parameters);
for($i = 0; $i < $cot; $i++)
{
$http .= $keys[$i] . '=' . $values[$i] . '&';
}
$http .= $this->url_prefix . '=';
$this->url = $http;
}
function set_current_page()
{
$page = strtolower($this->url_method) == "post" ? trim($_POST[$this->url_prefix]) : trim($_GET[$this->url_prefix]);
if($page != '')
{
if(!preg_match("/\d*/i",$page))
{
$page = 1;
}
else
{
if($page > $this->page_count || $page <= 0)
{
$page = 1;
}
}
}
else
{
$page = 1;
}
$this->current_page = $page;
}
function set_current_record()
{
if($this->current_record == '')
{
$this->current_record = 0;
}
}
function set_page_default()
{
if($this->page_text_type == '' || !in_array($this->page_text_type,array_keys($this->page_text)))
{
$this->page_text_type = 0;
}
if($this->page_msg_type == '' || !in_array($this->page_msg_type,array_keys($this->page_msg)))
{
$this->page_msg_type = 0;
}
}
function init()
{
$this->url_prefix = ($this->url_prefix != '' ? $this->url_prefix : 'page');
if(!is_array($this->url_parameters) || $this->url_parameters == '')
{
$this->url_parameters = array('class' => 'pager');
}
$this->url = ($this->url != '' ? $this->url : $_SERVER['PHP_SELF']);
if($this->page_text_style == '')
{
$this->page_text_style = "font-family:Arial;color:#222222;font-size:12px;";
}
if($this->page_msg_style == '')
{
$this->page_msg_style = "font-family:Arial;color:#222222;font-size:12px;";
}
if($this->page_number_style == '')
{
$this->page_number_style = $this->page_text_style;
}
$this->set_record_count();
$this->set_page_count();
$this->set_current_page();
$this->set_page_default();
$this->set_url();
}
function get_current_msg()
{
$str = '';
$str = '<font style="'.$this->page_msg_style.'">';
$str .= $this->page_msg[$this->page_msg_type][0] . $this->current_record . '/' . $this->record_count;
$str .= ' ';
$str .= $this->page_msg[$this->page_msg_type][1] . $this->current_page . '/' . $this->page_count;
$str .= '</font>';
return $str;
}
function get_page_msg()
{
$str = '';
if($this->current_page != 1)
{
$str .= '<a href="'.$this->url.'1" style="'.$this->page_text_style.'">'.
$this->page_text[$this->page_text_type][0].'</a>';
}
else
{
$str .= '<font style="'.$this->page_text_style.'">'.
$this->page_text[$this->page_text_type][0].'</font>';
}
$str .= ' ';
if($this->current_page > 1 && $this->current_Page < $this->page_count)
{
$str .= '<a href="'.$this->url.($this->current_page-1).'" style="'.$this->page_text_style.'">'.
$this->page_text[$this->page_text_type][1].'</a>';
}
else
{
$str .= '<font style="'.$this->page_text_style.'">'.
$this->page_text[$this->page_text_type][1].'</font>';
}
$str .= ' ';
if($this->current_page < $this->page_count)
{
$str .= '<a href="'.$this->url.($this->current_page+1).'" style="'.$this->page_text_style.'">'.
$this->page_text[$this->page_text_type][2].'</a>';
}
else
{
$str .= '<font style="'.$this->page_text_style.'">'.
$this->page_text[$this->page_text_type][2].'</font>';
}
$str .= ' ';
if($this->current_page != $this->page_count)
{
$str .= '<a href="'.$this->url.$this->page_count.'" style="'.$this->page_text_style.'">'.
$this->page_text[$this->page_text_type][3].'</a>';
}
else
{
$str .= '<font style="'.$this->page_text_style.'">'.
$this->page_text[$this->page_text_type][3].'</font>';
}
return $str;
}
}
function get_page_go()
{
if($this->page_go_type == '')
$this->page_go_type = 1;
$str = '';
if($this->page_go_type == '1' || $this->page_go_type == 'input')
{
$str = " <input type=\"text\" size=\"3\" id=\"GoPage\"
name=\"GoPage\" onKeyUp=\"if(isNaN(this.value)){alert('input is not a number!');this.value='';}\" />";
$str .= " <input type=\"button\" name=\"btnGoPage\" value=\"GO\" size=\"5\"
onClick=\"g = document.getElementById('GoPage');
if(g.value == '')
{alert('please type a number of page!');}
else if(g.value < 0 || g.value > ".$this->page_count.")
{alert('value must be 0 - ".$this->page_count."');g.value='';}
else
{location.href='".$this->url."'+g.value;}\" />";
}
else if($this->page_go_type == '2' || $this->page_go_type == 'select')
{
$str .= " <select name=\"GoPage\" id=\"GoPage\" onChange=\"location.href='".$this->url."'+this.options[this.selectedIndex].value\">";
for($i = 1; $i <= $this->page_count; $i++)
{
$str .= '<option value="'.$i.'">'.$i.'</option>';
}
$str .= '</select>';
}
return $str;
}
function get_page_number()
{
if($this->page_number_length == '')
$this->page_number_length = $this->page_count;
if($this->page_number_pad_type == '')
$this->page_number_pad_type = 0; $str = '';
$left = $this->page_number_pad[$this->page_number_pad_type]['left'];
$right = $this->page_number_pad[$this->page_number_pad_type]['right'];
if($this->page_count <= $this->page_number_length * 2)
{
for($i = 1; $i <= $this->page_count; $i++)
{
$str .= $left.($this->current_page == $i ? '<font style="'.$this->page_number_style.'">'.$i.'</font>' : '<a href="'.$this->url.$i.'" style="'.$this->page_number_style.'">'.$i.'</a>').$right;
$str .= ' ';
}
}
else
{
if($this->current_page <= $this->page_number_length + 2)
{
for($i = 1; $i <= $this->current_page; $i++)
{
$str .= $left.($this->current_page == $i ? '<font style="'.$this->page_number_style.'">'.$i.'</font>' : '<a href="'.$this->url.$i.'" style="'.$this->page_number_style.'">'.$i.'</a>').$right;
$str .= ' ';
}
}
else
{
$str .= $left.'<a href="'.$this->url.'1" style="'.$this->page_number_style.'">1</a>'.$right;
$str .= ' ... ';
for($i = $this->current_page - $this->page_number_length; $i <= $this->current_page; $i++)
{
$str .= $left.($this->current_page == $i ? '<font style="'.$this->page_number_style.'">'.$i.'</font>' : '<a href="'.$this->url.$i.'" style="'.$this->page_number_style.'">'.$i.'</a>').$right;
$str .= ' ';
}
}
if($this->current_page + $this->page_number_length >= $this->page_count)
{
for($i = $this->current_page + 1; $i <= $this->page_count; $i++)
{
$str .= $left.($this->current_page == $i ? '<font style="'.$this->page_number_style.'">'.$i.'</font>' : '<a href="'.$this->url.$i.'" style="'.$this->page_number_style.'">'.$i.'</a>').$right;
$str .= ' ';
}
}
else
{
for($i = $this->current_page + 1; $i <= $this->current_page + $this->page_number_length; $i++)
{
$str .= $left.($this->current_page == $i ? '<font style="'.$this->page_number_style.'">'.$i.'</font>' : '<a href="'.$this->url.$i.'" style="'.$this->page_number_style.'">'.$i.'</a>').$right;
$str .= ' ';
}
$str .= ' ... ';
$str .= $left.'<a href="'.$this->url.$this->page_count.'" style="'.$this->page_number_style.'">'.$this->page_count.'</a>'.$right;
}
}
return $str;
}
function show()
{
echo $this->get_current_msg();
echo ' ';
echo $this->get_page_msg();
}
function show_short()
{
echo $this->get_page_msg();
}
function show_long($go_type='')
{
echo $this->get_current_msg();
echo ' ';
echo $this->get_page_msg();
echo ' ';
echo $this->get_page_go($go_type);
}
function show_number()
{
echo $this->get_page_number();
} function show_all()
{
echo $this->get_current_msg();
echo ' ';
echo $this->get_page_msg();
echo ' ';
echo $this->get_page_number();
echo ' ';
echo $this->get_page_go();
}
function show_error($err)
{
$this->error = $err;
echo " <html>
<head>
<meta http-equiv='content-type' content='text/html; charset=utf-8'>
</head>
<body>
<font style='font-family:arial;font-size:12px;'><br />
<br />Return error :</b> $this->error <br />
<br />url :<font style='color:#0000ee;text-decoration:underline'> ${_SERVER['PHP_SELF']}
</font><br />
<br />please sumit the error report to web master or administrator!<br />
<br /><b><font color=red>scripit is stoped.</font></b></font>
</body>
</html>";
exit();
}
{
echo $this->get_current_msg();
echo ' ';
echo $this->get_page_msg();
echo ' ';
echo $this->get_page_go($go_type);
}哦,自己先发现一个.
require_once('conn.inc.php'); //数据库连接文件
class sep_page extends conn
{
var $pagenum=0; //当年页号
var $pagesize; //每页记录数
var $link; //保存数据库连接句柄
var $pageall; //总共的页数
/** 从数据库获得记录 **/
function getdata($table='',$cols=array(),$pagesize=0,$order_col='')
{
$this->get_page(); //获得页码,并进行过滤
$sql=$this->mk_sql($table,$cols,$pagesize,$order_col); //拼凑sql语句
$conn=parent::connect(); //连接数据库
$r=$conn->query($sql);
$r->setFetchMode(PDO::FETCH_ASSOC);
$row=$r->fetchAll(); //将从数据库取出的记录保存进二维数组 $row
$r1=$conn->query("select count(*) from $table"); //获得总记录数
$n=$r1->fetch();
$this->pageall=$n[0];
return $row; //返回记录集
}
function show_page_num()
{
$max=ceil($this->pageall/$this->pagesize)-1; //最大页码
$last=$this->pagenum-1; //上一页页码
if($this->pagenum>=5) //当页码数大于5时显示第一页的连接
{
$string=$this->getnum(0,'<<');
echo $string;
}
$string=$this->getnum($last,'<'); //显示上一页的连接
echo $string;
if($this->pagenum<5) //如果页码数小于5,显示页码形式如下
{
for($i=0;$i<10;$i++)
{
if($i!=$this->pagenum)
{
echo $this->getnum($i,$i+1); //获得页码连接代码
}
else
{
echo '<div class="pagenum"><b>'.($i+1).'</b></div>';
}
}
$string=$this->getnum($last,'>');
echo $string;
$string=$this->getnum($max,'>>');
echo $string;
}
else if($this->pagenum>=5) //如果页码数大于5,页码条右移
{
for($i=$this->pagenum-5;$i<$this->pagenum+5&&$i<=$max;$i++)
{
if($i!=$this->pagenum)
{
echo $this->getnum($i,$i+1);
}
else
{
echo '<div class="pagenum"><b>'.($i+1).'</b></div>';
}
}
if($this->pagenum<=$max-5) //如果页码数已经大于最大页码数减去5,就不显示下一页和最后一页连接
{
$string=$this->getnum($last,'>');
echo $string;
$string=$this->getnum($max,'>>');
echo $string;
}
}
echo '共有'.($max+1).'页';
}
function getnum($num,$page)
{
$string= '<div class="pagenum" ><a href="'.$_SERVER['PHP_SELF'].'?pagen='.$num.'">'.$page.'</a></div>'; //拼凑代码
return $string;
}
function get_page()
{
if($_GET['pagen'])
{
if(ctype_digit($_GET['pagen']))
{
if($_GET['pagen']>=0)
{
$this->pagenum=$_GET['pagen'];
}
if($_GET['pagen']<0) //如果页码数小于4,发送HTTP报头
{
header("http/1.1 404 Not Found");
exit;
}
}
else //如果页码不是数字,发送http报头
{
header("http/1.1 404 Not Found");
exit;
}
}
}
/* 拼凑SQL语句函数 */
function mk_sql($table='',$cols=array(),$pagesize=0,$order_col='')
{
if($cols)
{
$sql='SELECT ';
foreach ($cols as $col)
{
$sql.=$col.',';
}
$sql=substr($sql,0,-1);
}
else
{
echo '请输入要选择的字段';
exit;
}
if($table)
{
$sql.=' FROM '.$table;
}
else
{
echo '请输入表名称';
exit;
}
if($order_col!='')
{
$sql.=' ORDER BY '.$order_col.' DESC ';
}
if($pagesize!=0)
{
$sql.=' LIMIT '.$this->pagenum*$pagesize.','.$pagesize;
$this->pagesize=$pagesize;
}
else
{
echo '请输入长度值';
exit;
}
return $sql;
}
}
?>
require_once('conn.inc.php'); //数据库连接文件
class sep_page extends conn
{
var $pagenum=0; //当年页号
var $pagesize; //每页记录数
var $link; //保存数据库连接句柄
var $pageall; //总共的页数
/** 从数据库获得记录 **/
function getdata($table='',$cols=array(),$pagesize=0,$order_col='')
{
$this->get_page(); //获得页码,并进行过滤
$sql=$this->mk_sql($table,$cols,$pagesize,$order_col); //拼凑sql语句
$conn=parent::connect(); //连接数据库
$r=$conn->query($sql);
$r->setFetchMode(PDO::FETCH_ASSOC);
$row=$r->fetchAll(); //将从数据库取出的记录保存进二维数组 $row
$r1=$conn->query("select count(*) from $table"); //获得总记录数
$n=$r1->fetch();
$this->pageall=$n[0];
return $row; //返回记录集
}
function show_page_num()
{
$max=ceil($this->pageall/$this->pagesize)-1; //最大页码
$last=$this->pagenum-1; //上一页页码
if($this->pagenum>=5) //当页码数大于5时显示第一页的连接
{
$string=$this->getnum(0,'<<');
echo $string;
}
$string=$this->getnum($last,'<'); //显示上一页的连接
echo $string;
if($this->pagenum<5) //如果页码数小于5,显示页码形式如下
{
for($i=0;$i<10;$i++)
{
if($i!=$this->pagenum)
{
echo $this->getnum($i,$i+1); //获得页码连接代码
}
else
{
echo '<div class="pagenum"><b>'.($i+1).'</b></div>';
}
}
$string=$this->getnum($last,'>');
echo $string;
$string=$this->getnum($max,'>>');
echo $string;
}
else if($this->pagenum>=5) //如果页码数大于5,页码条右移
{
for($i=$this->pagenum-5;$i<$this->pagenum+5&&$i<=$max;$i++)
{
if($i!=$this->pagenum)
{
echo $this->getnum($i,$i+1);
}
else
{
echo '<div class="pagenum"><b>'.($i+1).'</b></div>';
}
}
if($this->pagenum<=$max-5) //如果页码数已经大于最大页码数减去5,就不显示下一页和最后一页连接
{
$string=$this->getnum($last,'>');
echo $string;
$string=$this->getnum($max,'>>');
echo $string;
}
}
echo '共有'.($max+1).'页';
}
function getnum($num,$page)
{
$string= '<div class="pagenum" ><a href="'.$_SERVER['PHP_SELF'].'?pagen='.$num.'">'.$page.'</a></div>'; //拼凑代码
return $string;
}
function get_page()
{
if($_GET['pagen'])
{
if(ctype_digit($_GET['pagen']))
{
if($_GET['pagen']>=0)
{
$this->pagenum=$_GET['pagen'];
}
if($_GET['pagen']<0) //如果页码数小于4,发送HTTP报头
{
header("http/1.1 404 Not Found");
exit;
}
}
else //如果页码不是数字,发送http报头
{
header("http/1.1 404 Not Found");
exit;
}
}
}
/* 拼凑SQL语句函数 */
function mk_sql($table='',$cols=array(),$pagesize=0,$order_col='')
{
if($cols)
{
$sql='SELECT ';
foreach ($cols as $col)
{
$sql.=$col.',';
}
$sql=substr($sql,0,-1);
}
else
{
echo '请输入要选择的字段';
exit;
}
if($table)
{
$sql.=' FROM '.$table;
}
else
{
echo '请输入表名称';
exit;
}
if($order_col!='')
{
$sql.=' ORDER BY '.$order_col.' DESC ';
}
if($pagesize!=0)
{
$sql.=' LIMIT '.$this->pagenum*$pagesize.','.$pagesize;
$this->pagesize=$pagesize;
}
else
{
echo '请输入长度值';
exit;
}
return $sql;
}
}
?>下面是引用次分页类的代码
<?php
ob_start();
require_once('sep_page.class.php');
$obj=new sep_page();
$cols=array(1=>'c_id',2=>'c_name');
$row=$obj->getdata('t_userdata',$cols,50,'c_pass');
if(!$row)
{
header("http/1.1 404 Not Found");
exit;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<style type="text/css">
.pagenum {display:inline;
float:left;
width:30px;
line-height:20px;
text-align:center;
margin-left:5px;
}
.pagenum a{
display:block;
width:30px;
color:#000000;
text-decoration:none;
height:20px;
border:1px solid #00CCFF;
}
.pagenum a:link{background-color:white;}
.pagenum a:hover {background-color:#00CCFF;}
.pagenum a:visited{backgroud-color:#ooccff;}
</style>
</head><body>
<?php
$numall=count($row);
for($i=0;$i<$numall;$i++)
{
echo $row[$i]['c_name'].'<br>';
}
$obj->show_page_num();
ob_end_flush();
?>
</body>
</html>
更郁闷的是,居然有三个空函数~~
上面的是我发的,第一次忘记加格式了,汗一个~~
我可以这么说
楼主的代码会造成SEO问题
在GOOGLE上,楼主的代码因为有了错误处理,当有人错误输入页数时,楼主的代码将其引向同一页面,这在搜索引擎上就叫重复内容,会让站点遭受惩罚。最好的办法,就是发送http/1.1 404 报头。
楼主的代码实在让人头晕,不过貌似楼主是把内容显示也加入进去了吧?
我认为这样做不好,不利于代码重用。因为如果别的页面也要分页,那么这个代码就用不上了
我写的那个是可以从不同的表,搜索不同的记录,每页显示的记录数也是可以改变的,其实很简单的了,随便想一下大家都能做出来,而且记录是返回给一个数组,这样,我们就可以再外部操作这个记录集,让他以与页面和谐的格式显示~~
说这么多不知道楼主有几点没想到,也许都想到了反正代码我是头太晕没看~_~!
本人比较菜,没怎么写过程序
笑我的人我晚上扒他们家窗户~
一个分页
至于弄的这么.........
一个limit
一个for语句不就搞定了吗
<?php
//例Page(281,8,10,$_GET["page"],"&id=1&uid=12");
//$records 总的记录数
//$groups 显示分页组目数
//$showsize 每页显示记录个数
//$thispage 当前页
//$other 其它参数function Page($records,$groups,$showsize,$thispage,$other)
{
if ($thispage=="")
{
$thispage=1;
}
else
{
$thispage=(int)$thispage;
}
$info= "<div style=font-size:13px;>\r\n";
if ($records>0)
{
if ($records % $showsize <>0)
{
$pcount=(int)($records/$showsize)+1;
}
else
{
$pcount=(int)($records/$showsize);
}
$info=$info . "<div class=page>".$thispage."/".$pcount."页</div>\r\n";
if ($pcount>=$thispage)
{
if ($thispage % $groups !=0)
{
$z=(int)($thispage/$groups)+1;
}
else
{
$z=(int)($thispage/$groups);
}
if ($z>1)
{
if (($z-1)*$groups>0)
{
$info=$info . "<div class=page><a href=?page=".($z-1)*$groups.$other.">上一组</a></div>\r\n";
}
else
{
$info=$info . "<div class=page><a href=?page=1".$other.">上一组</a></div>\r\n";
}
}
for($zp=($z-1)*$groups+1;$zp<=$z*$groups;$zp++)
{
if ($zp<=$pcount)
{
if ($zp==$thispage)
{
$info=$info . "<div class=page_true><a href=?page=".$zp.$other.">".$zp."</a></div>\r\n";
}
else
{
$info=$info . "<div class=page><a href=?page=".$zp.$other.">".$zp."</a></div>\r\n";
}
}
}
if ($zp<=$pcount)
{
$info=$info . "<div class=page><a href=?page=".$zp.$other.">下一组</a></div>\r\n";
}
}
else
{
if ($p<=$pcount)
{
$info=$info . "<div class=page><a href=?page=".($pcount-$groups).$other.">上一组</a></div>\r\n";
}
for ($p=$pcount-$groups;$p<=$pcount;$p++)
{
if ($p==$thispage)
{
$info=$info . "<div class=page_true><a href=?page=".$p.$other.">".$p."</a></div>\r\n";
}
else
{
$info=$info . "<div class=page><a href=?page=".$p.$other.">".$p."</a></div>\r\n";
}
}
}
}
else
{
$info=$info . "<div class=page>无记录</div>\r\n";
}
$info=$info . "</div>\r\n";
return $info;
}
?>我来发一个不需要数据库的分页。
//仿dz
class page extends db
{
var $pageSize,$curPage,$totalPage,$start;
var $flag;
var $totalRec;
function page($sql,$pagesize=5,$flag="page")
{
$this->pageSize=$pagesize;
$this->flag=$flag;//翻页标志
$res=parent::query($sql,1);
$this->totalRec=$res["row"];
$this->totalPage=ceil($this->totalRec/$this->pageSize);
$page=$_REQUEST[$this->flag];
if($page==""||$page<=0)
{
$this->curPage=1;
}
else
{
$page>$this->totalPage?$this->curPage=$this->totalPage:$this->curPage=$page;
}
$this->start=($this->curPage-1)*$this->pageSize;
}
function show($step=10,$url="")
{
//步进分页
if($this->totalPage<$step)
{
//总页数少于指定值
$start=1;
$end=$this->totalPage;
}
else
{
$start=$this->curPage-4;//start
$end=$start+$step-1;//end
if($this->totalPage-$start<$step)
{
$start=$this->totalPage-$step;
if($start<=0)
{
$start=1;
}
$end=$this->totalPage;
}
else
{
$start=$this->curPage-4;
if($start<=0)
{
$start=1;
$end=$step;
}
else
{
$end=$step+$start;
}
}
}
$str="<div class=\"pages\"><span>一共".$this->totalRec."条".$this->totalPage."页 当前是第".$this->curPage."页</span>";
$pre=$this->curPage-1;
$str.="<a href=?".$this->flag."=1$url><b>首页</b></a>";
if($pre>1)
{
$str.="<a href=?".$this->flag."=".($this->curPage-1)."$url><b>上一页</b></a>";
}
for($i=$start;$i<=$end;$i++)
{
if($i==$this->curPage)
{
$str.="<strong>$i</strong>";
}
else
{
$str.="<a href=?$this->flag=$i"."$url>$i</a>";
}
}
$next=$this->curPage+1;
if($next<$this->totalPage)
{
$str.="<a href=?".$this->flag."=".($this->curPage+1)."$url>下一页";
}
$str.="<a href =?".$this->flag."=".$this->totalPage."$url>最后一页</a></div>";
return $str;
}
}
我给你一段代码你看看
<?php
@mysql_connect("localhost","root","123456") or die("数据库连接失败");
@mysql_select_db("install") or die("数据库不存在或不可用");
mysql_query("set names gbk");
$query=@mysql_query("select * from cf_lstype inner join newtypecf_ls on cf_lstype.typeid=newtypecf_ls.typeid where newtypecf_ls.newid='$_GET[id]'") or die("SQL 语句执行查询失败");
while($row=mysql_fetch_array($query))
{
?><div class="ul2_2hb"><table width="808" height="282" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="244"><img src="../CMS/admin/flishi/fimg/<?php echo $row["lv"];?>" width="851" height="244" /></td>
</tr>
<tr>
<td height="38" align="center"><table width="755" height="28" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="475" height="28" align="left"><a href="http://<?php echo $row["weburl"];?>" target="_blank"><img src="im/pic_54.gif" width="129" height="28" border="0" /></a></td>
<td width="280" align="center"><span class="STYLE31"><a href="http://<?php echo $row["weburl"];?>" class="STYLE31" target="_blank"></a></span></td>
</tr>
</table></td>
</tr>
</table></div>
<?php
}mysql_close();
?>