homepage Welcome to WebmasterWorld Guest from 54.161.175.231
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
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.
phidentity




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

Hi,

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?

Jon

 

seomike2003




msg:1313397
 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");
$linkT=array("site1","site2","site3");
$r=rand(0,2);
<a href="<? echo ("http://$links[$r]");?>"><? echo $linkT[$r];?></a>

rpking




msg:1313398
 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.

BitBanger




msg:1313399
 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);
shuffle($links);
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.

phidentity




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

cheers all :)

Jon

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