Page 1 of 1

page numbering explained

Posted: Sat Aug 28, 2004 3:49 pm
by buddok
hey does anyone no any sites that realy explain exactly about page numbering and teach about them realy well using mysql ? any help grate full

Posted: Sat Aug 28, 2004 6:19 pm
by litebearer
Might have a look here

http://www.phpfreaks.com/tutorials/43/0.php

Lite...

Posted: Sat Aug 28, 2004 7:42 pm
by buddok
yer i tried that but it seemed a bit complex for me im a beginner so if anyone else knows of a simpler version please helP:)

Posted: Sat Aug 28, 2004 8:07 pm
by John Cartwright
just read it carefully it is very well commented with explainations. For the functions you dont understand just head over to php.net

Posted: Sat Aug 28, 2004 8:27 pm
by buddok
ive tried an i still cant do it i would like somthin a lil smaller without all the fancie background stuff jsu the basics cus that one wudnt work the links didnt work correctly :(

Posted: Sat Aug 28, 2004 8:45 pm
by m3mn0n
The term you should search for is "pagination"

http://www.google.com/search?hl=en&ie=U ... help&meta=

8)

Posted: Sat Aug 28, 2004 9:25 pm
by Christopher
The implementation of a pager is pretty straightforward. You just need to know the the number of total records, the number of records per page, and the page number you want to show. The number of records per page is something you define:

$records_per_page = 25;

The number of total records you need to get by doing a query (in MySQL) like:

$result = $db->query("SELECT COUNT(*) AS total_records FROM mytable");
$row = $result->fetchRow();
$total_records = $row['total_records'];

The page number is usually passed in the url (e.g. http://www.mysite.com/pager.php?page=2). Then you need to get the page number like (remember you ALWAYS have to filter ALL data from the request):

$page_number = intval($_REQUEST['page']);

Once you have these three values you need to do the calculation to figure out what the first record you want to fetch is:

if ($page_number > 0) {
$first_record = ($page_number - 1) * $records_per_page.
} else {
$first_record = 0;
}

You should add checks to see if you are trying to get records past the last record. I'd recommend calculating what the last page number is and checking $page_number against that.

Finally you need to do your query and fetch the data which is like this:

$result = $db->query("SELECT * FROM mytable LIMIT $records_per_page OFFSET $first_record");
while ($row = $result->fetchRow()) {
... // build HTML row
}

The obvious performance optimization is to only do the "SELECT COUNT(*)" the first time the pager is accessed. Store the total number of records and any other calculated values in the session which is (usually) much faster than the query. You can assume that you only need to do the "SELECT COUNT(*)" when there is no page specified in the request or no value set in the session.

Put it all together, add error checking to the database calls, bounds checking to the calculations and off you go. Building the URLs is easy (e.g. "<a href=\"?page=" .( $page_number + 1) . '">Next Page</a>'). Again, you can do bounds checking to see if there is a previous or next page.

Posted: Sat Aug 28, 2004 11:06 pm
by buddok
i no its called pagination but thanks aan im jus lookin for a realy good tutorial that explains becuase the ones ive found have to much pointless stuff i want it stripeed to its most basic and they never have the original display data either they jus tell you to do it an i dont get it all so if anyone knwos a good tutorial please submit here :)

Posted: Sun Aug 29, 2004 10:39 am
by McGruff
There's an Indexer class here which handles all the arithmetic.

It's intended to be used with a QueryIterator which picks out the rows you want from a full result set. Standard QueryIterators need a minor modification to allow them to seek to an offset.