工大学子

php 分页 我的第一个类 (没有封装)

<?php
/*
* Created on 2012-08-23
*
* Order by happig
*/
class Pages{
    var $records;       //总记录数
    var $page_size;     //页记录数
    var $pages;         //总页数
    var $setpage;       //跳转页数
    var $offset;        //页第一条记录数
    function __construct($records,$page_size,$setpage){
        $this->records = $records;
        $this->page_size = $page_size;
        $this->pages = ceil( $this->records / $page_size);
        $this->setpage = $setpage;
        $this->offset = $page_size * ($setpage-1);
        $this->offset = ($this->offset > $this->records)? $this->records : $this->offset;  //最大 offset 不能超过 records
        $this->str ="";
    }
    function page_list(){
        $this->str.= "共<strong>".$this->offset."/".$this->records."</strong>条记录:";
        if($this->setpage != 1 ){
            $this->str.="<a href="".$_SERVER['PHP_SELF']."?setpage=1">第一页</a> ";
            $this->str.="<a href="".$_SERVER['PHP_SELF']."?setpage=".($this->setpage-1)."">上一页</a>";
        }else {
            $this->str.=" ";
            $this->str.=" ";
        }
 
        if($this->setpage != $this->pages){
            $this->str.=" <a href="".$_SERVER['PHP_SELF']."?setpage=".($this->setpage + 1)."">下一页</a> ";
            $this->str.="<a href="".$_SERVER['PHP_SELF']."?setpage=".$this->pages."">最后一页</a>";
        }else {
            $this->str.=" ";
            $this->str.=" ";
        }
        return $this->str;
    }
}
 
header("Content-type: text/html; charset=utf-8"); 
require 'db.php';   //引入数据库连接文件,源码如下。
/*
//我的db.php文件
<?php
static $connect = null;
static $table = '';
if(!isset($connect)) {
    $connect = mysql_connect("localhost","root","");
    if(!$connect) {
        $connect = mysql_connect("localhost","Zjmainstay","");
    }
    if(!$connect) {
        die('Can not connect to database.Fatal error handle by /test/db.php');
    }
    mysql_select_db("pagelist",$connect);
    mysql_query("SET NAMES utf8",$connect);
    $conn = &$connect;
    $db = &$connect;
    $link = &$connect;
}
//End_php
//*/
if(empty($_GET['setpage'])||$_GET['setpage']<0){
    $setpage = 1;
}
else {
    $setpage = $_GET['setpage'];
}
 
$result = mysql_query('select * from v_char');
$records = mysql_num_rows($result);         //总记录数
$page_size = 5;                         //页记录数
 
$pageObj = new Pages($records,$page_size,$setpage);
$sql="SELECT * FROM v_char LIMIT {$pageObj->offset},{$page_size}";
$result = mysql_query($sql,$link);
$record_items = array();
while($row = mysql_fetch_array($result)){
    $record_items[] = $row;
}
echo $pageObj->page_list();
echo "<br><ul>";
foreach( $record_items as $item ) {
    echo "<li>".implode(',',$item)."</li>";
}
echo "</ul>";
?>
posted @ 2012-08-25 13:17  飞天小莫  阅读(206)  评论(0编辑  收藏  举报
飞天小莫