I'm building an online store as a project, and I think it'll be cool to see new different content with each page refresh i.e. each time a user comes to the website, he'll see different content on the font page even if no new products were added.
I don't know of any ways the code can 'break' or be user 'unfriendly', I haven't checked the possibilities. What do you think?
Here's the query:
SELECT
SQL_CALC_FOUND_ROWS
name,
id
FROM
products
ORDER BY
rand()
LIMIT
.........(php calculation to make the pagination work).........
I tested it out and noticed that no duplicates appear across pages (which is the only problem I see with sorting by random), I think that's cause of the super cool pagination-library I'm using. Unless it's just a coincidence....
Nah, it can't be a coincidence across 1,000 rows!
So what do you think?
That's not how pagination works. You need to define a dataset, which can consist of randomly sorted database entries, and the pagination makes it possible to only see part of it. If, however, every call is random (which I think you are doing, because you write SQL+PHP), then you cannot go to the previous page to see the items you previously saw. It'll always be random, and you can just leave out pagination and add a refresh-button instead.
You can test the behavior by going to page 2, then 3, then 2. You should always see different content, even though pagination should show you the same on page 2 as you already had the first time you opened page 2.
If you don't want to rely on JS to download an entire dataset, you'd have to cache the dataset on the server, which might explode depending on the store visitors. That's why, instead of using rand(), I suggest coming up with a different, deterministic algorithm, which shows new content every now and then but can be calculated and keeps pagination functional.
Maybe use a cookie to store a seed which you use to randomize the search, and give the seed a timeout, which you refresh on every page visit, on which the timeout is still active. This way, the user will see the same stuff when they surf around, but if they come back after some hours (timeout), they will be greeted by new and exciting things...
Gijo Varghese
A WordPress speed enthusiast
Random products on every reload? Not a good idea!