Paging Class using PHP and MySQL

Based on my last post on paging using PHP and MySQL, I have coded this class. It is very easy to implement and it will save your time. Here is a code snippet to show how simple to use it.

//include the class file
//making connection to the database
mysql_connect('localhost', 'root', '');
//prepare SQL
$sql = 'SELECT * FROM books';
//create an object of Pager passing the SQL
$pager = new Pager($sql);
//set the url. this is the current page
$pager->url = 'index.php';
//set number of rows. by default it is 10
$pager->rowPerPage = 5;
//build the pager
//get paged data
$rows = $pager->getPagedData();

Click the download link given below for a copy of the class. It also includes an working example.

download Paging Class using PHP and MySQL

share save 120 16 Paging Class using PHP and MySQL

12 thoughts on “Paging Class using PHP and MySQL

  1. Hello,
    In the internet I found yours php paging script.
    Unfortunatly I am new in php, and I’d like to make a pics gallery 2×2, with paging, but couldn’t make a edit in yours script that could show 2 pics in line and 2 pics in colum.
    If not so hard may bee you could help me?

  2. @Andrew
    Actually it depends on how you show/present the rows that you get using the script. Let me give an example based on your requirement.

    Suppose you have used my script/class, provided row per page 4 and the result is in


    variable. Now in your view file (where you are going to show the pictures) do the following:

    <table width="100%">
        $columNo = 1;
        foreach($rows as $row)
            if($columnNo == 1) //first column
                echo '<tr>';
            echo '<td><img src="' . $row['image_path'] . '" alt="" /></td>';
            if($columnNo > 2) //as you want to show 2 pictures in one line
                echo '</tr>';
                $columnNo = 1;
        //in case if the page has 3 photos
        if($columnNo == 2)
           echo '<td></td></tr>';

    I have not tested this code, but it should work as intended. Let me know if it has solved your problem or not.

  3. Hello,
    thank you very much it helps me and of course it works :)
    And the last one question are:

    in Pager.php file you was wrote about which items now are showing:

    return ‘Showing ‘ . $rangeStart . ‘ – ‘ . $rangeEnd . ‘ of ‘ . $this->totalRecords;

    But I prefered to translate in my native language, and I want to use utf-8 encoding, but I don’t know where I could this encoding change, because when I try to write char in the internet page I saw a symbol (unknown char), but not a char. May bee it is not so difficult to change it?

    Thank you again very much.

  4. Yes, this line I am add in html document, but it doesn’t work,
    but when I change encoding to Baltic it works, but didn’t propertlyshown other letters.
    But I found way of this situation, I have enter an ASCII code and it works great :)

  5. Dear brother sajjadhossain it is important and necessary script. It well be very much if you make complex.

  6. Thank you Tanvir bhai.

    Actually its a base. I assume “complex” means more customizable. You may try to add some new functions to it to make it more customizable. If you do so, please do not forget to share those with us. :)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>