Page 1 of 1

How to Paginate Results

Posted: Tue Jul 15, 2003 7:23 am
by mndwn
Hello, I am trying to order my results into pages by limiting the amount of results per page. But I wouldn't have a clue on how to add next and previous page links. This is the Code so far:

Code: Select all

<?php
print "<table border='0' bgcolor='#CCCCCC' align='center'><tr><td>
					       <form method=get>
                           <p>By Route Type:
                           <select name='search_type'>
                           <option>SELECT</option>
                           <option value='2'>Domestic</option>
                           <option value='3'>International</option>
                           <option value='1'>Regional</option>
                           </select>
                           <font color='#CCCCCC'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font>
                           <input name='Searchr3' type='submit' value='Search'> 
                           </form></td></tr></table>";
					  if ($_GET['search_type'])
					   {
					  $sql = "SELECT routes.routeid, depart.depart, depart.departcode, arrive.arrive, arrive.arrivecode 
					          FROM (routes INNER JOIN depart
							  ON routes.departid = depart.departid)
							  INNER JOIN arrive ON routes.arriveid = arrive.arriveid
							  INNER JOIN routetypes ON routes.routetypeid = routetypes.routetypeid
					          WHERE routetypes.routetypeid = '".$_GET['search_type']."'
                  LIMIT 0, 20";
                       $result = mysql_query($sql, $db);
					   print "<table width='75%' align='center' border=0>";
                       print "<tr><th><p><b>Route Code</b></th><th><p><b>Departure Airport</b></th><th><p><b>Code</b></th><th><p><b>Arrival Airport</b></th><th><p><b>Code</b></th></tr>";
					while
					   ($row = mysql_fetch_array($result)) {
                        print "<tr bgcolor='$rowcolour'><td align='center'><p>AR".$row['routeid']."</td>";
						print "<td align='center'><p>".$row['depart']."</td>";
						print "<td align='center'><p>".$row['departcode']."</td>";
						print "<td align='center'><p>".$row['arrive']."</td>";
						print "<td align='center'><p>".$row['arrivecode']."</td></tr>"; }
                        print "</table>";
						$rowcolour = ($rowcount%2)?$colour1:$colour2; 
                        $rowcount++; }
?>
Can anyone help me code it so that it creates a previous page and next page link? Thanx

Posted: Tue Jul 15, 2003 7:41 am
by bionicdonkey
set the number of results you want in a variable.
add a variable to the url to say the page number

for the limit bit in the query make start value number of results * page number and for end value just add number of results to the value of start value

Posted: Tue Jul 15, 2003 7:59 pm
by DuFF
Try out this tutorial, after a little editing it's now working great for me.

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

Posted: Wed Jul 16, 2003 1:12 pm
by daven
Random points for using the word "paginate". Now if you can find a way to work "antidisestablishmentarianisms" into your code, I will be impressed.

note: "antidisestablishmentarianisms" is the longest word in the English language (at least it was a few years ago).

Posted: Wed Jul 16, 2003 4:48 pm
by Drachlen
If you're still curious, i set this up for my website last night. What you will need to do in your query is something like:

Code: Select all

SELECT * FROM your_table ORDER BY ID ASC LIMIT $s, $f
Also keep in mind it starts at 0. So if you wanted to show the first five entries, you would type in the url ?s=0&f=4 .. And incase you're wondering, S means start, and F means finish.. Now this is what i did to display 44 results per page:
<a href=?p=vu&s=" . ($s+43) . "&f=" . ($f+43) . ">Next page</td>

You might not want the ?p=vu part, that just stands for 'view users'.. There are some things that you might be picky about.. Like, this will always say next page, you can keep going forever, and it never lists the page your on.. I think if you really needed to you could just add another variable called page and then have it ($page+1) in the url.. And not really sure how to stop displaying the next page button.. You could probably use something to count the rows and it its not equal to 44 then cut off the next page...or something.

Hope this helps..