Jump to content

Archived

This topic is now archived and is closed to further replies.

almightyegg

pages of data

Recommended Posts

when pulling data out of a table using a while loop, how do you make it separate the data into different pages??

like the first 10 rows is one page then it goes to a second page for 11-20 then third etc...

Share this post


Link to post
Share on other sites
the best place to start is the mysql LIMIT clause, which you tag onto the end of your SELECT. it takes either 1 or 2 parameters - 1 if you just want to pick a certain number of rows, 2 if you want to pick a certain number of rows starting from a particular record - which is what you need for pagination. there's a tutorial right here on phpfreaks that describes the basics well: http://www.phpfreaks.com/tutorials/43/0.php

this, for example, will pick 5 records starting from the very first record (0 signifies the first record, not 1):
SELECT * FROM mytable LIMIT 0, 10

this would pick 5 rows on "page" 3:
SELECT * FROM mytable LIMIT 20, 10

pagination generally involves passing the page number in the URL and passing the resulting $_GET['page'] into your LIMIT clause.

http://php.about.com/od/mysqlcommands/g/Limit_sql.htm

here's a little example pseudocode to set you on your way:
[code]
<?php
// how many results per page?
$num_per_page = 10;

// what record shall we start at?
$offset = ($_GET['page'] - 1) * $num_per_page;

$query = "SELECT * FROM mytable LIMIT $offset, $num_per_page";

... etc ...


?>
<a href="/mypage.php?page=1">Page 1</a>
<a href="/mypage.php?page=2">Page 2</a>
<a href="/mypage.php?page=3">Page 3</a>
etc
etc
[/code]

cheers
Mark

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.