Jump to content


Photo

Random HTML Code??


  • Please log in to reply
4 replies to this topic

#1 techiefreak05

techiefreak05
  • Members
  • PipPipPip
  • Advanced Member
  • 494 posts
  • LocationER, MN

Posted 06 August 2006 - 10:30 AM

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 shortener with advanced, detailed statistics:

http://tyny.us/

#2 ronverdonk

ronverdonk
  • Members
  • PipPipPip
  • Advanced Member
  • 277 posts
  • LocationNetherlands

Posted 06 August 2006 - 10:35 AM

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.

RTFM is an almost extinct art form, it should be subsidized.

#3 techiefreak05

techiefreak05
  • Members
  • PipPipPip
  • Advanced Member
  • 494 posts
  • LocationER, MN

Posted 06 August 2006 - 10:38 AM

0.o!! um .. how would i do that... ?
Link shortener with advanced, detailed statistics:

http://tyny.us/

#4 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 06 August 2006 - 10:41 AM

You can do it with an array, or with a database.
Array way:
<?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;
?>

The database way:
<?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'];
?>

Orio.
Think you're smarty?

(Gone until 20 to November)

#5 ronverdonk

ronverdonk
  • Members
  • PipPipPip
  • Advanced Member
  • 277 posts
  • LocationNetherlands

Posted 06 August 2006 - 10:45 AM

Something like this.

$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>';


RTFM is an almost extinct art form, it should be subsidized.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users