i'm creating this banner rotating PHP script (I'm new to PHP) and was wondering why my cases aren't working. I can get the random image to show via the array although I can't seem to make the cases work.
I would like it so if the image from the random array is the 0 image, it will say visit jackhair.co.uk and if the random image from the array is 2 it will say jamiehair.co.uk
<?php
/*Creates an array called banners which holds all the images of banners */
$banners=array("img/reversessh.jpg","img/failtwo.jpg","img/iptable.jpg");
$random = ($banners[(rand(0,2))]);
$visit = $random;
switch($visit)
{
case $visit = 0:
Echo "Visit www.jackhair.co.uk";
break;
case $visit = 1:
Echo "Visit www.lukehair.co.uk";
break;
case $visit = 2:
Echo "Visit www.jamiehair.co.uk";
break;
default:
null;
}
?>
<img src="<?= $random ?>" alt="Rotating Banners" />
Hope you can help! Thankyou.
Do:
$visit = rand(0,2);
$random = $banners[$visit];
Instead of other way around. That way you keep the int value random in the $visit variable and not a image url.
Your syntax is incorrect. You only need to use the value in the case statement:
switch($visit)
{
case 'img/reversessh.jpg':
echo "Visit www.jackhair.co.uk";
break;
case 'img/failtwo.jpg':
echo "Visit www.lukehair.co.uk";
break;
case 'img/iptable.jpg':
echo "Visit www.jamiehair.co.uk";
break;
default:
null;
}