Page 1 of 1

ajax pagination in php

Posted: Thu Nov 19, 2009 5:10 am
by bhanu
hi
i want to display 10 records per page. im using php pagination in database query

Code: Select all

 select * from $table_name order by $order_by limit $start,$per_page
 
i want to display results using ajax.for that please check the attachment i was added.

i want to dispaly pagination top and bottom for the records like


< 1 2 3 4 5 6>
sl.no name city
1 aaa ss
2 aa... f..
3 fdf ddfs


< 1 2 3 4 5 6>

please help me.

thank you in advance

Re: ajax pagination in php

Posted: Thu Nov 19, 2009 8:00 am
by iankent
First off you'll need to find out the total number of items (so you know how many pages there are). Something like this:

Code: Select all

 
$q = "select count(somecolumn) as total from $table_name;";
$r = mysql_query($q);
$l = mysql_fetch_assoc($r);
$total = $l['total'];
 
// Gets the number of pages (always rounded down)
$total_pages = floor($total / $per_page);
// Checks if there should be an extra page added ($total_pages is only correct if $total is a perfect multiple of $per_page (i.e., $total=100 and $per_page = 10)
$total_pages = ($total % $per_page > 0) ? $total_pages + 1 : $total_pages;
 
If you want to display the results using AJAX then you could return only the total results from PHP and handle the rest of the calculations in Javascript, but either way you must retrieve the total using SQL

Once you know how many pages there are, which page you're on, and how many there are per page, you can then output the page numbers somewhere (whether thats in javascript or php is up to you)

hth