homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Random Variable Echo
I want to use an array to print some words in varying order.

 1:16 pm on Jan 19, 2004 (gmt 0)


I need help. I want to use 'mt_rand()', to print a series of links in different order. I thought about using an array.

How can I do this so the same link isnt printed twice, or some of the links don't appear.

It's so I can produce a list of links in random order so nobody gets missed out.

Any idea how?




 1:44 pm on Jan 19, 2004 (gmt 0)

Hey try this out. Just remember to that arrays start their count at 0 so if you add 10 random links into the array then your rand function should be rand(0,9)

$links= array("www.site1.com","www.site2.com","www.site3.com");
<a href="<? echo ("http://$links[$r]");?>"><? echo $linkT[$r];?></a>


 1:54 pm on Jan 19, 2004 (gmt 0)

If you want to print the entire list of links randomly, look at using array_rand() to randomize your array before printing it out.


 3:36 pm on Jan 19, 2004 (gmt 0)

Actually using shuffle() might be a better idea. All you would need to do is to seed the random number generator, shuffle the array, then dump the entire array.

function dump_rand_links ($links) {
srand((float)microtime() * 1000000);
foreach ($links as $link) {
echo "<a href=\"{$link}\">{$link}</a><br />";

This way you don't need to worry about links being missed or duplicated.


 12:16 am on Jan 21, 2004 (gmt 0)

cheers all :)


Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved