Random image weighting

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Random image weighting

Post by JayBird »

I am about to create a script that will display random images.

The image paths and descriptions will be stored in a MySQL database.

Now, i want to be able to weight the images, so that images that are higher weighted are more likely to be shown than images that are weighted lower.

I have no idea where to even start with this idea!?

Any suggestions?

Mark
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

in the last week i have created half a dozen "random" scripts

what do you mean by "higher weighted" ?

viewed more?
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

malcolmboston wrote:what do you mean by "higher weighted" ?

viewed more?
No, say i have a system where i give each image a number between 1-5.

Images that are weighted 5 have more chance of being display than images which are rated 1.

Mark
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

create an array with image names to choose randomly from and put the ones you want "higher weighted" in the array more times, like buying more raffle tickets
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

magicrobotmonkey wrote:create an array with image names to choose randomly from and put the ones you want "higher weighted" in the array more times, like buying more raffle tickets
aha, you may be oto something there. Like if i weight it 5, it appears in the array 5 times.

Hmmmm....

Mark
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Actually could do it like this

Code: Select all

priority=array( 
    "HI", 
    "HI", 
    "HI", 
    "MED", 
    "MED", 
    "LO" 
); 

$pri=rand(0,sizeof($priority)-1); 
$query="Select * FROM $ads_image WHERE status=1 AND priority=" . $priority[$pri];
Mark
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

Yea that was more what I meant then the first one. it would be pretty easy to write a function to take an image name, an array, and a weight and insert the image name into the array weight times and the array would just be holding image names so it wouldnt be a size problem.
Post Reply