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>";
}
?>
Random image link code
Moderator: General Moderators
Re: Random image link code
Please use
Code: Select all
[/code ] tags when posting code.
I believe the PHP function [url=http://us3.php.net/array_rand]array_rand()[/url] would come in handy here.
First you should put everything into an array. Set the keys to start at 1 to correspond to your image naming convention. If you want to add more links later, just add them and the code in the next section can compensate.
[syntax=php]$linkarray = array(1 => "http://www.digitalfg.com",
"http://www.google.com",
"http://www.newyorkyankees.com",
"http://www.newyorkyankees.com");[/syntax]
Then to get a random number, do this:
[syntax=php]$random = array_rand($linkarray);[/syntax]
To use the random number, just use the variable by itself. To get the link referenced by the random number, use the variable as the array key.
[syntax=php]echo "<a href=\"{$linkarray[$random]}\">";
echo "<img src=\"$path".$random.".jpg\" border=\"0\">";
echo "</a>";[/syntax]
None of the code I entered is tested so I apologize if there are any syntax errors. Just tried to at least give you an idea of how to handle your request.
Another helpful tip. You can put a backslash in front of PHP special chars like quotes \" and dollar signs \$ to escape them in a string. Double quotes around html tag attributes is the W3C compliance standard.Re: Random image link code
You could also create a 3 dimensional array and put the img url into the array along with the link. Then you don't have to comply to a file naming convention when associating links to pictures.
And of course, if the links and images were stored in a DB table, you could select random row out of the database to build your linked image and completely omit the mess of hardcoded array values.
PHP is wonderful. 
Code: Select all
$linkarray[] = ('image'=>'digital.jpg', 'url'=>'http://www.digitalfg.com');
$linkarray[] = ('image'=>'google.jpg', 'url'=>'http://www.google.com');
$linkarray[] = ('image'=>'ny1.jpg', 'url'=>'http://www.newyorkyankees.com');
$linkarray[] = ('image'=>'ny2.jpg', 'url'=>'http://www.newyorkyankees.com');
$img = $linkarray[$random]['image'];
$url = $linkarray[$random]['url'];Code: Select all
SELECT * FROM my_table ORDER BY RAND() LIMIT 1