Boolean Island !

Pagination By php & mysql

Posted by: ranacse05 on: April 3, 2008

Just yesterday i was finding a solution to view data. Actually its called “Pagination” .I have so many row/entity in a table and i want to show 10/5 entity per page then what i have to do ? I just design a algo for this using php and mysql but its very easy and a little bit long so i made a post on phpxperts and Aman Bhai ans me about it.Here it is..

1. Obtain the required page number

This code will obtain the required page number from the $_GET array. Note that if it is not present it will default to 1.

if (isset($_GET['pageno'])) {
$pageno = $_GET['pageno'];
} else {
$pageno = 1;
} // if

2. Identify how many database rows are available

This code will count how many rows will satisfy the current query.

$query = “SELECT count(*) FROM table WHERE …”;
$result = mysql_query($query, $db) or trigger_error(“SQL”, E_USER_ERROR);
$query_data = mysql_fetch_row($result);
$numrows = $query_data[0];

3. Calculate number of $lastpage

This code uses the values in $rows_per_page and $numrows in order to identify the number of the last page.

$rows_per_page = 15;
$lastpage = ceil($numrows/$rows_per_page);

4. Ensure that $pageno is within range

This code checks that the value of $pageno is an integer between 1 and $lastpage.

$pageno = (int)$pageno;
if ($pageno > $lastpage) {
$pageno = $lastpage;
} // if
if ($pageno < 1) {
$pageno = 1;
} // if

5. Construct LIMIT clause

This code will construct the LIMIT clause for the sql SELECT statement.

$limit = ‘LIMIT ‘ .($pageno1) * $rows_per_page .‘,’ .$rows_per_page;

6. Issue the database query

Now we can issue the database qery and process the result.

$query = “SELECT * FROM table $limit;
$result = mysql_query($query, $db) or trigger_error(“SQL”, E_USER_ERROR);
… process contents of $result …

7. Construct pagination hyperlinks

Finally we must construct the hyperlinks which will allow the user to select other pages. We will start with the links for any previous pages.

if ($pageno == 1) {
echo ” FIRST PREV “;
} else {
echo ” <a href=’{$_SERVER['PHP_SELF']}?pageno=1′>FIRST</a> “;
$prevpage = $pageno-1;
echo ” <a href=’{$_SERVER['PHP_SELF']}?pageno=$prevpage‘>PREV</a> “;
} // if

Next we inform the user of his current position in the sequence of available pages.

echo ” ( Page $pageno of $lastpage ) “;

This code will provide the links for any following pages.

if ($pageno == $lastpage) {
echo ” NEXT LAST “;
} else {
$nextpage = $pageno+1;
echo ” <a href=’{$_SERVER['PHP_SELF']}?pageno=$nextpage‘>NEXT</a> “;
echo ” <a href=’{$_SERVER['PHP_SELF']}?pageno=$lastpage‘>LAST</a> “;
} // if

But the main thing is its same algo which one i designed. :)

Note : Above tutorial is collected from this link .

4 Responses to "Pagination By php & mysql"

Don’t forget to protect against any potential injection, and to sanitize your input.

Nice post… Very structurally organized. Booked marked it for my reference :)

@Aman
Thanks for ur such a nice comment.Very glad to see ur comment on my blog:) i’m a big fan of urs.I heard about u from H2.

Great post.
Was wondering how I would be able to actually display the different page numbers —
thanks again!

Leave a Reply

My blog worth


My site is worth $3431.
How much is yours worth?

My Twittes

Error: Twitter did not respond. Please wait a few minutes and refresh this page.

FireFox

Firefox 3

My Facebook

this is Raquibul's profile

View Raquibul Islam's profile on LinkedIn

My fav

 

April 2008
S M T W T F S
« Mar   May »
 12345
6789101112
13141516171819
20212223242526
27282930  

Flickr Photos

Tea !

History

me

me

ramsagor

me,monga,mim

me,monga,mim

me,monga,mim

More Photos

My Bookmarks

Dynamic Malloc

Blog Stats

  • 36,018 hits