samoht Posted August 27, 2007 Share Posted August 27, 2007 Hello all, Does anyone know of an easy way to turn a hard coded list of links (these links are <a href='something'><img...) into a random display?? most likely triggered by the page refresh. Link to comment https://forums.phpfreaks.com/topic/66903-solved-random-link-list/ Share on other sites More sharing options...
uwannadonkey Posted August 27, 2007 Share Posted August 27, 2007 i dont know if its the MOST efficient way, but heres an idea, set a variable = rand($min,$max) and then set each number == to a a value between min and max, FOR example: $rand=Rand(1,4) if($rand == 1) { echo"the link"; } elseif($rand==2) { echo"link 2"; } and so on, it provides randomness, and hard coded links =D Link to comment https://forums.phpfreaks.com/topic/66903-solved-random-link-list/#findComment-335372 Share on other sites More sharing options...
lemmin Posted August 27, 2007 Share Posted August 27, 2007 $list = array("<a href='something'><img></a>", "<a href='something2'><img></a>"); for ($i=0;$i<rand(1, count($list));$i++) $link = $list[$i]; If the list is an array, you can do it like that. You can do the same thing if the list is in a file. EDIT: Actually, if it is in an array, you can just use array_rand(). http://us2.php.net/manual/en/function.array-rand.php Link to comment https://forums.phpfreaks.com/topic/66903-solved-random-link-list/#findComment-335382 Share on other sites More sharing options...
samoht Posted August 27, 2007 Author Share Posted August 27, 2007 thanks for the help, I tried: <?php $list = array('<a href="http://www.link1.com" target="_blank"><img src="assets/logo1.jpg" /></a><br>','<a href="http://link2.org" target="_blank"><img src="assets/logo2.jpg" /></a>','<a href="http://www.link3.org" target="_blank"><img src="assets/logo3.jpg" /></a>'); $rand_link = array_rand($list,3); echo $list[$rand_link[0]]. "\n"; echo $list[$rand_link[1]]. "\n"; echo $list[$rand_link[2]]. "\n";?> Which seems to work fine - and was quite easy! Thanks again Link to comment https://forums.phpfreaks.com/topic/66903-solved-random-link-list/#findComment-335411 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.