BBLJ84
BBLJ84

Reputation: 173

php random image function returns the same image if function is called more than once, any way to prevent this?

i have the following array which stores my photos

$photos = array(
    "karate" => array('karate1.gif','karate2.png','karate3.gif','karate4.gif'),
    "judo" => array("judo1.png","judo2.png","judo3.png","judo4.png"),
    "kickboxing" => array("kb1.gif","kb2.png","kb3.gif","kb4.png")
);

and i have my function

function rndImage($category, $photos)
{
    echo "<p>".count($photos[$category]);
    $num = mt_rand(0, count($photos[$category])-1);
    $varIMG = $photos[$category][$num];
    echo $category." = ".$varIMG."<br />";
    if(($key = array_search($varIMG, $photos[$category])) !== false)
    {
        array_splice($photos[$category], $key, 1);
    }
    echo count($photos[$category]);
    return $varIMG."</p>";
}

now this works fine if i only call the function once, however if i call it several times on the page like so

rndImage("karate",$photos);
rndImage("karate",$photos);
rndImage("karate",$photos);
rndImage("judo",$photos);
rndImage("kickboxing",$photos);

i often get results where the image returned is the same like so

4karate = karate1.gif3
4karate = **karate3.gif**3
4karate = **karate3.gif**3
4judo = judo.png3
4kickboxing = kb1.gif3

its is removing the selected image from the array each time the function is ran but it resets each time the function is run too meaning duplicate images can be returned.

Is there a way i can keep a track on which images have been used and therefor not allow them to be chosen next time the function is run on that page?

any ideas would be greatly appreciated

Many Thanks

Upvotes: 0

Views: 55

Answers (3)

Samir Selia
Samir Selia

Reputation: 7065

You can show images sequentially rather than randomizing it to have full control on what is shown.

Have modified your code a bit.

<?php

$photos = array(
    "karate" => array('karate1.gif','karate2.png','karate3.gif','karate4.gif'),
    "judo" => array("judo1.png","judo2.png","judo3.png","judo4.png"),
    "kickboxing" => array("kb1.gif","kb2.png","kb3.gif","kb4.png")
);

$show_image = array(); // Maintain counter for image to be shown

rndImage("karate",$photos);
rndImage("karate",$photos);
rndImage("karate",$photos);
rndImage("karate",$photos);
rndImage("karate",$photos);
rndImage("judo",$photos);
rndImage("kickboxing",$photos);

function rndImage($category, $photos)
{
    global $show_image;

    if(!isset($show_image[$category]))
    {
        // Initially first image will be shown.
        $show_image[$category] = 1;
    }
    else
    {
        // Increment counter by 1 to display next image.
        // If it exceeds the total images, reset the counter to 1

        $show_image[$category] = ($show_image[$category] == count($photos[$category])) ? 1 : $show_image[$category] + 1;
    }

    $num = $show_image[$category] - 1;
    $varIMG = $photos[$category][$num];

    echo $category." = ".$varIMG.PHP_EOL;

    return $varIMG;
}
?>

Output

karate = karate1.gif

karate = karate2.png

karate = karate3.gif

karate = karate4.gif

karate = karate1.gif

judo = judo1.png

kickboxing = kb1.gif

Working Example

Upvotes: 0

trincot
trincot

Reputation: 351338

Make the $photos argument a reference, so that the removals you make on that array apply to the original array, not a copy made by the function (PHP has this habit). Add the ampersand(&):

function rndImage($category, &$photos) {
    // ...etc

Upvotes: 2

Giovazz89
Giovazz89

Reputation: 401

Try calling mt_srand before mt_rand to seed the better random number generator.

Upvotes: 0

Related Questions