Welcome to WebmasterWorld Guest from 54.158.65.139

Forum Moderators: coopster & jatar k

How to create multiple pages displaying results

   
2:39 pm on Jan 23, 2006 (gmt 0)

5+ Year Member



This is much used but i dont know how:

When you have 4 example 300 results there will be a link wich creates someting like page1 page 2 page 3 page 4 page 5 page 6 and the next and back buttons howto:?

2:55 pm on Jan 23, 2006 (gmt 0)

10+ Year Member



There are a lot of pagination scripts out there.. I've used a number of canned ones, but lately I use a good one that I found and almost completely rewrote to suit my needs.

In a nutshell, you need to set a variable $limit to limit the query in question to a given number of rows, and use a $_GET variable $page to determine where you are at in the result set (what "page")

Make your display so that the appropriate links display. Do a little math and you can display only the next 5 or 6 and/or the previous 5 or 6 "pages"

One pitfall is that if your display is dependant on a $_POST to a form, you need to modify the script so that you get the same results with a $_GET. So your initial page will show the first x number of rows from a POST to a form, e.g. and the subsequent pages will use GET - e.g. example.com/widgetsearch.php?query=$this&page=$page

2:58 pm on Jan 23, 2006 (gmt 0)

5+ Year Member



Stil i dont see much script here becouse i dont know how it works anyways i use get on my forms when they get submitted i use a header to get the vars away and stuff (its easyer to create some standard hyperlinks that way)
2:59 pm on Jan 23, 2006 (gmt 0)

5+ Year Member



hmm y ou mean something like

limit 0-50
if rows is >50 display link to page 2
if rows is >100 display etc.?

5:17 pm on Jan 23, 2006 (gmt 0)

10+ Year Member



Well first you need to know how many rows are in the result set. $totalrows

Then you need to know how many rows you want to display per page... $limit

Then $totalrows / $limit should give you the total number of pages

I usually do a COUNT(*) query first to determine the $totalrows ... This may or may not be the best way to get the count...

Then a bit like...
$numofpages = $totalrows / $limit;
$pagetotal = ceil($totalrows / $limit);
$page = $_GET['page'];
if(empty($page)){
$page = 1;
}// if empty $page
$limitvalue = $page * $limit - ($limit);

Then for the actual query that will return results, use

LIMIT $limitvalue,$limit

Then to display the links at the bottom, you need to do a lot of IF's to determine page state, etc.

6:13 pm on Jan 23, 2006 (gmt 0)

10+ Year Member



If you do SELECT SQL_CALC_FOUND_ROWS field, field... in your query with the limit, you can follow up with a SELECT FOUND_ROWS() to get the entire number. That way you don't have to worry about COUNT() queries. I've found it to be much, much faster to do it this way because the second query is read from cache.

Ex: Let's say you have a table with 1,000 rows...

SELECT SQL_CALC_FOUND_ROWS id, description FROM table LIMIT 0, 10
- Returns 10 records
SELECT FOUND_ROWS()
- Returns 1 record with a value of 1,000.
 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month