Posts: 1,832
Location: Somewhere else entirely
|
Skipping through the results isn't the best way of doing pages. MySQL provides the LIMIT and OFFSET clauses to do this:
PHP Code:
<?php
//Connect to your database, //YOU WILL NEED TO FILL THIS BIT IN
//Get the page number from the URL, or set it to 1 if the URL doesn't //have page set to anything. if(isset($_GET['page'])) { $page = $_GET['page']; } else { $page = 1; }
$num_per_page = 5;
//Calculate the starting item from the page number and the num per page $start = ($page-1)*$num_per_page +1;
//Get the right number of items from the right place in the db. $query="SELECT * FROM test3 LIMIT $num_per_page OFFSET $start"; $result = mysql_query($query) or die(mysql_error());
//Start a counter to display the item numbers $i = $start;
//Loop over the results while($row = mysql_fetch_array($result)) { echo "[item " . $i . "]: "; echo $row[0] . "<br/>"; $i++; }
//Print the Next and Prev links if($page > 1) { echo '<a href="' . $_SERVER['PHP_SELF'] . "?page=" . ($page-1) . '"><--Prev </a>'; } if(mysql_num_rows($result) == $num_per_page) { echo '<a href="' . $_SERVER['PHP_SELF'] . "?page=" . ($page+1) . '"> Next--></a>'; }
?>
You'll need to fill in the top bit with your database connection (mysql_connect or whatever)
You'll also need to change the table name from test3 to whatever your table is.
This prints a very basic list of items and two links. You could extend it very easily to print nice tables or something.
Also when there are an exact multiple of five items in the database, this script adds an extra page which shouldn't be there. Getting rid of that is possible but would make this nice simple example a lot more messy.
I've tested this and put quite a few comments in it, let me know if any part of it needs clarifying....
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';
Please login or register to view this content. Registration is FREE (aka MSN handwriting for forums)
Last edited by 0beron; 12-07-2005 at 01:41 PM..
|