Jump to content

Previous and Next Mysql Results


kirk112

Recommended Posts

Really stumped on how to acheive this....

I have a form in which people can search the database for items and when they click on the item it takes them to the item details page on the bottom of this page I want to have previous and next item.

How can I get the pervious and next item_id passed to this page or if I run the query again on the details page how can I get the pervious and next item from the results?

Hope this makes sense (does not to me) I can't seem to get my head around this....


Thanks for your help!!!!
Link to comment
https://forums.phpfreaks.com/topic/33604-previous-and-next-mysql-results/
Share on other sites

theres lots of tutorials about pagination here :-)
not tested... but should work...
[code]
$topallowed=10;
if(empty($_GET[page])) $topfile=0;
else $topfile=$_GET[page];

$topallowed=$topallowed+$page;
$result=mysql_query("SELECT * FROM table LIMIT $topfile, $topalllowed");
while($row=mysql_fetch_array($result)){

}

echo '<a href="?page='.$page-1.'">prev</a>';
echo '<a href="?page='.$page+1.'">next</a>';
[/code]
just for ideas...
Hi Taith, 

Thanks for your response - I have managed to do the pagination on the search page - with a very very helpful tutorial of this site.

I will try to explain a little better...

on the search page when the user click on a product it passes the item_id and the search string to the item details page, on the item details page it displays the details and an option to go back to the search (which is fine) but instead of going back to the search page I want to be able to have previous and next buttons so that they can go to the next item with in the search that they previously have done.  But I can I find what the pervious and next items are??

personally... if it were me, i like functions... i'd do it this way....

[code]
function get_itemname($id){
if(!isset($allitemnames[$id])){
  $result=mysql_query("SELECT * FROM items WHERE `id`='$id' LIMIT 1");
  $row=mysql_fetch_array($result);
  $allitemnames[$id]=$row[name];
}
return $allitemnames[$id];
}
[/code]

then wherever you want an item name...
just...
[code]
echo get_itemname(1);
echo get_itemname($page-1);
echo get_itemname($page+1);
[/code]

or to that effect :-)
Thanks again Taith

If I use your function I still don't know what the previous and next result would be, as there are about 300 result produced on each search and the item_id's are not incremental because the search would filter out some of the item.

on the search result page it displays

item_id  item_name  item_price
  1          test          33
  4          test2        54
  5          test3          88 
  8          test66        99

first page 2 ,3, 4, 5, 6, last page etc

If I click on item_id 5 on the item details page it passes the item_id(5) and the search string eg(price less than <100)

On the items details page I have:


item details - test3
price 88

back to results (which take me back to the search details page)

What I need is a next button which would take me to item_8 and and previous button which would take me to item_4,  how I can get item4 and item8 passed to the item details page to that I can use these variable to create the link to these different items page.


Thanks for your patience



I have a function that does this...It's not the most elegant piece of code I have written but it works.  I did this for viewing images one at a time from an album.

[code]
<?php
while($row = mysql_fetch_assoc($result))
{
If ($row['image_id'] == $image_id)
{
$previous_image = $tmpPrev;
$image_name = $row['image_file_name'];
$bool_next = TRUE;
}elseif ($bool_next === TRUE)
{
$next_image = $row['image_id'];
$bool_next = FALSE;
}else
{
$tmpPrev = $row['image_id'];
}
$i=$i+1;
?>
}[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.