【问题标题】:Pagination Class calculates incorrectly分页类计算不正确
【发布时间】:2011-03-30 06:31:27
【问题描述】:

我写了一个分页类,看起来它没有输出正确数量的行。我有一个包含 51 条记录的表,但它只显示 30 条记录,而对于页面,它只显示第 1 页,它应该显示第 1 页和第 2 页。通过将 page=1 作为浏览器参数查看页面。当我尝试查看 page=2 时,我看到了其余的记录。这是课程。

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() {
            $qRows = $this->db->query($this->sql);
            $numRows = $this->db->num_rows($qRows);
            $numOfPages = ceil($numRows / $this->perPage);
            $param = $this->param;
            $pageName = $this->pageName;



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

            print "<a href='$this->pageName?page=1' 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'> $i </a>";
                    }



                    }  else {

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

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

                        }
            // ----------------------------------------------------------------------------

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

            print "</div>";
        }





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

    }

这就是我使用课程的方式:

$db = 新的 MysqlDB; $paginate = 新分页;

$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(); //显示分页div

【问题讨论】:

    标签: php sql mysql pagination


    【解决方案1】:

    在函数 setIndex 你必须写:

    return $this->start = (($this->param - 1) * $this->perPage) - $this->perPage;  
    

    因为查询应该如下所示:

    对于第 1 页:SELECT ... LIMIT 0,[nr_of_pages]

    对于第 2 页:SELECT ... LIMIT [nr_of_pages]*1,[nr_of_pages] ...

    【讨论】:

    • 我这样做了,这就是查询出来的结果,它给出了错误。 select * from tbl_content where visible = '1' order by year desc LIMIT -30, 30
    • 可能 $this->param 变为零了
    猜你喜欢
    • 2012-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多