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 include('Pager.php'); //making connection to the database mysql_connect('localhost', 'root', ''); mysql_select_db('test'); //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 $pager->build(); //get paged data $rows = $pager->getPagedData(); |
Click the download link given below for a copy of the class. It also includes an working example.
15 Comments
Other Links to this Post
-
Paging using PHP and MySQL | Dream House — April 24, 2008 @ 11:30 pm
RSS feed for comments on this post. TrackBack URI
By Fahmi, April 24, 2008 @ 11:42 pm
Thanks for nice work Sajjad Bhai….
I will try to implement it in my current project…
By Arafat Rahman, April 27, 2008 @ 1:50 pm
Everyday we need pagination.
Hope this paging class may make our life easy.
By Andrew, April 3, 2009 @ 2:20 pm
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?
By Mohammad Sajjad Hossain, April 3, 2009 @ 2:39 pm
@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
$rows
variable. Now in your view file (where you are going to show the pictures) do the following:
I have not tested this code, but it should work as intended. Let me know if it has solved your problem or not.
By Andrew, April 6, 2009 @ 1:44 pm
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.
By Mohammad Sajjad Hossain, April 6, 2009 @ 1:51 pm
Hi Andrew,
You may try this meta tag (in head section)…
By Andrew, April 7, 2009 @ 10:24 am
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 🙂
By Mohammad Sajjad Hossain, April 7, 2009 @ 10:28 am
I think if you have used Unicode then that should have worked with that meta tag. Anyway, alls well that ends well 😉
By Tanvir, June 28, 2009 @ 10:29 am
Dear brother sajjadhossain it is important and necessary script. It well be very much if you make complex.
By Mohammad Sajjad Hossain, June 28, 2009 @ 11:30 am
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. 🙂
By Josh Beauregard, September 20, 2011 @ 1:17 am
Thank you it was quick and easy to implment
By Dashmesh Singh, April 18, 2014 @ 3:49 pm
Thanks for such a good work…i have a query ..this script does not work if i uses it with where caluse .Please suggest how can i pass $reg value or v_date value to second page for pagination
By Mohammad Sajjad Hossain, April 18, 2014 @ 5:03 pm
Thanks Dashmesh. I think you are talking about query string. You may use the following class instead. It is a modified version.
To build the links with query string do the following:
Here is the modified class:
By Dashmesh Singh, April 23, 2014 @ 11:48 am
thanks for the reply sir….is it not possible to pass variable with url
e.g. view_mrd.php?id=$_POST[‘id]’