Random image link code
Posted: Thu Feb 12, 2009 1:41 pm
Hello all,
I know there is a better way to clean this code up without the use of the "if" statement I think an array should do it but Im not sure how to make it work. Please help me.
Thanks,
-Curtis
<?php
$images = 4;
$link1 = "http://www.digitalfg.com";
$link2 = "http://www.google.com";
$link3 = "http://www.newyorkyankees.com";
$link4 = "http://www.newyorkyankees.com";
$path = "images/";
$random = rand(1,$images);
if ( $random == "1" ) {
echo "<a href=$link1>";
echo "<img src=$path"."$random".".jpg"." border=\'0\'>";
echo "</a>";
}
if ( $random == "2" ) {
echo "<a href=$link2>";
echo "<img src=$path"."$random".".jpg"." border=\'0\'>";
echo "</a>";
}
if ( $random == "3" ) {
echo "<a href=$link3>";
echo "<img src=$path"."$random".".jpg"." border=\'0\'>";
echo "</a>";
}
if ( $random == "4" ) {
echo "<a href=$link4>";
echo "<img src=$path"."$random".".jpg"." border=\'0\'>";
echo "</a>";
}
?>
I know there is a better way to clean this code up without the use of the "if" statement I think an array should do it but Im not sure how to make it work. Please help me.
Thanks,
-Curtis
<?php
$images = 4;
$link1 = "http://www.digitalfg.com";
$link2 = "http://www.google.com";
$link3 = "http://www.newyorkyankees.com";
$link4 = "http://www.newyorkyankees.com";
$path = "images/";
$random = rand(1,$images);
if ( $random == "1" ) {
echo "<a href=$link1>";
echo "<img src=$path"."$random".".jpg"." border=\'0\'>";
echo "</a>";
}
if ( $random == "2" ) {
echo "<a href=$link2>";
echo "<img src=$path"."$random".".jpg"." border=\'0\'>";
echo "</a>";
}
if ( $random == "3" ) {
echo "<a href=$link3>";
echo "<img src=$path"."$random".".jpg"." border=\'0\'>";
echo "</a>";
}
if ( $random == "4" ) {
echo "<a href=$link4>";
echo "<img src=$path"."$random".".jpg"." border=\'0\'>";
echo "</a>";
}
?>