Dividing results up over several pages

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Matt Phelps
Forum Commoner
Posts: 82
Joined: Fri Jun 14, 2002 2:05 pm

Dividing results up over several pages

Post by Matt Phelps »

I have a list of name in my database and I want to print them out over several pages rather than just one long page. How do I get the results of my mySQL query to start on a new page after - say - 10 names? And how about ordering them alphabetically? As in all the 'A's on one page and all the 'B's on another etc..

Can anyone explain to me how to achieve this? I have done a search for answers but I've not understood the answers that people have given.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

f1nutter
Forum Contributor
Posts: 125
Joined: Wed Jun 05, 2002 12:08 pm
Location: London

Post by f1nutter »

I did something similar to this yesterday!

Code: Select all

<?php

  $records = 10; // Number of records per page

  $start = $_GET&#1111;"start"]; // Get "start" value from address line

  if ($start == NULL) // No address line variable
  &#123;
    $start = 0; // Default value
  &#125;

  $query = "SELECT name FROM table LIMIT " .$start. ", " .$records;

  // YOUR CODE HERE
  // Run the query and display results

  // Add number of records per page to starting value to get records for next page
  $next = $start + $records;

  print("<form method="GET" action="?$next">);
  print("<input type="submit" value="Next">);
  print("</form>");

?>
So, what do we have here?
* Get the row number to start displaying from, from the address line
* Set to 0 to begin
* Run query, limiting the output to 10 rows at a time.
* Add a form button to get the next page of records

* Address line = http://www.somewhere.com/page.php
* Press next
* Address line = http://www.somewhere.com/page.php?start=10
* etc.
Post Reply