I have have this query which fetches 2 random rows from a database:
$query = "SELECT * FROM foo ORDER BY RAND() LIMIT 2";
$result = mysql_query($query);
What I want is for the 2 choosen items to be remembered so they can be shown again on the next page, I had the idea this would be done by updating another table with the RAND() info but for the life of me cannot think how to do it nor can I find an info on how to do it.
Edit:
The overall idea is to show 2 new items as well as the last 2 items.
Edit 2:
Ok so it looks like it needs to be done using SESSIONS and I have come up with this so far but the results are the current items info rather then the previous
$item_array = array($item_id);
$_SESSION['lastitems'] = $item_array;
foreach($_SESSION['lastitems'] as $key=>$value) {
echo $value . ' <br />';
}
the problem is I think is that $item_id is actually $row['itemid'] and can only be called from within the while loop hence the SESSIONS like I said before are the current items info rather then the previous how do I “store” so to show previous?
Edit 3: I stuck the above foreach outside the loop but it only returns 1 item
You can’t get the same
order by rand()in two subsequent pages – that’s the whole point of ordering by rand in the first place.If you want to remember the data on the next page, why not stick the output into a
$_SESSIONand simply refer to it there?Having said that, you can seed the
rand()function with a value along the lines oforder by rand(3)which will give you the same results – but that opens up the can of worms where you will need to start passing the same seed to the other pages – which you will probably have to store in a$_SESSIONso you end up at square one.Edit: If you want to add 2 new items, use your query as it is, and keep inserting the data into the
$_SESSIONvariable. You can store arrays in there, so it should work a charm. Pull the data back, add it into the session, display all the results from the session. Next page, get two new results, add to session, display all the results from the session.Edit 2:
Firstly, make sure you start the session off on each page:
You want to treat the
$_SESSIONarray just like any other array:Not sure how you iterate through your results before you output, so apply as you see fit:
Now, when you get to displaying the data you can do something along the lines of: