It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming PHP and MySQL Forum Script-archive

<Prev 1,2,3... Next> Buttons

Share your completed scripts.

Moderator: Malcolm

<Prev 1,2,3... Next> Buttons

Postby dflynn on Mon Feb 16, 2009 9:11 pm

Hey guys.
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 :D

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 . "\">&lt;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&gt;</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.
User avatar
dflynn
500+ Club
 
Posts: 860
Joined: Wed Oct 03, 2007 9:06 pm
Location: Guelph, Canada

Re: <Prev 1,2,3... Next> Buttons

Postby dflynn on Tue Feb 17, 2009 7:55 pm

In my application, this is being used for a guestbook application.
$a is the last name of the person signing,
$b is the first name,
$c is the date signed.

I added this to the list of variables:
Code: Select all
   if($last==""){
         $comma="";
         }else{
         $comma=", ";}

And then I replaced the beginning of the next part with this:
Code: Select all
      #Into the cell
              echo "<tr>";
         echo "<th>" . $last . $comma . " "

This way, if the person chooses not to include their last name it won't display a comma before the first name.

Image
User avatar
dflynn
500+ Club
 
Posts: 860
Joined: Wed Oct 03, 2007 9:06 pm
Location: Guelph, Canada

Re: <Prev 1,2,3... Next> Buttons

Postby HotNoob on Sat Oct 02, 2010 4:58 am

your query should be

select * from table_name Limit 0,10

Or in this case you want to check the next 20 results... than you do

select * from table_name Limit 0,30

So an easy way to do it...

$min = $_GET['min'];
if($min < 0)
{
$min = 0;
}
$max = $_GET['max'];
$max += 20;
$query = 'select * from table_name Limit '.$min.','.$max.'';
---
limiting in your queries is extremely important, other wise you could end up loading your entire database every time the page is loaded.
---
For example... if you have 30 000 results, and you load them without limiting, you could end up using 30 megs of ram. Get 100 users loading the same page at the same time, and you've got 30 gigs of ram being used, and before you know it, your server crashes(worst case).
---
oh yeah, in this case your trying to count the total rows...
well.. EASY solution, use the mysql table info query.
http://dev.mysql.com/doc/refman/5.1/en/show-table-status.html
HotNoob
100+ Club
 
Posts: 169
Joined: Sun May 02, 2010 1:38 am

Re: <Prev 1,2,3... Next> Buttons

Postby dflynn on Sat Oct 02, 2010 8:38 pm

Yeah this is a really old post I created back when I was still learning PHP.


But anyway, the limit is being set by variable which would be up to whoever is implementing the code to set, depending on how many posts they'd want to deliver on each page. The first page Start is set to 0 and Limit to 10, the second page just adds 10 to each value, so Start is 11 and Limit is 20, etc.

It worked for a while but I haven't had to use this script in a long time. I figure I'd find a simpler way to do it now. Or at least prettier code.

But again, old post.
User avatar
dflynn
500+ Club
 
Posts: 860
Joined: Wed Oct 03, 2007 9:06 pm
Location: Guelph, Canada

Re: <Prev 1,2,3... Next> Buttons

Postby HotNoob on Sat Oct 02, 2010 11:03 pm

:)
HotNoob
100+ Club
 
Posts: 169
Joined: Sun May 02, 2010 1:38 am


Who is online

Users browsing this forum: No registered users and 0 guests