您的问题有很多可能的解决方案。
我会告诉你我会做什么:
-
创建一个包含您希望随机显示的图像数据的数组
1b.如果您有很多图像 (100+),请只在数组中放入有限的数量以获得更好的性能。限制用于获取图像文件路径的函数中的返回量(Database Query 或文件系统,例如scandir()、glob() 或 DirectoryIterator)
1c。我建议您将图像数据存储在 MySQL 数据库中。它更快,您可以用它做更多事情。请参阅下面的示例。
-
然后要随机化图像的顺序,您可以使用类似shuffle()
-
所以,例如:
<?php
$images = array();
// Get the images (just for this example, do it however you like).
// With sql queries ORDER BY RAND() could be used to randomize the results returned, but
// it depends on the amount of data stored in the db. The more the slower the query will be.
$database->query('SELECT id, name, title, url, anchor_text, path FROM featured_images ORDER BY id LIMIT 10');
// Randomize the images
shuffle($images);
// Echo the randomized images
echo '<ul>';
foreach ($images as $image)
{
echo '<li>';
echo '<img src="'.$image->path.'" alt="" title="'.$image->title.'" class="...>';
echo '<p><a href="'.$image->url.'">'.$image->anchor_text.'</a></p>';
echo '</li>';
}
echo '</ul>';
在示例代码中,我使用了一个基本的 SQL 查询。要使用 SQL 原生的RAND(),可以使用以下查询 (注意 LIMIT 10 是可选的,但建议在表包含大量图像时使用,因此根据需要调整 10)(另请阅读上面示例中关于性能的 sql 查询的评论):
SELECT id, title, path FROM featured_images WHERE id IN(SELECT id FROM featured_images ORDER BY RAND() LIMIT 10)
我不知道您是如何以及是否从中获取图像数据的,但是从目录中读取数据可以像这样 完成(请注意,这种方法不能包含标题和锚文本,请使用来自的数据库变体取而代之的是上面的例子,因为它也更快):
$dir = new DirectoryIterator("/path/to/images");
foreach ($dir as $file)
{
$images[] = array(
'path' => $file->getPath(), // file path w/o filename
'name' => $file->getFilename(),
);
}
试一试,摆弄它并通读 PHP 手册 (我将函数名称与相应的手册页链接)。
祝你好运。
编辑:
我正在更新此答案以根据提问者的this comment 提供an array only solution:
嗨,Markus,我没有使用 db,因为它只有几张图像,如果我们使用数组,你能给我一些关于在 foreach 循环中编写代码的提示吗?谢谢你,阿奈
好的,所以您想使用包含少量图像的数组。我假设您是"hard-coding" 将数据写入脚本。我们开始:
<?php
// Since the image paths given in your example both conside in the same directory,
// you can create a base_path variable to prevent repeating the path over and over again
$imgBasePath = 'http://www.prevention.com/sites/default/files/imagecache/102x104/images/news/featured_images/';
// Create the array containing the required image data (hard-coded)
$images = array(
array(
'path' => $imgBasePath . '156854573-raw-meat-lab-628x363.jpg',
'title' => '',
'width' => '102',
'height' => '104',
'link_url' => '/food/healthy-eating-tips/theres-test-tube-burger-your-future',
'link_text' => 'There\'s A Test-Tube Burger In Your Future',
),
array(
'path' => $imgBasePath . '86541117-grocery-shopping-produce-organic-label-mixed-greens-lg.jpg',
'title' => '',
'width' => '102',
'height' => '104',
'link_url' => '/food/smart-shopping/connecticut-gmo-labeling-law',
'link_text' => 'GMOs-Exposed!',
),
);
// Now loop through the $images array and create the desired list
// I'm putting the html into a var to be more flexible of where to echo the created html
$html = '';
foreach ($images as $image)
{
$html .= '<li>';
$html .= '<img src="'.$image['path'].'" alt="" title="'.$image['title'].'" class="imagecache imagecache-102x104" width="'.$image['width'].'" height="'.$image['height'].'">';
$html .= '<p><a href="'.$image['link_url'].'">'.$image['link_text'].'</a></p>';
$html .= '</li>';
}
// Finally echo the list to the browser
echo '<ul>'.$html.'</ul>';
Hard-coded 表示您将数据放入未动态更改的脚本中。从数据库中获取数据可以更改(例如更新或添加),因此它是动态的。示例中的完成方式称为硬编码)。
The foreach loop 遍历 $images 数组并返回循环内的每个单独的 $image。要访问 $image 数据,可以使用 $image['path'] 之类的东西。非常直接。
这是一个如何创建数组并在 foreach 循环中使用它们的基本示例。
编码愉快。