views:

39

answers:

1

hi, ive written a pagination class and it looks like its not outputting the correct amount of rows. i have a table full of 51 records and instead it shows only 30 records and as for the pages it shows only page 1 where it should show page 1 and 2. the pages are viewed by going to page=1 as a browser param. when i try to view page=2 i see the rest of the records. here is the class.

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";
        }

    }

this is how i use the class:

$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/>";
}

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

A: 

In function setIndex you have to write:

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

because a query should look like:

for page 1: SELECT ... LIMIT 0,[nr_of_pages]

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

Zsolti
i did that and this is what the query came out to which gave an error. select * from tbl_content where visible = '1' order by year desc LIMIT -30, 30
sam
probably the $this->param became somehow zero
Zsolti