ホームページ  >  記事  >  php教程  >  php mysqlデータベース操作ページングクラス

php mysqlデータベース操作ページングクラス

WBOY
WBOYオリジナル
2016-06-13 12:28:55852ブラウズ

コードをコピー コードは次のとおりです:


/*
* mysql数据库 分页类
* @package pagelist
* @author yytcpt(無影)
* @version 2008-03 -27
* @copyrigth http://www.d5s.cn/
*/
/*
* 分页样式
.page{float: left;font: 11px Arial, Helvetica 、サンセリフ。 パディング:6px 0; マージン: 0px 10%; margin-top: 10px;}
.page a, .page strong{padding: 2px 6px; ボーダー: 実線 1px #ddd;    背景: #fff; text-decoration: none;}
.page a:visited{padding: 2px 6px; ボーダー: 実線 1px #ddd; 背景: #fff; text-decoration: none;}
.page .break{padding: 2px 6px; 境界線: なし。 背景: #fff; text-decoration: none;}
.page strong{padding: 2px 6px; 境界線の色: #999; フォントの太さ: 太字; フォントサイズ: 13px; 垂直配置: 上; 背景: #fff;}
.page a:hover{color: #fff; 背景: #0063DC; 境界線の色: #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] = "首页";
$this->img_btn[1] = "上一页";
$this->img_btn[2] = "下一页";
$this->img_btn[3] = "末页";
}
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 = "& lt;strong>".$this-> img_btn[0]."gt; ".$this->img_btn[1]."";
}else{
$str = "".$this->img_btn[0]."< ;/a> ";    //此处1は首页、page值は1
$str.= "
".$this->img_btn[1]."";
}
return $str;
{ t;".$this-> ;img_btn[2]."gt; ".$this->img_btn[3]."";
}else{
$str = "".$this->img_btn [2]." ";
$str.= "".$this->img_btn[3]."";
}
return $str;
}
function show_num_text(){
$str = " 转到第
$str.= "
url."' document.getElementById('go_num_text').value;" style=' font-family: Arial, Helvetica, sans-serif;font-weight:bold;font-size:14px;'>[Go]";
return $str;
}
function show_num_select(){
if ($this->total_pages $str = " ";
}else{
$str = "";
}
return $str;
}
function show_num_btn(){
if ($this->page>=1 and $this->page<=$this->total_pages){ $tmp_p = ( $this->num_btn-1)/2;
if (($this->page - $tmp_p)<=0){
$start_p = 1;
}else{
if (($this->page-$tmp_p)>$this->num_btn and ($this->page-$tmp_p)>($this->total_pages - $this ->num_btn 1)){
$start_p = $this->total_pages - $this->num_btn 1;
}else{
$start_p = $this->page - $tmp_p;
}

if (($this->page $tmp_p) < $this->total_pages){
$end_p = ($this->ページ $tmp_p)< ;$this->num_btn?$this->num_btn:($this->ページ $tmp_p);
if ($end_p>$this->total_pages){
$end_p = $this->total_pages;
}
}else{
$end_p = $this->total_pages;

}
$str = "";
for ($i=$start_p; $i<=$end_p; $i ){
if ($i==$this->page){
$str.= " ;".$i." ";
else{
$str.= " ".$i." ";

}
return $str;
}
function show_page_info(){
$str = " 共".$this->total_records."条/".$this->total_pages."页";
return $str;
}
function show_page(){
if ($this->total_records $this->set_show_page();
}
$str = $this->total_pages>1 ? $this->show_first_prv().$this->show_num_btn().$this->show_next_last().$this->show_page_info().$this->show_num_text():"";
return $str;
}
//总页数
function set_total_pages(){
$this->total_pages = ceil($this->total_records/$this->page_サイズ);
}
//总记录数
function set_total_records()
if ($this->total_records==0 or !isset($this->total_reコード)){
if (empty($this->count_sql) and !empty($this->table["tablename"])){
$sql = "SELECT count(".$this->table["id "].") as count_id FROM `".$this->table["tablename"]."` ".($this->table["where"]!=""?" WHERE ".$this ->テーブル["どこ"]:"");
else{
$sql = preg_replace("/SELECT(.*?)FROM(.*?)/i", "SELECT count(id) AS count_id FROM\2", $this-> SQL);

$arr = $this->db->row_query_one($sql);
$this->total_records = $arr["count_id"];
}
}
/*
* sql 返還データに基づく
* $sql を指定する、指定する必要はない
*/
関数 get_rows_by_sql($sql){
$this->sql = $sql." LIMIT ".$this->page_size*($this->page-1).",".$this->page_size;    // 指定されたSQL;
return $this->db->row_query($this->sql);
}
/
function get_rows($tablename, $where="", $orderby=""){
$this->table["tablename"] = $tablename;
$this- >table ["where"] = $where;
$orderby ? $this->table["orderby"] = $orderby : "";
$arr = array(
"ページ" => $this->show_page(), - "行" => $this->total_records ,ステートメント、$row_count 合計統計情報
*/
function get_rows_sql($sql_query, $row_count=0) ) {
$this->total_records = $row_count;
$arr["rows"] = $this->get_ rows_by_sql($sql_query); $arr["page"] 🎜> }
function get_sql(){
if ($this->total_records>10000) {
$this->sql = "SELECT ".$this->table["fileds"]. " FROM ` ".$this->table["テーブル名"]."` ".($this->table["where"]!=""?" WHERE ".$this->table[" where"] .' AND '.$this->table["id"].'>=':' WHERE '.$this->table["id"].'>='). (SELECT ' .$this->table["id"].' FROM `'.$this->table["tablename"].'` ORDER BY '.$this->table["id"] .' LIMIT '.$this->page_size*($this->page-1).', 1)'." ORDER BY ".$this->table["orderby"]." ".$ this-> ;table["descasc"]." LIMIT ".$this->page_size; ." FROM `".$this->table["テーブル名"]."` ".($this ->table["where"]!=""?" WHERE ".$this->table[ "where"]:"")." ORDER BY ".$this->table["orderby"] ." ".$this->table["descasc"]." LIMIT ".$this->page_size*($this->page-1).", ".$this->page_size; 🎜>
//SELECT * FROM articles ORDER BY id DESC LIMIT 0, 20
//SELECT * FROM articles WHERE category_id = 123 AND id >= (SELECT id FROM articles ORDER BY id LIM IT 10000、1) 制限 10
return $this->sql;        //SQL语句
}
function set_url(){
$arr_url = array();
parse_str($_SERVER["QUERY_STRING"], $arr_url);
unset($arr_url["page"]);
if (empty($arr_url)){
$str = "page=";
}else{
$str = http_build_query($arr_url)."&page=";
}
$this->url = "http://".$_SERVER["HTTP_HOST"].$_SERVER["PHP_SELF"]."?".$str;
}
}
?>


复制代代码如下:


$db_config["ホスト名"] = "127.0.0.1";    // 服务器地址
$db_config["username"] = "root";        //データベース库用户名
$db_config["パスワード"] = "root";        //データベース库密码
$db_config["database"] = "wap_blueidea_com";        //データ库名
$db_config["charset"] = "utf8"; 
$config["charset"] = "utf-8";        //网站编码

include('db.php'); 
include('pagelist.php'); 
$db = new db(); 
$db->connect($db_config); 
header("content-type:text/html;charset=".$config["charset"]);//设置页面编码
$pl = new pagelist(); 
$arr = $pl->get_rows('table_name'); 
設定解除($pl); 
echo '

';  <br> print_r($arr);  <br> echo '
'; 
//特殊 sql 時期
$pl = new pagelist(); 
$sql = 'SELECT * FROM `wap_article` AS a, `wap_article_info` AS b WHERE a.id=b.articleid'; 
$arr = $pl->get_rows_sql($sql); 
設定解除($pl); 
echo '
';  <br> print_r($arr);  <br> echo '
'; 
?> 

表内の評価総数が 10000 条の場合、子以上の評価パケットが使用され、このような効率はより高く、データ量が少ない場合には、直接評価がより高速になります。
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。