Jump to content

Archived

This topic is now archived and is closed to further replies.

techiefreak05

Random HTML Code??

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 link
echo $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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.