php怎么设置数据分页 php对数据进行分组( 二 )


/html
php分页怎么弄?/*
总数据条数$total;
每页数据条数$perpage;
当前页$page;
总页数 $cnt = ceil($total/$perpage); ceil函数+1取整.
在$page页,说明前面已经过了($page-1)*$perpage条数据
从($page-1)*$perpage+1开始取数据
分页导航思路:
循环在当前页的前后加“a href=""[]/a”,使其效果为[2][3][4]5[6][7][8]
以下是分页导航类的代码
*/
class PageTool {
protected $total = 0;
protected $perpage = 6;
protected $page = 1;
public function __construct($total,$page=false,$perpage=false) {
$this-total = $total;
if($perpage) {
$this-perpage = $perpage;
}
if($page) {
$this-page = $page;
}
}
// 创建分页导航
public function show() {
$cnt = ceil($this-total/$this-perpage);// 得到总页数
$uri = $_SERVER['REQUEST_URI'];
$parse = parse_url($uri);
$param = array();
if(isset($parse['query'])) {
parse_str($parse['query'],$param);
}
// 不管$param数组里,有没有page单元,都unset一下,确保没有page单元,
// 即保存除page之外的所有单元
unset($param['page']);
$url = $parse['path'] . '?';
if(!empty($param)) {
$param = http_build_query($param);
$url = $url . $param . '';
}
// 计算页码导航
$nav = array();
$nav[0] = 'span class="page_now"' . $this-page . '/span';
for($left = $this-page-1,$right=$this-page+1;($left=1||$right=$cnt)count($nav) = 5;) {
if($left = 1) {
array_unshift($nav,'a href="' . $url . 'page=' . $left . '"[' . $left . ']/a');
$left -= 1;
}
if($right = $cnt) {
array_push($nav,'a href="' . $url . 'page=' . $right . '"[' . $right . ']/a');
$right += 1;
}
}
return implode('',$nav);
}
}
//测试分页导航
$page = $_GET['page']?$_GET['page']:1;//?page=5
$p = new PageTool(100,$page,6); //数据总数100条,每页6条,当前第5页
echo $p-show();//效果为[2][3][4]5[6][7][8]
在php中如何对多条记录进行分页方法一:讲sql查询进行分页进行,需要调用几个函数,具体见脚本:
1.pager.class.php
?php
class pager {
public $sql; //SQL查询语句
public $datanum; //查询所有的数据总记录数
public $page_size; //每页显示记录的条数
protected $_errstr;
protected $_conn;
protected $_query_id;
public function query($query)///这个函数有问题,暂时可以不用
{
$ret = false;
if (!empty($query)) {
if ($this-_conn === false || !is_resource($this-_conn)) {
warningLog(__METHOD__ . ': query sql with no connection', true);
return false;
}
$this-_query_id = @mysql_query($query, $this-_conn);
if ($this-_query_id === false) {
$this-_errstr = @mysql_error();
$ret = false;
} else {
$this-_errstr = 'SUCCESS';
$ret = $this-_query_id;
}
}
$msg = ($ret === false) ? 'false' : strval($ret);
debugLog(__METHOD__.": [$msg] returned for sql query [$query]");
return $ret;
}
function __construct($sql,$page_size) {
$result = mysql_query($sql);
$datanum = mysql_num_rows($result);
$this-sql=$sql;
$this-datanum=$datanum;
$this-page_size=$page_size;
}
//当前页数
public function page_id() {
if($_SERVER['QUERY_STRING'] == ""){
return 1;
}elseif(substr_count($_SERVER['QUERY_STRING'],"page_id=") == 0){
return 1;
}else{
return intval(substr($_SERVER['QUERY_STRING'],8));
}
}
//剩余url值
public function url() {
if($_SERVER['QUERY_STRING'] == ""){
return "";
}elseif(substr_count($_SERVER['QUERY_STRING'],"page_id=") == 0){

推荐阅读