/    Sign up×
Community /Pin to ProfileBookmark

Image rotation

Hi!
I need help to create a function than show me image in random order.
I have 3 images and that have different weight:
Image A: weight 2
Image B: weigth 4
Image c: weigth 7
The first thime I open my page I want see Image C (highest weight). The secnd thime I refresh page I want see Image B. My problem is that I can see Image C 70% of the time, Image B 40% of time,…
Can you help me to create something that help me in this..thanks!

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@Jarrod1937Oct 12.2010 — Hi!

I need help to create a function than show me image in [B]random order[/B].

I have 3 images and that have different weight:

Image A: weight 2

Image B: weigth 4

Image c: weigth 7

[B]The first thime I open my page I want see Image C (highest weight). The secnd thime I refresh page I want see Image B.[/B] My problem is that I can see Image C 70% of the time, Image B 40% of time,...

Can you help me to create something that help me in this..thanks![/QUOTE]

If you want C to show the first time you load the page and B the second time, then you're not wanting a random order, are you? Even with weighting, "A" may show first if its truly random. So first figure out what you want, then after that, post the existing code you have and we can take a look.
Copy linkTweet thisAlerts:
@SrWebDeveloperOct 12.2010 — Untested, worth a try:

[code=php]

$img_arr=array("A"=>2,"B"=>4,"C"=>7);
echo "<img src='".w_rand($img_arr)."' alt='' />";
FUNCTION w_rand($weights) {
$r = MT_RAND(1,1000);

$offset = 0;

FOREACH ($weights AS $k => $w) {

$offset += $w*1000;
IF ($r <= $offset) { RETURN $k; }

}
} [/code]


Gives higher priority to higher weights.

-jim
×

Success!

Help @ojal__ spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...