2013-04-20 111 views
1

以下脚本非常适合我的需求,但很不幸显示的是经常出现的重复图像。如何避免显示从随机图像脚本重复?

如何修改以解决此问题?

或者取而代之的是,有一个类似的脚本允许在一列中显示随机图像,并且每个图像都有自己的链接?

谢谢。

<?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); 
?> 

回答

1

如果在您的图像集中只有3张图像,则图像显示两次的几率为1/3。所以增加你的图像列表。

但是,你应该使用mt_rand()什么会给你更好随机性。但主要的问题是图像


更新量小:

思考一点点关于你的问题,我想你需要的东西是这样的:

你只需要一个阵列:

$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' 
) 
); 

而这种功能,它使用shuffle()生成随机性:

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; 
} 

现在调用的函数,它将输出$maxcount图片,如果你有更多的图像,也就是每默认3

echo display_random_images($ads_array); 

,那么你可以这样调用:

echo display_random_images($ads_array, 10); // 10 imgs or whatever 
+0

可能会缓慢与巨大的图像阵列感谢您的答案,但因为我想在我的网站上使用此脚本的广告横幅,我不能添加更多的图像。 – mattew 2013-04-20 00:40:08

+0

检查我的更新 – hek2mgl 2013-04-20 00:43:21

+0

我尝试了你的建议,但它不起作用。没有图片显示。我没有找到问题出在哪里:( 你测试过了吗?它只发生在我身上吗?谢谢 – mattew 2013-04-21 15:38:12

0

这可能工作,或者至少是这个想法:

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]; 
    // ... 
} 

虽然

+0

它不起作用:( 这是系统显示的消息: “超过PHP cpu时间限制display_random_img()...“ – mattew 2013-04-21 15:44:14

+0

我只是给你这个想法,这取决于你找到最终的错误。 – Virus721 2013-04-22 15:38:29