So I have the following code with works great for randomly generating position for 5 different sidebar ads, my problem is how to give the ads a link that will always be paired with them.
I’m looking for suggestions from some PHP gurus as to best practices for doing this…
<ul class="top_ads">
<?php
$totalImages = 5;
$all = range(1,$totalImages);
shuffle($all);
foreach ($all as $single) {
echo "<li><a href='' /><img src='"; echo bloginfo('template_url') . "/images/ads/ad_0$single.png' alt='ad' /></li>";
}
?>
</ul>
The simplest way is to have an array of images with links and then have $single be the array index. There are two ways to accomplish this. One is to have a two dimensionsal array that contains both links and images, the other is to have two parallel arrays. Here are both options illustrated: