1

the following script is perfect for my needs but unfortunately displays very often duplicate images.

How can I modify to fix this issue?

Or in place of this is there a similar script allowing to display random images in one column and each of them its own link?

Thanks.

<?php

function display_random_img($array) {
$key = rand(0 , count($array) -1);
$link_url = $array[$key]['url'];
$alt_tag = $array[$key]['alt'];
$random_img_url = $array[$key]['img_url'];
list($img_width, $img_height) = getimagesize($random_img_url);
return "<a href=\"$link_url\"><img src=\"$random_img_url\" width=\"$img_width\" height=\"$img_height\" alt=\"$alt_tag\" /></a>";
}
//-----------------------
$ads_array = array(
array(
    'url' => 'http://www.mysite.com/',
    'alt' => 'Image1',
    'img_url' => 'http://www.mysite.com/pic1.jpg'
),
array(
    'url' => 'http://www.yoursite.com/',
    'alt' => 'Image2',
    'img_url' => 'http://www.mysite.com/pic2.jpg'
),
array(
    'url' => 'http://www.theirsite.com/',
    'alt' => 'Image3',
    'img_url' => 'http://www.mysite.com/pic3.jpg'
)
);
//-----------------------
$ads_array_1 = array( 
array(
    'url' => 'http://www.mysite.com/',
    'alt' => 'Image1',
    'img_url' => 'http://www.mysite.com/pic1.jpg'
),
array(
    'url' => 'http://www.yoursite.com/',
    'alt' => 'Image2',
    'img_url' => 'http://www.mysite.com/pic2.jpg'
),
array(
    'url' => 'http://www.theirsite.com/',
    'alt' => 'Image3',
    'img_url' => 'http://www.mysite.com/pic3.jpg'
)
);

//-----------------------
$ads_array_2 = array( 
array(
    'url' => 'http://www.mysite.com/',
    'alt' => 'Image1',
    'img_url' => 'http://www.mysite.com/pic1.jpg'
),
array(
    'url' => 'http://www.yoursite.com/',
    'alt' => 'Image2',
    'img_url' => 'http://www.mysite.com/pic2.jpg'
),
array(
    'url' => 'http://www.theirsite.com/',
    'alt' => 'Image3',
    'img_url' => 'http://www.mysite.com/pic3.jpg'
)
);

//-----------------------
echo display_random_img($ads_array);
echo display_random_img($ads_array_1); 
echo display_random_img($ads_array_2);
?>

2 Answers 2

1

If you have just 3 images in your set of images it is a chance of 1/3 that the image is displayed two times. So increase your list of images.

However, you should use mt_rand() what will give you better randomness. But the main problem is the small amount of images


Update:

After thinking a little bit about your question, I think you need something like this:

You'll need just one array:

$ads_array = array(
array(
    'url' => 'http://www.mysite.com/',
    'alt' => 'Image1',
    'img_url' => 'http://www.mysite.com/pic1.jpg'
),
array(
    'url' => 'http://www.yoursite.com/',
    'alt' => 'Image2',
    'img_url' => 'http://www.mysite.com/pic2.jpg'
),
array(
    'url' => 'http://www.theirsite.com/',
    'alt' => 'Image3',
    'img_url' => 'http://www.mysite.com/pic3.jpg'
)
);

And this function which uses shuffle() to generate randomness:

function display_random_images($array, $maxcount = 3) {
    // shuffle $array elements
    shuffle($array);
    $html = '';
    for($i = 0; $i < min(count($array), $maxcount); $i++) {
        $img = $array[$i];
        $link_url = $img['url'];
        $alt_tag = $img['alt'];
        $random_img_url = $img['img_url'];
        list($img_width, $img_height) = getimagesize($random_img_url);
        $html .= "<a href=\"$link_url\"><img src=\"$random_img_url\" width=\"$img_width\" height=\"$img_height\" alt=\"$alt_tag\" /></a>";
    }
    return $html;
}

Now call the function, it will output $maxcount images, which are per default 3

echo display_random_images($ads_array);

If you have more images, then you can call it like this:

echo display_random_images($ads_array, 10); // 10 imgs or whatever
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your answer, but as i want to use this script for ad banners on my site, i can't add more images.
I tried your suggestion but it doesn't work. No picture displayed. I didn't find where is the issue :( Did you test it? It happens only to me? Thanks
There was a little typo. I've used once $maxount instead of $maxcount. Fixed this. Can you try again?
0

This might work, or at least that' the idea :

function display_random_img($array) {
    static $displayedImages = array();
    // ...
    $length = count($array);
    if(count($displayedImages) === $length) {
        // Every image was picked, restarting.
        $displayedImages = array();
    }
    while(true) {
        $rand = rand(0, $length - 1);
        if( ! isset($array[$rand])) {
            // Image at $rand not shown yet.
            $displayImages[$rand] = true;
            break;
        }
    }
    $imageDetails = $array[$rand];
    // ...
}

Could be slow with huge image arrays though

2 Comments

It doesn't work :( This is the message that the system displays: "PHP cpu time limit exceeded in display_random_img()..."
I'm just giving you the idea, it's up to you to find eventual mistakes.

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.