English 中文(简体)
出生等级计算错误
原标题:Pagination Class calculates incorrectly

撰写假歌,认为它不会产生正确的增长。 i 有一个有51个记录的表格,而只显示30个记录,而只有第1页的页,应显示第1和第2页。 页: 1 i 在试图查看网页=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 = new MysqlDB; $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/>";
}

gin ;

问题回答

职能组合 页: 1

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

因为要问:

页: 1 ......LiMIT 0, [nr_of_pages]

for page 2: SELECT ... LIMIT [nr_of_pages]*1,[nr_of_pages] ...





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签