Jump to content


Photo

pages of data


  • Please log in to reply
1 reply to this topic

#1 almightyegg

almightyegg
  • Members
  • PipPipPip
  • Advanced Member
  • 791 posts
  • LocationEngland

Posted 22 October 2006 - 07:57 PM

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...


Visit my Game - Under Construction

#2 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 22 October 2006 - 08:28 PM

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...orials/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...g/Limit_sql.htm

here's a little example pseudocode to set you on your way:
<?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

cheers
Mark
"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users