用PHP操作MySql数据库(分页)

网络整理 - 08-17

以下为引用的内容:
<?php
/*
* mysql数据库 分页类
* @package pagelist
* @author yytcpt(无影)
* @version 2008-03-27
* @copyrigth  
*/
/*
* 分页样式
.page{float: left;font: 11px Arial, Helvetica, sans-serif; padding:6px 0; margin: 0px 10%; margin-top: 10px;}
.page a, .page strong{padding: 2px 6px; border: solid 1px #ddd; background: #fff; text-decoration: none;}
.page a:visited{padding: 2px 6px; border: solid 1px #ddd; background: #fff; text-decoration: none;}
.page .break{padding: 2px 6px; border: none; background: #fff; text-decoration: none;}
.page strong{padding: 2px 6px; border-color: #999; font-weight: bold; font-size: 13px; vertical-align: top; background: #fff;}
.page a:hover{color: #fff; background: #0063DC; border-color: #036; text-decoration: none;}
.page a:hover div{color: #FFF;cursor: pointer !important;cursor: hand;}
*/
class pagelist{
var page; //分页页码
var sql; //分页sql
var img_path; //图标路径
var img; //图标名称
var img_btn; //图标地址
var page_size; //设置每页显示条数
var num_btn; //设置数字分页的显示个数
var total_pages; //一共分多少页
var total_records; //一共有多少条记录
var url;
var table;
var new_sql; //指定的SQL语句
var db;
function __construct(){
global db;
this->db = db;
tmp_page = intval(trim(_GET["page"]));
this->page = empty(tmp_page)?1:tmp_page;
this->set_table();
this->page_size = 20;
this->num_btn = 9;
this->img_path = ’/images/’;
this->img = array("ico_first.gif", "ico_front.gif", "ico_next.gif", "ico_last.gif");
}
function set_table(){
this->table["tablename"] = "";
this->table["id"] = "id";
this->table["orderby"] = this->table["id"];
this->table["descasc"] = "DESC";
this->table["fileds"] = "*";
this->table["where"] = "";
}
function set_img(){
this->img_btn[0] = "<img src=’".this->img_path.this->img[0]."’ alt=’首页’ border=’0’ align=’absmiddle’/>";
this->img_btn[1] = "<img src=’".this->img_path.this->img[1]."’ alt=’上一页’ border=’0’ align=’absmiddle’/>";
this->img_btn[2] = "<img src=’".this->img_path.this->img[2]."’ alt=’下一页’ border=’0’ align=’absmiddle’/>";
this->img_btn[3] = "<img src=’".this->img_path.this->img[3]."’ alt=’末页’ border=’0’ align=’absmiddle’/>";
}
function set_show_page(){
this->set_img(); //设置翻页图片路径
this->set_url();
this->set_total_records();
if (this->total_records<this->page_size){
this->total_pages = 1;
}else{
this->total_pages = ceil(this->total_records/this->page_size);
}
if (this->page>this->total_pages){
this->page = this->total_pages;
}
}
function show_first_prv(){
if (this->page==1){
str = "<strong>".this->img_btn[0]."</strong> <strong>".this->img_btn[1]."</strong>";
}else{
str = "<a href=’".this->url."1"."’>".this->img_btn[0]."</a> "; //此处1为首页,page值为1
str.= "<a href=’".this->url.(this->page-1)."’>".this->img_btn[1]."</a>";
}
return str;
}
function show_next_last(){
if (this->page>=this->total_pages){
str = "<strong>".this->img_btn[2]."</strong> <strong>".this->img_btn[3]."</strong>";
}else{
str = "<a href=’".this->url.(this->page+1)."’>".this->img_btn[2]."</a> ";
str.= "<a href=’".this->url.this->total_pages."’>".this->img_btn[3]."</a>";
}
return str;
}
function show_num_text(){
str = " 转到第 <input id=’go_num_text’ type=’text’ value=’".this->page."’ style=’border:0;border-bottom:1px solid #CCC;text-align:center;width:20px;’/> 页 ";
str.= "<a href=’#’ onClick=\"window.location=’".this->url."’+document.getElementById(’go_num_text’).value;\" style=’font-family: Arial, Helvetica, sans-serif;font-weight:bold;font-size:14px;’>[Go]</a>";