Forum Moderators: coopster
I'd like to sprinkle in 5 random links on each page to other pages on the site. However I want the same 5 every time the pages loads. Think of it a a mini site map on each page.
I have a couple of ideas and I'm not sure which way to go.
--I could just get the next 5 items from the table and wrap if I'm near the end of the table.
--I could use a straight forward rand and cache the 5 links and using them every time a page loads. But I figured there must be a better way than than going to the hard drive and wasting space on cache files.
--I could seed the rand with the id of the current row number of the database.
srand($row["id"])
$linkid=rand()
for ($i=1; $i<=5; $i++)
{
getlinkforitem($linkid)
$linkid=$linkid+1
}
or would this be better....
for ($i=1; $i<=5; $i++)
{
srand($row["id"]+$i)
$linkid=rand()
getlinkforitem($linkid)
}
--Then I thought hmm....what if I try something like converting the url to a md5 hash to seed the rand function. I would have to strip out letters from the hash. I could then use the resulting integer to seed the rand function.
Am I going down the right path? Any thoughts or ideas?
Imho, it would be better with 5 relevant non-random links ;)
I was expecting someone would raise this issue :)
In theory all the links would be related since the site is about widgets. I'll be linking to brown widgets, furry widgets, classic widgets.
And heck maybe some links won't be related. In my mind that's ok. Maybe my visitors are tired of widgets. Here are some links to celebrity gossip, online games and other mindless stuff.