By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,370 Members | 1,552 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,370 IT Pros & Developers. It's quick & easy.

Random Order Help Request - prevent previously displayed objects

P: n/a
I have a query that is showing new items in a store based upon their
inventory date. The problem with my Random order sort is that when the
"next" link is pressed, the random function is called again and I may see
the image previously displayed rather than go through the entire new
inventory.

Any suggestions on how to prevent that occurrence?

SELECT *
FROM `Catalog`
WHERE (`Catalog`.thumbnail LIKE '%-04.%' or `Catalog`.thumbnail LIKE
'%-05.%' or`Catalog`.thumbnail LIKE '%-03.%' ) and `Catalog`.img_status =
'1' and `Catalog`.views != '0' ORDER BY RAND()
Thanks
Jeff
~~~~~~~~~~~~
Jefferis Peterson, Pres.
Web Design and Marketing
http://www.PetersonSales.com

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

"Jefferis NoSpamme" <je********@hotmail.com> wrote
I have a query that is showing new items in a store based upon their
inventory date. The problem with my Random order sort is that when the
"next" link is pressed, the random function is called again and I may see
the image previously displayed rather than go through the entire new
inventory.


Couple of ideas:

1. Make up a pseudo-random sort order, say, by combining several fields
together, and save the last key fields displayed in a session variable.

When they hit next, use the session key variable to bound your search.

2. Alternatively, if not a high volume site, put the entire result set of
your rand() ordered query (all rows) in a session variable, and store the
highest row displayed in a session variable for use by the "next" logic.

3. Add a random column (randcol) to your table, and refresh it every x
hours or days to reshuffle the deck, so to speak. Sort the query by randcol,
unique_Id (possible for two rows to have the same random number). Save the
last displayed key in-session and bound your "next" search by that saved
key.

-- Dan
Jul 17 '05 #2

P: n/a
Thanks Dan. I'll think on these options.

Jeff
On 3/8/05 2:35 PM, in article
38**************@newsread1.news.pas.earthlink.net, "Dan Stumpus"
<ds***********@mindspring.com> wrote:
together, and save the last key fields displayed in a session variable.

When they hit next, use the session key variable to bound your search.

2. Alternatively, if not a high volume site, put the entire result set of
your rand() ordered query (all rows) in a session variable, and store the
highest row displayed in a session variable for use by the "next" logic.

3. Add a random column (randcol) to your table, and refresh it every x
hours or days to reshuffle the deck, so to speak. Sort the query by randcol,
unique_Id (possible for two rows to have the same random number). Save the
last displayed key in-session and bound your "next" search by that saved
key.


~~~~~~~~~~~~
Jefferis Peterson, Pres.
Web Design and Marketing
http://www.PetersonSales.com

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jul 17 '05 #3

P: n/a
On Tue, 8 Mar 2005, Jefferis NoSpamme wrote:
I have a query that is showing new items in a store based upon their
inventory date. The problem with my Random order sort is that when the
"next" link is pressed, the random function is called again and I may
see the image previously displayed rather than go through the entire new
inventory.

Any suggestions on how to prevent that occurrence?


Assuming you are using sessions, you could seed the database's RAND()
function with a value which you create for each new visit and save in the
PHP session.

That way each visit would get a (different) randomized list but the order
would stay constant throughout the course of the visit.

Something like:

<?

if (!array_key_exists("randomseed", $_SESSION)) {
$_SESSION["randomseed"] = time();
}

mysql_query("SELECT ... ORDER BY RAND($_SESSION[randomseed])");

?>

--
Matt

Jul 17 '05 #4

P: n/a
Thanks Matt.

The client decided just to limit the page to this year's items which was an
easy fix. It isn't random, but the limited number of items make it less
important.
On 3/9/05 12:03 PM, in article
Pi*******************************@hermes.athens, "Matt Raines"
<matt@rai#remove#nes.me.uk> wrote:
On Tue, 8 Mar 2005, Jefferis NoSpamme wrote:
I have a query that is showing new items in a store based upon their
inventory date. The problem with my Random order sort is that when the
"next" link is pressed, the random function is called again and I may
see the image previously displayed rather than go through the entire new
inventory.

Any suggestions on how to prevent that occurrence?


Assuming you are using sessions, you could seed the database's RAND()
function with a value which you create for each new visit and save in the
PHP session.

That way each visit would get a (different) randomized list but the order
would stay constant throughout the course of the visit.

Something like:

<?

if (!array_key_exists("randomseed", $_SESSION)) {
$_SESSION["randomseed"] = time();
}

mysql_query("SELECT ... ORDER BY RAND($_SESSION[randomseed])");

?>

--
Matt


~~~~~~~~~~~~
Jefferis Peterson, Pres.
Web Design and Marketing
http://www.PetersonSales.com

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.