techiefreak05 Posted August 6, 2006 Share Posted August 6, 2006 I was wondering how you could generate a random html code on a php page. say if the user refreshes the page, a different link appears but in the same spot. Quote Link to comment Share on other sites More sharing options...
ronverdonk Posted August 6, 2006 Share Posted August 6, 2006 Store your links in an array and pick, before displaying the HTML, a link at random. Then store that in your HTM link statement. After that display the HTML code. Quote Link to comment Share on other sites More sharing options...
techiefreak05 Posted August 6, 2006 Author Share Posted August 6, 2006 0.o!! um .. how would i do that... ? Quote Link to comment Share on other sites More sharing options...
Orio Posted August 6, 2006 Share Posted August 6, 2006 You can do it with an array, or with a database.Array way:[code]<?php//Create an array with the links$links=array('<a href="www.domain1.com">domain1</a>','<a href="www.domain2.net">domain2</ a>');$rand=array_rand($links); //pick a random linkecho $rand;?>[/code]The database way:[code]<?php//Let's say the table "links" has a column called "link" (values look like <a href...>...</ a>)$sql="SELECT link FROM `links` ORDER BY RAND() LIMIT 1";$result=mysql_query($sql);$array=mysql_fetch_array($result);echo $array['link'];?>[/code]Orio. Quote Link to comment Share on other sites More sharing options...
ronverdonk Posted August 6, 2006 Share Posted August 6, 2006 Something like this.[code]$links = array("link1.php", "link2.php", "link3.php","link4.php", "link5.php", "link6.php");$idx = rand(0, 5);echo '<a href="'.$links[$idx].'">'. $links[$idx]. '</a>';[/code] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.