real_illusions Posted May 23, 2008 Share Posted May 23, 2008 Hi all,I have a simple script to display a random image, however, how can i make it so that image links to its own page?I was thinking of using multi-dimensional arrays, but i cant work out how to write it..This is what i have for the random image at the moment - <?php$randimg = array ('image.jpg', 'image2.jpg', 'image3.jpg');$random_key=array_rand($randimg,1);$file = "$randimg[$random_key]";echo ("<img alt=\"Random Image\" src=\"$file\" />")?>thanks:) Link to comment Share on other sites More sharing options...
Wander Posted May 23, 2008 Share Posted May 23, 2008 you mean something like this?: $images = array( array('image_1.jpg','page_1.php'), array('image_2.jpg','page_2.php'), array('image_3.jpg','page_3.php'),);$img = array_rand($images);$image = $img[0];$link = $img[1];echo "<a href=\"$link\"><img src=\"$image\" /></a>"; Link to comment Share on other sites More sharing options...
real_illusions Posted May 23, 2008 Author Share Posted May 23, 2008 that doesnt work..it outputs nothing in the browser, and this in the source code<a href="Array"><img src="Array" /></a> Link to comment Share on other sites More sharing options...
Wander Posted May 23, 2008 Share Posted May 23, 2008 oh im sorry, not completly awake yet $images = array( array('image_1.jpg','page_1.php'), array('image_2.jpg','page_2.php'), array('image_3.jpg','page_3.php'),);$rand = array_rand($images); //or mt_rand(0,count($images)-1) might be faster, not sure$image = $images[$rand][0];$link = $images[$rand][1];echo "<a href=\"$link\"><img src=\"$image\" /></a>"; Link to comment Share on other sites More sharing options...
real_illusions Posted May 23, 2008 Author Share Posted May 23, 2008 thanks..works fine now:) Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.