Home Backend Development PHP Tutorial PHP mysql database operation paging class_PHP tutorial

PHP mysql database operation paging class_PHP tutorial

Jul 21, 2016 pm 03:52 PM
mysql php code Pagination copy operate database kind

Copy code The code is as follows:

/*
 *    mysql数据库 分页类
 *    @package    pagelist
 *    @author        yytcpt(无影)
 *    @version    2008-03-27
 *    @copyrigth    http://www.d5s.cn/ 
 */
/*
 *    分页样式
    .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_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_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 = "".$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) < $this->total_pages){
                    $end_p = ($this->page + $tmp_p)<$this->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);
        }
                                                                                                                                           /
function get_rows($tablename, $where="", $orderby=""){
$this->table["tablename"] = $tablename;
$this->table ["where"] = $where;
$orderby ? $this->table["orderby"] = $orderby : "";
$arr = array(
"page" => $this->show_page(),                                                                                                                        ‐ "rows" => $this->total_records ,                                                                                 Statement, $row_count total statistics
*/
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["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;
                                                                                                                                  ." 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 = "http://".$_SERVER["HTTP_HOST"].$_SERVER["PHP_SELF"]."?".$str;
        }
    }
?>

复制代码 代码如下:

    $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";        //网站编码 

    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'); 
    unset($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); 
    unset($pl); 
    echo '';  <br>    print_r($arr);  <br>    echo ''; 
?> 
When the total number of records in the table is more than 10,000, subquery paging is used, which will be more efficient. When the amount of data is small, direct query is faster.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/318982.htmlTechArticleCopy the code code as follows: ?php /* *mysql database paging class*@packagepagelist *@authoryytcpt(无影) *@version2008-03-27 *@copyrigthhttp://www.d5s.cn/ */ /* *Page style.page{flo...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP's Current Status: A Look at Web Development Trends PHP's Current Status: A Look at Web Development Trends Apr 13, 2025 am 12:20 AM

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP: The Foundation of Many Websites PHP: The Foundation of Many Websites Apr 13, 2025 am 12:07 AM

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

PHP's Purpose: Building Dynamic Websites PHP's Purpose: Building Dynamic Websites Apr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

See all articles