Page 1 of 1

paginization of my mysql records

Posted: Mon Jul 26, 2010 8:28 am
by krsm1980
hello forum,
I m having problem in limiting the no. of mysql data displayed on a single page.May anyone plz tell me the effidient way to do that.
Thanks

Re: paginization of my mysql records

Posted: Mon Jul 26, 2010 10:37 am
by websitesca
Yes, pagination of MySql records is an easy thing to do with MySql.

First, you want to use the LIMIT clause on your mysql query. You will have something like:

Code: Select all

select * from customer LIMIT 20, 10
See the "LIMIT 20, 10" part? That is what you want. When you do this query, mysql will give you back 10 records, starting at record 20. Then you just print out your records like normal... something like this:

Code: Select all

$result = mysql_query($sql, $db);
while ($row = mysql_fetch_array($result))
{
  print_r($row);
}
To make this into PAGES, like page 1, 2, 3 etc. then you will need to know how make records you want to put on each page. Then in order to get the numbers to plug into our LIMIT clause we need the following formula. Lets say you want 9 records per page and you're on page 3 (for example):

Code: Select all

$page_num = 3;
$rows_per_page = 9;
$index = $rows_per_page * ($page_number - 1);
$sql = "select * from customer LIMIT $index, $rows_per_page";
Keep in mind that $page_num starts at 1. This is important.

Then you can create NEXT and PREVIOUS links passing the $page_num variable in the URL:

[text]<a href="page.php?page_num=2">PREVIOUS</a>
<a href="page.php?page_num=4">NEXT</a>[/text]

Usually, your webpage layout or design will have these links at the bottom.

Dig it?

Hope that helps with your website!

Georges,
http://www.websites.ca