Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Back and Next

Status
Not open for further replies.

buzzt

Programmer
Oct 17, 2002
171
CA
What is the easiest way to do back and next links so I can limit the number of results per page? I have come up with something, but it seems awfully complicated for nothing. I'm sure there is a better way.
 
Paging through sets of result data can be complicated.

If the data is coming from a database server, the trick is correct use of the LIMIT clause of your SELECT statements.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Yeah, but what about the next and back buttons? How do I make them work?
 
Pick a set number of records your script will display each time it is run -- for sake of discussion, let's pick 25. Call that output a "page" of records. Each page is numbered, such that page 1 displays records 1-25, page 2 displays records 26-50, etc.

Your script will be looking for GET-method input. If it is run with no input, it assumes that it should display page 1.

So your links will provide page numbers. If the script is currently displaying page 6, it will add links to the page like:

<a href="yourscript.php?page=5">Previous</a>
and
<a href="yourscript.php?page=7">Next</a>

Your script then looks for the existence of and then a value in $_GET['page'].

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
I like to hide my next and prev is hidden textboxes and use submit buttons to move thru the recordset. I append the value of the nextblock to the sql statement with a limit to make it select the correct block of records.
Code:
//figure out what the user wants
//$Display_len is the number of records to display on one page
//get the values of the paging system
//If neither has a value then
if ((!$_POST['submit']=="Next")&&(!$_POST['submit']=="Previous")){
   $NextBlock = 0;

//move forwards thru recordset results
}elseif($_POST['submit']=="Next"){
   $NextBlock=+$Display_len;

//move backwards thru recordset results
}elseif($_POST['submit']=="Previous"){
   $NextBlock=-$Display_len;
}
//can't move back past first record
if ($NextBlock < 0 ){
   $NextBlock = 0;
}

the code for the sql
Code:
$sql="select a.ad_id as aid, a.ad_title as title, a.ad_text as text,
      u.user_id as uid, u.user_name as user, u.user_email as email
      from ads a, users u
      where ad_deleted = 0 and ad_duration >= now()
      and a.ad_user_id = u.user_id order by ad_last_access_timestamp desc
      limit $NextBlock, $Display_len";

and the hidden elements
Code:
//paging
    echo "<td align=\"center\"><b> $total Ads available (Page $currpage of $totalpage)</b><br /><br />
          <input type=\"hidden\" name=\"total\" value=\"$total\">
          <input type=\"hidden\" name=\"NextBlock\" value=\"$NextBlock\">";

    if ($total < $Display_len + $NextBlock + 1){ $NextStatus = " disabled ";}
    if ($NextBlock == 0){  $PrevStatus = " disabled ";}
    echo "<input type=\"submit\" value=\"Previous\" name=\"submit\" $PrevStatus >&nbsp;&nbsp;&nbsp;";
    echo "<input type=\"submit\" value=\"Next\" name=\"submit\" $NextStatus>";
    echo "</td></tr>";

hth


Bastien

Cat, the other other white meat
 
Hello,

I'm pretty new to php but i'm using it with. There's a good book called Web database applications with PHP and MySQL Williams, Hugh E. that has an example of what you're asking in it. It contains function called browse that I've used it myself. However, it runs the function for every page of results and passes the query through. I had a problem with passing the query through the url because it was so large and so I used form buttons for my previous and next buttons instead to pass the query back. Dunno if this is gonna be of any use to you.Good luck.

Manni
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top