...

View Full Version : Coding next button in PHP



avizhome
03-22-2010, 01:22 PM
Hi,
My SQL query returns 50 rows, but am trying to sort that in 10 per page and have a next button to view others.
Am using the following code:
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<img src=" . "'upload/" . $row['photo'] . "' width='150' height='150' />". "</td>";
echo "<td width='400'>" . $row['description'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "</tr>";

}
Can someone plese help me on this.
Thanks

Dormilich
03-22-2010, 02:13 PM
you need to limit that for each page. i.e. use the LIMIT clause in SQL and let each page pass a parameter, that determines, which 10 results to print.

avizhome
03-22-2010, 02:30 PM
thanks for your reply Dormilich. I got the LIMIT clause in sql, but how do i pass parameter for each page.

Dormilich
03-22-2010, 02:32 PM
in the URL. say index.php?page=1 and in PHP

$page = (int) $_GET['page'];
$query = "… LIMIT " . (($page - 1) * 10) . ", 10";

avizhome
03-22-2010, 02:36 PM
thanks for your help Dormilich. Really appreciate it :)

Dormilich
03-22-2010, 02:38 PM
you’re welcome.

of course, a Prepared Statement would be much cooler (and safer).

avizhome
03-22-2010, 02:56 PM
Hi Dormilich,
i have tried:


$page = (int) $_GET['page'];
$sql="select * from cars LIMIT 0,2" . (($page - 1) * 10) . ", 10";

but its giving me this error:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/buysell5/public_html/car_search_result.php on line 90

Dormilich
03-22-2010, 02:57 PM
that’s correct, your SQL syntax is wrong.

avizhome
03-22-2010, 03:06 PM
oh sorry typo error


$page = (int) $_GET['page'];
$sql="select * from cars LIMIT" . (($page - 1) * 10) . ", 10";

Dormilich
03-22-2010, 03:07 PM
there is a space missing after LIMIT *geez*

avizhome
03-22-2010, 03:13 PM
thanks :)

avizhome
03-23-2010, 12:51 AM
ok, i got the part of sorting 10 records/page. I can do that by having a next button, but i would like to have something similar to page 1,2...10. where users can select which page they woild like to view. Is it possible with PHP?

MattF
03-23-2010, 12:56 AM
Do a search on here or Google for pagination scripts.

avizhome
03-23-2010, 01:05 AM
Thanks MattF, i love this forum :) therz always someone there 2 help you....thanks alot

Dormilich
03-23-2010, 01:14 AM
guess what the $page parameter does …

avizhome
03-23-2010, 02:26 AM
Hi,
Iam able to show 10 records per page by using:
<code>
$page = (int) $_GET['page'];
$sql="select * from cars LIMIT " . (($page - 1) * 10) . ", 10";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . "<img src=" . "'upload/" . $row['photo'] . "' width='150' height='150' />". "</td>";
echo "<td width='400'>" . $row['description'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "</tr>";

}
<code>
Now am trying to create a next button to navigate through my other records. I have tried the code mention below, but it does not increment the page value :(


<a href="car_search_result.php?page=<?php echo $page++; ?>">Next Page</a>

Any help?

MattF
03-23-2010, 02:32 AM
Increment before echo.



<a href="car_search_result.php?page=<?php echo ++$page; ?>">Next Page</a>

avizhome
03-23-2010, 02:37 AM
PLEASE IGNORE THIS POST, THIS WAS REPOSTED :(
Hi,
I managed to get the 10 records/page using the following code:


$page = (int) $_GET['page'];
$sql="select * from cars LIMIT " . (($page - 1) * 3) . ", 3";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . "<img src=" . "'upload/" . $row['photo'] . "' width='150' height='150' />". "</td>";
echo "<td width='400'>" . $row['description'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "</tr>";

}
mysql_close($con);

Now am trying to code a next button that will help me navigate through the other records. I have tried the following code, but it doesn't increment the $page value :(


<a href="car_search_result.php?page=<?php echo $page++; ?>">Next Page</a>

Any help?

MattF
03-23-2010, 02:40 AM
Any help?

Yes. Use the link code I posted.

avizhome
03-23-2010, 03:03 AM
Thanks Matt,
It works perfectly fine now :)
But when i do a decrement code it doesn't decrement the $page value, instead it increments it.
Thanks

MattF
03-23-2010, 03:05 AM
Where's your code for the previous page link?

avizhome
03-23-2010, 03:07 AM
i have tried both, decrement before $page and after $page. But both increments the value:


<div align="right"><a href="car_search_result.php?page=<?php echo $page--; ?>">Previous Page</a> </div>

MattF
03-23-2010, 03:09 AM
Try this code for the page links:



<a href="car_search_result.php?page=<?php echo ($page+1); ?>">Next Page</a>
<a href="car_search_result.php?page=<?php echo ($page-1); ?>">Previous Page</a>

avizhome
03-23-2010, 03:22 AM
awesome thanks!!!
Works perfectly fine, really appreciate your help :)

informaticar
06-29-2011, 11:12 PM
Hi there,
I am new in php and i am trying to make next/previous button, following your comments.

When I go to Next/Previous Page link show me
Notice: Undefined index: zanr in D:\xampp\htdocs\Order film online\prebaruvanje-nenajaveni.php on line 27

Also url is a bit strange: http://localhost/Order%20film%20online/prebaruvanje-nenajaveni.php?page=%3C?php%20echo%20++1;%20?%3E

I am taking parameters (like zanr) from html script in my php with POST method, is there some problem?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum