So this is something I've been wanting to use for a while and found it confusing to implement. With some guidance from Webmaster here: using-next-prev-for-20-items-vt12635.html
I was able to implement and add some new features.
So, here is what I came up with. If you have any improvements, please post them and I will edit this code to make it better.
NOTE: I was trying to implement other codes I found online before I remembered that thread I posted a few months ago. This was the only one that worked properly, so props to Webmaster
Here it is:
- Code: Select all
<?php
//Get the total number or rows in the table
$numresults=mysql_query("select * from table_name");
$numrows=mysql_num_rows($numresults);
//Set the maximum results per page before creating a new page
$limit=10;
//This table holds the <Prev 1,2,3... Next> buttons. CSS was giving me problems.
echo "<table class=\"guest\">";
//Get the Previous Page Button
$prevpage= $_G.ET['page']-1;
//Display the <Prev button if you are not on the first page of results
echo "<tr><td class=\"end\">";
if($_G.ET['page']>0){
echo "<a href=\"?page=" . $prevpage . "\"><Prev</a>";
}
echo "</td>";
//This divides the number of results in the table by the total number of results per page you set earlier. It will determine how many pages you will need so that we can set up the numbered system.
$total = $numrows/$limit;
if ($numrows%$limit ) {
$total++;}
//Here, I got lazy. I plan on tackling this after there have been some results to display. Basically, if there are more than 20 pages it will display:
//'1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,...'
//After 20 pages, I don't know how this will react. It needs improvement.
echo "<td class=\"mid\">";
for ($i=1;$i<=$total;$i++) {
$ilink=$i-1;
if($i<20){
echo "<a href=\"guestbook.php?page=" . $ilink ."\" >$i</a>, \n";
}else{
echo "...";
}
}
echo "</td>";
//This checks that page is not set. It stores info in the $start variable that will later tell the database where to start gathering information from per page.
if(empty($_G.ET['page'])) {
$start = 0;
} else {
//If page is set, it multiplies the page number you are on by the limit to find where to start gather data from
$start = $_G.ET['page']*$limit;
}
//This part checks to see if there are more results left over that are not being displayed on the current page.
if($start+10>$numrows){
}else{
//This sets up the Next> button to link to the next page.
if(empty($_G.ET['page'])) {
$page = 1;
} else {
$page = $_G.ET['page']+1;
}
echo "<td class=\"end\">";
echo "<a href=\"?page=" . $page . "\">Next></a>";}
echo "</td></tr>";
echo "</table>";
//This is where you will start displaying the data. As you can see my application is a guestbook.
echo "<table class=\"guestbook\">";
//Get the posts starting from the last one entered. I haven't tested this script with an Ascending order. Feel free and comment if it works or not.
$query = "SELECT * FROM table_name ORDER BY id DESC LIMIT " . $start . ",$limit";
$result= mysql_query($query);
while($row=mysql_fetch_array($result, MYSQL_ASSOC)) {
//Set up the variables to be used to display data
$a = $row['a'];
$b = $row['b'];
$c = $row['c'];
$d = $row['d'];
//Echo out the cell and the data.
echo "<tr>";
echo "<th>" . $a . ", " . $b . " - " . $c . "</th>";
echo "</tr>";
echo "<tr>";
echo "<td><p>" . $d . "</p></td>";
echo "</tr>";
}
echo "</table>";
// And we're done
?>
Note: Don't forget to replace G.ET with GET
Hope this helps and I hope I can get some feedback to improve the script. It's a little choppy as I made it for a single purpose.
Thanks again to Webmaster for the backbone of the script.




