要做到這一點,你需要存儲一個視圖計數器與用戶一個cookie,然後根據該計數器顯示:
session_start();
if(!isset($_SESSION['views'])) {
$_SESSION['views'] = 0;
}
else {
$_SESSION['views']++;
}
,然後顯示:
<?php if($_SESSION['views'] % 2 == 0): ?>
<a href="http://www.link1.tld"><img src="files/image1.jpg" border="0" /></a>
<? endif; ?>
<a href="http://www.link2.tld"><img src="files/image2.jpg" border="0" /></a>
<?php if($_SESSION['views'] % 2 == 1): ?>
<a href="http://www.link1.tld"><img src="files/image1.jpg" border="0" /></a>
<? endif; ?>
如果查看計數器甚至會首先打印image1。如果它很奇怪,它會打印第二個。
縮放這兩個以上的圖像可以做這樣的:
// map of images to URLs
$images = array(
'image1.jpg' => 'http://www.link1.tld',
'image2.jpg' => 'http://www.link2.tld',
'image3.jpg' => 'http://www.link3.tld',
'image4.jpg' => 'http://www.link4.tld',
);
// reorder the list of images based on the current view count
$ordered = array_merge(array_slice($images, $_SESSION['views'] % count($images)), array_slice($images, 0, $_SESSION['views'] % count($images)));
,然後顯示只有通過有序列表循環:
<?php foreach($ordered as $image => $url): ?>
<a href="<?php echo $url; ?>"><img src="files/<?php echo $image; ?>" border="0" /></a>
<?php endforeach; ?>
你希望它永遠旋轉或可隨機? – nortron
理想情況下,我希望它旋轉 - 我已更新標題以反映,謝謝。 – scraft3613