【问题标题】:pagination class not showing correct amt of pages分页类未显示正确的页面数量
【发布时间】:2010-08-25 18:47:51
【问题描述】:

这个类的问题是,如果我想一次显示 30 条记录并且我总共有 60 条记录,那么显示页码的分页 div 只显示第 1 页而不是第 2 页。我试图弄清楚如何解决它,但我已经放弃了。任何帮助将不胜感激。

这就是我调用类属性的方式。

$paginate = new Pagination;
$paginate->pageName = "index.php";  //sets the page to use
$paginate->perPage = 10; //show num of records per page
$paginate->adjacents = 3; //current page adjacent to 
$paginate->sql = "select * from tbl_products"; //the main query
$query = $db->query($paginate->getData());

while($row = mysql_fetch_object($query)) {
  print $row->pName."<br/>";
}


$paginate->showPagination(); //shows the pagination div  

这是课程。

<?php

include_once("class.db.php");

class Pagination 
{

    var $param;
    var $perPage;
    var $adjacents;
    var $start;
    var $sql;
    var $pageName;


     function __construct() 
    {
        $this->db = new  MysqlDB;
        $this->db->connect();
     }

    function setParam() 
    {

        if(isset($_GET['page']) && is_numeric($_GET['page']) && ($_GET['page'] > 0)) 
        {
            $this->param = $_GET['page'];
        } 

        else 
        {
            $this->param = 1;
        }
    }


        function setIndex()
        {
            $this->setParam();
            return $this->start = ($this->param * $this->perPage) - $this->perPage;
        }

        function showPagination($param1=null,$param2=null,$param3=null) 
        {
            $qRows = $this->db->query($this->sql);
            $numRows = $this->db->num_rows($qRows);
            $numOfPages = ceil($numRows / $this->perPage);
            $param = $this->param;
            $pageName = $this->pageName;
            $string = "";

            //set pagination parameters.
            if($param1 != null) 
            {
                if(isset($_GET[$param1])) 
                {
                    $param1Value = $_GET[$param1];
                    $string .= "&".$param1."=".$param1Value;
                }
            }


            if($param2 != null) 
            {
                if(isset($_GET[$param2])) 
                {
                    $param2Value = $_GET[$param2];
                    $string .= "&".$param2."=".$param2Value;
                }
            }


            if($param3 != null) 
            {
                if(isset($_GET[$param3])) 
                {
                    $param3Value = $_GET[$param3];
                    $string .= "&".$param3."=".$param3Value;
                }
            }




            print "<div class='pagination'>";

            print "<a href='$this->pageName?page=1$string' class='previous-off'> First </a>";


            // ----------------------------------------------------------------------------        
                // PRINT ALL PAGES TO THE LEFT //
                if(($param - $this->adjacents) > 1) 
                {
                    print "<span>...</span>";

                    $lowerLimit = $param - $this->adjacents;

                    //print all on left side.
                    for($i = $lowerLimit; $i< $param; $i++) 
                    {
                        print "<a href='$pageName?page=$param = $i$string'> $i </a>";
                    }

                    }  

                    else 
                    {

                        //print all numbers between current page and  first page.

                        for($i = 1; $i < $param; $i++) 
                        {
                            print "<a href='$pageName?page=$i$string'> $i </a>";
                        }
                    }
            // ----------------------------------------------------------------------------



            //print current page
            if(($param != 0) && ($param != $numOfPages)) 
            {
                print "<span class='current'>$param</span>";
            }




            // ----------------------------------------------------------------------------            
                        //PRINT ALL PAGES TO THE RIGHT
                if(($param + $this->adjacents) < $numOfPages) 
                {

                    $upperLimit = $param + $this->adjacents;

                    for($i=($param + 1); $i<=$upperLimit; $i++) 
                    {
                        print "<a href='$pageName?page=$i$string'> $i </a>";
                    }
                        print "<span>...</span>";
                    } 
                    else 
                    {

                        //print all page numbers if out of padded range

                        for($i = ($param + 1); $i<$numOfPages; $i++ ) 
                        {
                            print "<a href='$pageName?page=$i$string'> $i </a>";
                        }

                    }

            // ----------------------------------------------------------------------------

            $lastPage = $numOfPages - 1;
            print "<a class='next' href='$pageName?page=$lastPage$string'> Last </li>";

            print "</div>";
        }





        function getData() 
        {
            $query = $this->sql;
            $this->start = $this->setIndex();
            return "$query LIMIT $this->start, $this->perPage";
        }



        function errors() 
        {
            $query = $this->sql;
            print "$query LIMIT $this->start, $this->perPage"."<br/>";
            print "this->start ".$this->start."<br/>";
            print "this->param ".$this->param."<br/>";
            print "this->perPage ".$this->perPage."<br/>";
            print "this->setindex() ".$this->setIndex()."<br/>";
        }




}

?>  

【问题讨论】:

    标签: php pagination


    【解决方案1】:

    我个人倾向于偏离SELECT * FROM tbl 类型的查询,就好像你有一个大表一样,它可能会很慢。因此,我运行了两个查询:一个是获取记录总数,另一个是根据 URL 中的分页参数获取我需要的数据集,例如example.com/news.php?page=2.

    从那里,我可以实例化我自己的分页类,这非常简单:

    <?php
    class Paging {
    
        private $total;
        private $batch;
        private $page;
        private $prefix;
    
        public function __construct($total=0, $batch=10, $page=1, $url="") {
            $this->total = intval($total);
            $this->batch = intval($batch);
            $this->page  = intval($page);
            $this->url   = $url;
        }
    
        public function total_pages() {
            return ceil($this->total/$this->batch);
        }
    
        public function render() {
            $html = "";
            // only display paging if we have more than the batch value
            if ($this->total > $this->batch) {
                $html.= '<p>Go to page:';
                for ($i=1; $i <= $this->total_pages()) {
                    if ($i==$this->page) {
                        $html.= ' <span class="current">'.$i.'</span>';
                    }
                    else {
                        $html.= ' <a href="'.sprintf($this->url, $i).'">'.$i.'</a>';
                    }
                }
                $html.= '</p>';
            }
            return $html;
        }
    }
    

    然后使用:

    <?php
    // get total number of records
    $sql = "SELECT COUNT(*) AS total FROM tbl";
    $res = $db->query($sql);
    $row = $res->fetch();
    $total = $row['total'];
    $batch = 20;
    $page  = intval($_GET['page']);
    
    // instantiate paging class
    require_once('paging.class.php');
    $paging = new Paging($total, $batch, $page, "mypage.php?page=%s");
    
    // do normal page stuff here...
    
    // render the pagination links
    echo $paging->render();
    

    请随意使用以上代码并根据您的需要进行修改

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-30
      • 2016-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-26
      相关资源
      最近更新 更多