Pagination methods

I am loading a page with a table of results queried from a mySQL database.

The maximum number of records to display in the table is hardcoded although this may be stored in the database eventually.

I ascertain the number of records and calculate the number of pages and hold this in a variable.

I can pass the next page number at the end of the url and this works fine.
The First , Previous, Next, Last links also work fine.

How do I retain the number of pages needed when I call the next page? What is the proper way? Can I pass this by means of the url?

The next stage is to put a form on the page so that the user can determine which records to query. Is it good practice to pass the criteria values at the end of the url as well?

Drop down menu’s on the form are data driven, do I refresh these each time I load a new page or do I put the form on a different page?

Thanks

Taken in and good advice, thanks.

I’ve got the pagination all working now. Just starting to set up a
selection form to allow the user to query the database.

you must use as
1- count your query result to find number of pages … let it as $totalrecord = 100;
2- put limits per page ( number of results to show) … let as 10
3- so your $totalpage= 10;

use your url like http://example.com/?page=1

get the page number , multiply with limit and then make your select query with start limit and endlimit

hope it explains…

How do I retain the number of pages needed when I call the next page? What is the proper way? Can I pass this by means of the url?

Retain? You don’t, you recalculate these every page request, otherwise when a record is added or removed your pager won’t properly reflect the number of records.

Passing paging information in the URL is the most common practice, simplest to implement and should be favoured. POSTing paging data will make it less likely your pages are indexed by search engines and COOKIE will make your pager less available, such as instances where the user agent doesn’t support COOKIE’s

I’ve done this a few times recently by making use of the LIMIT function in MySQL.

The first step is to find out the total number of items. I usually write PHP function to save time so it might be called TotalArticles for example if you were dealing with news articles. Call this each time the page loads.

Take that number and divide it by the size of list you require. Remember to round up as the last page may not be full. You can then run a query such as SELECT * FROM news LIMIT 0,10. That will get 10 results starting from row 0.

That is all you need for the first page. On the links for subsequent pages I simply include a page number for example news.php?page=2.

You can then check if $_GET[‘page’] is set. If it is you just repeat everything as before but this time start from the 11th row. The starting position can be worked out as:
start = (page-1) * listsize

So putting the value of start in to you query for page two would give SELECT * FROM news LIMIT 10,10.

Some very rough psudo code might be


//10 results per page 
$newscount = TotalNews();
$totalpages = ceil($newscount/10);
//output link to all pages
for($x=1;$x<=$resultspages;$x++)
{
	echo '<a href="news.php?page='.$x.'">'.$x.'</a>';
}

//get list for current page 
$start = ($_GET['page']-1)*10;
$query = "SELECT * FROM news LIMIT $start,10";