betway必威-betway必威官方网站
做最好的网站

【betway必威官方网站】mysql数据库操作分页类,附

主导逻辑思路和.net的同风流倜傥,正是将透超过实际体类来拓宽配置换到了经过数组实行配备,逻辑比较轻巧,依据准则推断拼接分页html。

复制代码 代码如下:

有以下多少个大约的功力:

/*
 *    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;        //Logo路径
        var $img;            //Logo名称
        var $img_btn;        //Logo地址
        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_path.$this->img[0]."' alt='首页' border='0' align='absmiddle'/>";
            $this->img_btn[1]    = "img_path.$this->img[1]."' alt='上一页' border='0' align='absmiddle'/>";
            $this->img_btn[2]    = "img_path.$this->img[2]."' alt='下一页' border='0' align='absmiddle'/>";
            $this->img_btn[3]    = "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_recordspage_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 = "".$this->img_btn[0]." ".$this->img_btn[1]."";
            }else{
                $str = "url."1"."'>".$this->img_btn[0]." ";    //此处1为首页,page值为1
                $str.= "url.($this->page-1)."'>".$this->img_btn[1]."";
            }
            return $str;
        }
        function show_next_last(){
            if ($this->page>=$this->total_pages){
                $str =  "".$this->img_btn[2]." ".$this->img_btn[3]."";
            }else{
                $str = "url.($this->page 1)."'>".$this->img_btn[2]." ";
                $str.= "url.$this->total_pages."'>".$this->img_btn[3]."";
            }
            return $str;
        }
        function show_num_text(){
            $str = " 转到第 page."' style='border:0;border-bottom:1px solid #CCC;text-align:center;width:20px;'/> 页 ";
            $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<50){
                $str = "";
                for ($i=1; $i<=$this->total_pages; $i ){
                    $str.= "url.$i."' ".($this->page==$i ? " selected='selected'":"").">".$i."";
                }
                $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) total_pages){
                    $end_p = ($this->page   $tmp_p)num_btn?$this->num_btn:($this->page   $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.= " url.$i."'>".$i." ";
                }
            }
            return $str;
        }
        function show_page_info(){
            $str = " 共".$this->total_records."条/".$this->total_pages."页";
            return $str;
        }
        function show_page(){
            if ($this->total_records<1){
                $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_size);
        }
        //总记录数
        function set_total_records(){
            if ($this->total_records==0 or !isset($this->total_records)){
                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->table["where"]:"");
                }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时,不必钦命limit
         */
        function 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);
        }
        /*
         * 最常用的分页方法,只须求传3个参数
         * $tablename 表名, $where 查询条件, $orderby 排序字段(暗中同意以id倒序排列)
         */
        function get_rows($tablename, $where="", $orderby=""){
            $this->table["tablename"]    = $tablename;
            $this->table["where"]        = $where;
            $orderby ? $this->table[betway必威官方网站,"orderby"] = $orderby : "";
            $arr = array(
                "page"    => $this->show_page(卡塔尔(قطر‎,            //分页代码
                "rows"    => $this->get_rows_by_sql(),    //记录数
                "sum"    => $this->total_records,        //总记录数
            );
            return $arr;
        }
        /*
         * 特殊查询,$sql_query 查询sql语句, $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"]    = $this->show_page();
            $arr["sum"]        = $this->total_records;
            return $arr;
        }
        function get_sql(){
            if ($this->total_records>10000) {
                $this->sql = "SELECT ".$this->table["fileds"]." FROM `".$this->table["tablename"]."` ".($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;
            }else{
                $this->sql = "SELECT ".$this->table["fileds"]." FROM `".$this->table["tablename"]."` ".($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 LIMIT 10000, 1) LIMIT 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 = ";
        }
    }
?>

1:帮忙相关开关的显示与否配置
2:帮衬每页数目,文本名称,html标签类名称的跋扈布置
3:支持url重写过的页面(需和煦在布署数组中加多重写法规)

复制代码 代码如下:

**简单吗,照旧一直上代码:

    $db_config["hostname"]    = "127.0.0.1";    //服务器地址 
    $db_config["username"]    = "root";        //数据库顾客名 
    $db_config["password"]    = "root";        //数据库密码 
    $db_config["database"]    = "wap_blueidea_com";        //数据库名称 
    $db_config["charset"]    = "utf8"; 
    $config["charset"]        = "utf-8";        //网站编码 

**核心代码:pager.class.php
复制代码 代码如下:

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

class pager{
//分页的参数配置
private $config=array(
//首页开关的文本文字
"first_btn_text"=>"首页",
//上生机勃勃页按键的文书文字,
"pre_btn_text"=>"上一页",
//下风流倜傥页的公文文字
"next_btn_text"=>"下一页",
//最后后生可畏页的文本文字,
"last_btn_text"=>"末页",
//总记录数 *必需
"record_count"=>0,
//每页分页尺寸
"pager_size"=>10,
//当前页码 *必需
"pager_index"=>1,
//每页呈现的最大额开关
"max_show_page_size"=>10,
//页码在浏览器中传值的称号 默感到page
"querystring_name"=>"page",
//UPAJEROL是不是重写 默以为flase
"enable_urlrewriting"=>false,
//url重写准绳 比如page/{page} 个中{page}就是意味着页数
"urlrewrite_pattern"=>"",
//分页容器的css名称
"classname"=>"paginator",
//当前页按键的class名称
"current_btn_class"=>"cpb",
//分页文字描述span标签的css
"span_text_class"=>"stc",
/*跳转的详细文本
*totle代表总页数,
*size代表每页数目
* goto代表要跳转的输入框
* record代表总记录数
* index代表当前的页码
*/
"jump_info_text"=>"共{totle}页,每页{size}条记录,跳转到{goto}页",
//跳转开关的文件
"jump_btn_text"=>"确定",
//是或不是出示跳转
"show_jump"=>false,
//是不是出示后边的按键 首页&上意气风发页
"show_front_btn"=>true,
//是不是出示前边的按键 下风流倜傥页&末页
"show_last_btn"=>true
);
/*
* 类的构造函数
* $config:该分页类的安排
*/
public function __construct($config)
{
$this->init_config($config);
}
function __destruct()
{
unset($this->config);
}
/*
* 布局分页主函数
*/
public function builder_pager()
{
//分页的字符串
$pager_arr=array();
//每页的尺寸
$pager_size=$this->config["pager_size"];
//得到生机勃勃共的分页数目
$pager_num=$this->config["record_count"]%$pager_size==0?$this->config["record_count"]/$pager_size:floor($this->config["record_count"]/$pager_size) 1;
//当前的页码序号 如若是0,则置为1
$pager_index=round($this->config["pager_index"])==0?1:round($this->config["pager_index"]);
//如若当前的页码大于等于最生平机勃勃页,则当前的页码置为最终风流罗曼蒂克页
$pager_index=$pager_index>=$pager_num?$pager_num:$pager_index;
//下后生可畏页的页码
$pager_next=$pager_index>=$pager_num?$pager_num:($pager_index 1);
//获取须求跳转 的url
$url=$this->get_url();
//增加开班的div
$classname=$this->config["classname"];
$pager_arr[]="n";
//增加前边几个开关的html
if($this->config["show_front_btn"])
{
//假设当前的页码为1 则front那七个按键则会被剥夺
$attr=$pager_index==1?"disabled=disabled":"";
$pager_arr[]=$this->get_a_html(self::format_url($url,1),$this->config["first_btn_text"],$attr);
$pager_arr[]=$this->get_a_html(self::format_url($url,$pager_index-1),$this->config["pre_btn_text"],$attr);
}
//当前展现页码的前奏 1~10 1 11~20 11
$current_pager_start=$pager_index%$pager_size==0?($pager_index/$pager_size-1)*$pager_size 1:floor($pager_index/$pager_size)*$pager_size 1;
//当前来得页码的竣事
$current_pager_end=($current_pager_start $pager_size-1)>=$pager_num?$pager_num:($current_pager_start $pager_size-1);
//增加跳转到上意气风发层的html
if($pager_index>$pager_size)
{
$pager_arr[]=$this->get_a_html(self::format_url($url,$current_pager_start-1),"...");
}
//主体页码部分
for($i=$current_pager_start;$i<=$current_pager_end;$i )
{
if($i!=$pager_index)
{
$pager_arr[]=$this->get_a_html(self::format_url($url,$i),$i);
}else{
//假使这些是当前页
$pager_arr[]=$this->get_span_html($i,$this->config["current_btn_class"]);
}
}
//增添下风度翩翩层的html
if($pager_index<=($pager_num-$pager_num%$pager_size))
{
$pager_arr[]=$this->get_a_html(self::format_url($url,$current_pager_end 1),"...");
}
//增多前面四个按键的html
if($this->config["show_last_btn"])
{
//固然当前的页码为最后生机勃勃页 则last那五个开关则会被剥夺
$attr=$pager_index>=$pager_num?"disabled=disabled":"";
$pager_arr[]=$this->get_a_html(self::format_url($url,$pager_next),$this->config["next_btn_text"],$attr);
$pager_arr[]=$this->get_a_html(self::format_url($url,$pager_num),$this->config["last_btn_text"],$attr);
}
//增加跳转的html
if($this->config["show_jump"])
{
$patterns=array("/{totle}/","/{size}/","/{goto}/","/{record}/","/{index}/",);
$replacements=array(
$pager_num,
$pager_size,
"n",
$this->config["record_count"],
$this->config["pager_index"]
);
//替换特定的标签组成跳转
$pager_arr[]=preg_replace($patterns,$replacements,$this->config["jump_info_text"]);
$btn_text=$this->config['jump_btn_text'];
$pager_arr[]="".$this->config['jump_btn_text'].";")n";
$pager_arr[]=$this->get_jumpscript($url);
}
$pager_arr[]="<>";
$this->config["pager_index"]=$pager_index;
return implode($pager_arr);
}
/*
*收获须求管理的url,扶持重写配置,种种参数的url
*/
private function get_url()
{
//假设设置了同意url重写
if($this->config["enable_urlrewriting"])
{
//获得调用文件所在的url
$file_path="];
//获得调用url所在的网络目录
$file_path=substr($file_path,0,strripos($file_path,"/"))."/";
//直接将引得附加重写法规 形成新的url
$url=$file_path.$this->config["urlrewrite_pattern"];
}else{
//获得当前调用页面的相对化url
$url="];
//分页参数在浏览器中传递的称呼
$querystring_name=$this->config['querystring_name'];
//假设该url中带有php?的字符串 则供给将分页参数替换
if(strpos($url,"php?"))
{
//假诺存在page=xxx的字样
$pattern="/$querystring_name=[0-9]*/";
if(preg_match($pattern,$url))
{
//将含page=***的字样中的数字替换来{0}
$url=preg_replace($pattern,"$querystring_name={page}",$url);
}else{
$url.="&$querystring_name={page}";
}
}else{
//直接附加参数产生分页的意气风发体化url
$url.="?$querystring_name={page}";
}
}
return $url;
}
/*
* 得到a标签的html
*$url:a标签所要导向的html
*$title:a标签的标题
**$attr:a标签上的附加属性 可以不写
*/
private static function get_a_html($url,$title,$attr="")
{
return "$titlen";
}
/*
* 获得span标签的html
* $num:span中的文本,即页序号
* $classname:span标签的class名称
*/
private static function get_span_html($num,$classname)
{
return "$numn";
}
/*
* 格式化url
* $url 原url
* $page 页码
*/
private static function format_url($url,$page)
{
return preg_replace("/{page}$/",$page,$url);
}
/*
*开头化分页的配置文件
*举个例子在参数中不含该键值,则暗中认可使用表达的值
*/
private function init_config($config)
{
//判别该值是不是留存、是还是不是是数组、是或不是带有记录
if(isset($config)&&is_array($config)&&count($config)>0){
foreach($config as $key=>$val)
{
$this->config[$key]=$val;
}
}
}
/*
* 构造跳转职能脚本的方式
*$url:须求跳转的额那么些url
*/
private function get_jumpscript($url)
{
$scriptstr = "

';  
    print_r($arr);  
    echo '

本文由betway必威发布于编程开发,转载请注明出处:【betway必威官方网站】mysql数据库操作分页类,附

TAG标签: betway必威
Ctrl+D 将本页面保存为书签,全面了解最新资讯,方便快捷。