peppy — 2013-01-24T18:34:26-05:00 — #1
I have a site where members can sell items and place them in categories and sub-categories.
When a buyer views an item page, I would like to display 10 random items being sold by the same member. So far, everything is working fine:
$sql = "SELECT * FROM items WHERE seller = '$seller' ORDER BY RAND() LIMIT 10";
I would like to show similar items related to the category that the item page is listed in, rather than totally random, un-related items.
For example, here are some categories:
Collectibles > Bottles
Collectibles > Coins/Banknotes > Coins
Collectibles > Coins/Banknotes > Banknotes
Collectibles > Comic Books
Movies > DVDs
Movies > VHS
Let's say this member has thousands of items listed in all of these categories. When a buyer looks at an item listed in: "Collectibles > Coins/Banknotes > Coins" , I would like to show random "Coins" items from this seller. If he has less than 10 coins for sale, I would like to then show random "Coins/Banknotes" items. If there are still less than 10 items, show random "Collectibles". Basically the randomness starts specifically and then grows more general.
I hope this description is understandable. Please let me know how this could be done.
starlion — 2013-01-24T20:17:28-05:00 — #2
How are you establishing the relationship between categories and their parents/children in the database?
peppy — 2013-01-24T22:48:51-05:00 — #3
In my items table, I have columns for the categories with names like:
cat1, cat2, cat3
cat1 is the main category such as "Collectibles". cat2 is a sub-category such as "Coins/Banknotes". cat3 is a sub-sub-category such as "Coins".
Collectibles > Coins/Banknotes > Coins
Note that these columns in the items table stores the id numbers referencing category row in a separate categories table.
starlion — 2013-01-25T08:36:26-05:00 — #4
Hrm. Not the most common model of mapping, but we'll roll with it for the moment.
This is going to be easier to do in a mix of PHP and SQL i believe... unless the guys have some tricks up their sleaves... pauses, thinks about that
Okay... tricksy time. Completely untested and probably can be done better! (I really dislike ORDER BY RAND(), btw)
WHEN col3 = $thiscol3 THEN 3000
WHEN col2 = $thiscol2 THEN 2000
WHEN col1 = $thiscol1 THEN 1000
END AS weight
WHERE seller = '$seller'
ORDER BY (RAND()*999)+weight DESC
EDIT: Wait.. no, not null doesnt work.. gotta be equals. fixes
EDIT2: Wow. Brain not fully engaged this morning. Fixed it more, and put the order in the right direction :lol:
peppy — 2013-01-25T14:49:39-05:00 — #5
Thanks StarLion, this works great and is VERY COOL! I love learning such great information here at SitePoint.
The SQL works perfect and I got the PHP to output similar items very nicely.
Thanks for the help!
kylewolfe — 2013-01-25T14:52:19-05:00 — #6
Now about your table structure ...
peppy — 2013-01-25T15:14:09-05:00 — #7
What's wrong with the table structure?
kylewolfe — 2013-01-25T15:28:51-05:00 — #8
You generally want to use a "parentId" column and then a PHP recursion to create a tree such as this. It enables you to have an infinite amount of levels as well as becomes easier to look at / maintain.
felgall — 2013-01-25T17:38:48-05:00 — #9
I did some testing to compare ORDER BY RAND() with the alternative of counting how many entries meet the criteria in one query and then retrieving using a random number between 1 and that count to retrieve the record at that specific location within the results. For retrieving just one result where less than about 700 entries satisfy the condition the ORDER BY RAND is faster while with 1000 or more entries the other way is faster. As you want to retrieve more results the ORDER BY RAND() approach is the more efficient way for larger numbers in the results. For example if you are retrieving 30 random results then ORDER BY RAND() was more efficient if the condition is met by less than about 50,000 entries.
With the OP's situation where 10 results are to be returned at random using ORDER BY RAND() would probably be more efficient as long as there are less than say 6,000 entries to select those 10 from. As few sellers would be selling that many items this would make ORDER BY RAND() the more efficient alternative to use for this particular situation.
here's an outline of the code for the alternative that I tested that doesn't use the ORDER BY RAND() for retrieving one random result:
SELECT COUNT(*) FROM tablename WHERE condition
$limit = rand(1, $myrow);
SELECT * FROM tablename WHERE condition LIMIT 1 OFFSET $limit
For retrieving more than one the second line would be run as many times as necessary loading the results into an array and dropping any duplicates and then the third line would be run for each entry in the array.