随机图像作为与图像相关的链接

时间:2008-10-23 15:43:40

标签: scripting random

我正在寻找一个脚本,它会在pageload上调用链接到其相应站点的随机图像。有人知道javascript或php方式吗?

1 个答案:

答案 0 :(得分:1)

<?php

$random = array(
  array('image' => 'http://example.com/image1.jpg', 'url' => 'http://example1.com/'),
  array('image' => 'http://example.com/image2.jpg', 'url' => 'http://example2.com/'),
  array('image' => 'http://example.com/image3.jpg', 'url' => 'http://example3.com/'),
  array('image' => 'http://example.com/image4.jpg', 'url' => 'http://example4.com/'),
);

$current = rand(0, count($random) - 1);
print "<a href=\"" . $random[$current]['url'] . "\"><img src=\"" . $random[$current]['image'] . "\" alt=\"\" /></a>\n";

?>

快速简便。可能不是最好的方式 - 我个人将它挂钩到数据库而不是静态数组 - 但它会让你在几秒钟内启动并运行。