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. Link to comment https://forums.phpfreaks.com/topic/16697-random-html-code/ 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. Link to comment https://forums.phpfreaks.com/topic/16697-random-html-code/#findComment-70124 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... ? Link to comment https://forums.phpfreaks.com/topic/16697-random-html-code/#findComment-70127 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. Link to comment https://forums.phpfreaks.com/topic/16697-random-html-code/#findComment-70128 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] Link to comment https://forums.phpfreaks.com/topic/16697-random-html-code/#findComment-70131 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.