Page 1 of 1
How to create a next page button with php and mysql ?
Posted: Sat Nov 29, 2003 5:11 pm
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.
Posted: Sat Nov 29, 2003 7:03 pm
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
Posted: Sat Nov 29, 2003 9:35 pm
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.
Posted: Tue Dec 02, 2003 2:54 am
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);" ?
Posted: Tue Dec 02, 2003 5:47 am
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.
Posted: Tue Dec 02, 2003 7:26 am
by GentooBox
thanks
