tags:

views:

24

answers:

1

the problem with the class is that if i want to display 30 records at a time and i have 60 total records the pagination divs that shows the page numbers only shows page #1 and not page #2. i have tried to figure out how to fix it but i have given up. any help would greatly be apreciated.

this is how i call attributes to the class.

$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  

here is the class.

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




}

?>  
A: 

Personally I tend to stray from SELECT * FROM tbl type queries as if you have a large table, it can be slow. Therefore, I run two queries: one to get the total number of records, and one to get the data set I need based on a paging parameter in the URL, e.g. example.com/news.php?page=2.

From there, I can instantiate my own paging class, which is pretty simple:

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

Then to use:

<?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();

Feel free to use the above code and modify to your needs

Martin Bean