下面的脚本非常适合我的需要,但不幸的是经常显示重复的图像。
如何修改以解决此问题?
或者在这个地方有一个类似的脚本允许在一列中显示随机图像,每个图像都有自己的链接?
谢谢。
<?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);
?>
如果您的图像集中只有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
这可能行得通,或者至少是这个想法:
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];
// ...
}
对于巨大的图像数组可能很慢,但是