Does anyone have a php next previous button tutorial they are willing to post?
Printable View
Does anyone have a php next previous button tutorial they are willing to post?
for what? browsing through databases?
this is what I usually use, not exactly a tutorial, but it will show you what works... I basically took it right out of an application I've already developed, so I kept in some the table data. I added a lot of comments for ease of reading, too.
a working example of this exact script (no changes AT ALL, except for the database/password and stuff, so this is live information!) can be found right here. the information is from a ladder system, and is sorted by their rating (which essentially defines their rank, which is $i).PHP Code:<pre>
<?
mysql_connect("hostname", "username", "password");
mysql_select_db("database_name");
$mysql['table'] = "table_name";
$pages = array();
//this is how many records we display per page
$pages['display'] = 25;
//this is the current page that we're on, defined by GET['pg']
$pages['current'] = (isset($_GET['pg']) && is_numeric($_GET['pg']) && $_GET['pg'] > 0) ? $_GET['pg'] : 1;
//this is the number we start at in our query
$pages['cur_nums'] = (($pages['current'] - 1) * $pages['display']);
//this is what we append to our query to limit the results
$pages['query'] = " LIMIT " . $pages['cur_nums'] . ", " . $pages['display'];
$sql = "SELECT " .
"id, displayname, wins, losses FROM {$mysql['table']} " .
"WHERE ratingh>0 AND activation='' " .
"ORDER BY rating DESC";
//now query the database twice, once to get the results for this page
$query = mysql_query($sql . $pages['query']);
//and secondly to get the TOTAL results from the database
// note: just for the number, not for the data
$count = mysql_query($sql);
//find out if we have any results
$n = mysql_num_rows($count);
if($n == 0 || mysql_num_rows($query) == 0){
echo 'No information!';
$nodisplay = true;
}
//do we need to display pages?
if($n > $pages['display']){
//this is how many pages we need to have
$pages['pages'] = ceil($n / $pages['display']);
//we do this so that it will display (1 to 25) and not (0 to 25)
$c = ($pages['cur_nums'] == 0) ? 1 : $pages['cur_nums'];
//this is the last number on the current page
$pages['this'] = ($pages['cur_nums'] + $pages['display']);
if($pages['this'] > $n) $pages['this'] = $n;
//this is the complex version of what we display to the user
// ie: ("displaying 1 to 25 of 200 results")
$c .= "</strong> to <strong>" . $pages['this'] . "</strong> of <strong>" . $n;
}else{
//this is the simple version of what we display to the user
// ie: ("displaying 21 results")
$c = $n;
}
//define the first index as correct number
// (ie: page 2 starts at 26 and goes to 50, while page 1 would start at 1 and go to 25)
$i = $pages['cur_nums'];
//if there were results, we display them now
if($nodisplay == false){
?>
displaying <strong><?php echo $c; ?></strong> results
<div style="border-top: 2px solid #000; border-bottom: 2px solid #000; ">
<?php
while($array = mysql_fetch_array($query)){
$i++;
echo " #" . $i . ". " . $array['id'] . " : " . $array['displayname'] . " " . $array['wins'] . "-" . $array['losses'] . ", (" . floor(($array['wins'] / ($array['wins'] + $array['losses'])) * 100) . "% wins) \n";
}
?>
</div>
<?php
//display our page numbers
if(isset($pages['pages'])){
for($i = 1; $i < ($pages['pages'] + 1); $i++){
if($pages['current'] == $i) echo '<strong>';
echo '<a href="./pages.php?pg=' . $i . '">' . $i . '</a> ';
if($pages['current'] == $i) echo '</strong>';
}
}
}
?>
</pre>
hope that helps! feel free to ask questions or, if someone else reads through this, feel free to tell me my system sucks?!@