How to create a next page button with php and mysql ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
GentooBox
Forum Newbie
Posts: 3
Joined: Sat Nov 29, 2003 5:11 pm

How to create a next page button with php and mysql ?

Post by GentooBox »

Hi.

I have a problem with my php design.
i dont know how to make a page with mysql results, and limit the results to 10 entrys, and then make a link to the next 10 results. - do you know what i mean ?

its like a navigation bar at the bottom of the page with a link to the next results.

my page withe the mysql results have 60 entrys, and i use $_GET to grap a mode from the URL bar.

Code: Select all

www.linktomysite.com?page=main&action=show
i would like the navigation bar to insert &limit=10 in the URL if its possible.
ghost007
Forum Commoner
Posts: 49
Joined: Sat Nov 22, 2003 10:10 am

Post by ghost007 »

hi,

you can just add a LIMIT statement in your mysql query as shown in the example below. Note that I'm not sure this is the best way!

Code: Select all

<?php
$rows_to_show = 10;

//first select all to count total rows
$query = "Select * from table WHERE bla='bla'"
   $result= mysql_query($query);
$max = @mysql_num_rows($result);

//now you can add the LIMIT parameter => start will be a GET param!
$query = "Select * from table WHERE bla='bla LIMIT $start,$max'"
  $sqlquery = mysql_query($query);

//define if numbers should be shown by adding an if statement
if ($max > rows_to_show){
<html or PHP if u use template engine>
}
?>
hope this helps
siech
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

I used something similar, but i had...

Code: Select all

<?php
switch ($_GET['page'])
{
 default: 
    $max = 1;
    $min = 10;
 break;
 case 1;
    $max = 1;
    $min = 10;
  break;
 case 2;
    $max = 11;
    $min = 20;
  break;
 case 3;
    $max = 21;
    $min = 30;
  break;
// etc...
}
?>
...for the LIMIT switcher.

It might not be a good method if you have a few hundred pages, but it works.
GentooBox
Forum Newbie
Posts: 3
Joined: Sat Nov 29, 2003 5:11 pm

Post by GentooBox »

ghost007 wrote:hi,

you can just add a LIMIT statement in your mysql query as shown in the example below. Note that I'm not sure this is the best way!

Code: Select all

<?php
$rows_to_show = 10;

//first select all to count total rows
$query = "Select * from table WHERE bla='bla'"
   $result= mysql_query($query);
$max = @mysql_num_rows($result);

//now you can add the LIMIT parameter => start will be a GET param!
$query = "Select * from table WHERE bla='bla LIMIT $start,$max'"
  $sqlquery = mysql_query($query);

//define if numbers should be shown by adding an if statement
if ($max > rows_to_show){
<html or PHP if u use template engine>
}
?>
hope this helps
siech
thanks.. why do you have a @ in front of "@mysql_num_rows($result);" ?
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

1. The @ sign means that the function will not return an error regardless of whether it succeeds or fails. More about that on http://www.php.net/manual/en/language.o ... ontrol.php

2. I also wanted to have Next and Previous pages and found this tutorial very helpful http://www.phpfreaks.com/tutorials/43/0.php.
GentooBox
Forum Newbie
Posts: 3
Joined: Sat Nov 29, 2003 5:11 pm

Post by GentooBox »

DuFF wrote:1. The @ sign means that the function will not return an error regardless of whether it succeeds or fails. More about that on http://www.php.net/manual/en/language.o ... ontrol.php

2. I also wanted to have Next and Previous pages and found this tutorial very helpful http://www.phpfreaks.com/tutorials/43/0.php.
thanks :)
Post Reply