|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
i have this code displaying random images from a database, they work just well.
Now i need to give priorities(HI , MED, LOW) to the images, where the HI-priority image will be displayed more often than MED-priority image, and the LOW-priority images will be the least to be displayed. any ideas how i can implement this? PHP Code:
thanks in advance |
|
#2
|
|||
|
|||
|
I would suggest using a weighted random selection. For example doing rand % 6 where -
0 = HI 1 = HI 2 = HI 3 = MED 4 = MED 5 = LO Then add an extra clause to your SQL select statement where priority=HI/MED/LO (or rather the numerical equivalent of HI/MED/LO) based on the outcome of the random selection. Hope that helps, -KM- |
|
#3
|
|||
|
|||
|
Thanks kode_monkey for your reply!
what do you mean by rand % 6 and can you please explain further? |
|
#4
|
||||
|
||||
|
Markrenn, I believe kode_monkey was suggesting that you give your images a priority code corresponding to those listed and then add a where clause correlating that priority code to a randomly chosen number. I see a problem with that, though, in that if you use numeric indices in your query, you then have to make sure that you insert your values in the correct proportions. That is, if you use
0 = HI 1 = HI 2 = HI 3 = MED 4 = MED 5 = LO as your key/value map and stick the numeric indices in the database rather than the string "HI," "MED," or "LO," you have to keep up with the frequency with which you insert the different index numbers in order to ensure that your weighting is intact. I think I'd consider inserting the text strings into the database and then having code that creates an array in your PHP code correlating numbers to the text strings, selecting a random array element, and building your query using the value for that array key. So for example, assuming you've inserted "HI," "MED," and "LO" as the weight values in your database: PHP Code:
This also makes it a snap to change the weights. If all of a sudden you need to make the HI images appear more frequently, you just add a "HI" element to your array and it's all taken care of. If you insert numeric keys into the database, then you'd have to do some tricky updating to guarantee that the number of zeroes, ones, and twos were evened out so that the weighting among the HIs was equal. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > General Programming Help > showing random images with priorities |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|