0

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

2
  • Well, it's random. Perhaps you're rather looking for shuffling a list of images into a random order…? Commented Dec 28, 2016 at 8:54
  • thats an interesting idea, what would you suggest? thanks Commented Dec 28, 2016 at 23:27

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for your reply, this kind of works, it removes the image from the array & remembers it next time the function is called meaning the same image wont appear twice, the main issue is that this code now stops it being a randomly generated function, the outcome is the same every time. for example i now get 4karate = karate1.gif3 4karate = karate3.gif3 3karate = karate2.gif2 2karate = karate4.gif1 4judo = judo.png3 4kickboxing = kb1.gif3 every time, the randomness is no longer a feature of the function. Is there a way to get this to randomise the images each time the page loads? thanks
I cannot reproduce the behavior you report: when I run this code, the produced order is different most of the time. You could look into calling mt_srand(), but the documentation says PHP does this automatically already....
ah i had an extra bit of code in, which i added when trying one of the suggestions below, this was causing the issue. have removed the code and it now works superbly!!! thank you very much!!
0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.