Randomizing Content with PHP
- ID: PHP - 15
- Date Added: 2007-12-22
- Category: PHP
- Difficulty: Novice
- Author: Rei
<?php
$filename = "random.txt";
$file = file($filename);
srand((double)microtime()*1000000);
while ($Random == "") {
$Random = ereg_replace("\n","",$file[rand(0,count($file))]);
}
print "$Random";
?>
This is one way of doing this. You must create a file called random.txt, and insert anything you want. However, you must separate each link or value with a new line. This is a sample of a text page:<a href="link.html" target=_blank>Akatsuki Designs</a> <a href="link.html" target=_blank>Kyoukei</a> <a href="link.html" target=_blank>JSound</a> Quote 1 Quote 2 Quote 3 <img src="something.gif"> <img src="something.gif">The second method is simpler, requires no .txt files, but you will define your random content within the php. This is recommended for when you don't have a lot of things to randomize:
<?php $quotes[] = "Quote1"; $quotes[] = "Quote2"; $quotes[] = "Quote3"; $quotes[] = "Quote4"; $quotes[] = "Quote5"; $quotes[] = "Quote6"; srand ((double) microtime() * 1000000); $randomquote = rand(0,count($quotes)-1); echo " " . $quotes[$randomquote] . " "; ?>You can modify
$quotes[] = "Quote1"; to your heart's content =)« back · clear ¦ reload · forward »
Akatsuki Designs © Rei [2006-2008]. Site, layout, and content © Rei. All rights reserved. Valid CSS and HTML Fight Spam! | Spot a typing error? | HostTracker



