ajax pagination in php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bhanu
Forum Commoner
Posts: 46
Joined: Thu Nov 05, 2009 4:25 am

ajax pagination in php

Post 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
Attachments
ajax paging.rar
ajax pagination
(19.2 KiB) Downloaded 8 times
User avatar
iankent
Forum Contributor
Posts: 333
Joined: Mon Nov 16, 2009 4:23 pm
Location: Wales, United Kingdom

Re: ajax pagination in php

Post 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
Post Reply