astraeuz
01-28-2007, 07:32 AM
I have a list of 100 URLs and want to display 20 of them, randomly. But, i'm not a PHP programmer so please guide me to build an array of these URLs and display them...
the array would look like...
[0, 0] = "http://www.google.com/?q=", "Google Search";
[0, 1] = "http://www.yahoo.com", "Yahoo! Search";
[0, 2] = "http://www.msn.com", "MSN Search";
.....
.....
.....
.....
.....
[0,100] = "http://www.....", "description";
for($i=0; $i<20, $i++)
{
RANDOMIZE;
echo("<a href='url'>desc</a>");
}
Please help me properly format above array and do the task.
Thanks a lot.
mikey1090
01-28-2007, 08:06 AM
$rand = (1,100);
$url = $array_name[0,$rand];
echo "<a href=$url>";
get the idea?
horizon
01-28-2007, 09:32 AM
This line:
$rand = (1,100);
for:
$rand = rand(1,100);
;)
Xenatino
01-28-2007, 10:02 AM
Just put something together quickly as an example for you, or someone else to build on. I wasn't sure on the exact structure of your array, so if may need tweaking to accommodate it.
Also, this example will only display each domain once, as just using rand() may have the same domain 20 times (in an extreme situation).
http://www.1921681100.com/public/144.php
astraeuz
01-28-2007, 10:18 AM
Xenatino,
You rock, you just rock. Thanks a lot :D
maxymizer
01-28-2007, 11:08 AM
This is a perfect example where a principle named K.I.S.S. should be applied :)
Why go to so much trouble when you can just use array_rand()?
$urls[] = 'some url';
$urls[] = 'some url 2';
/*
and so on until you populate the array
*/
$random_index = array_rand($urls); // now you have a random index of the array
echo $urls[$random_index]; // voila
Why is the solution proposed by mikey1090 ok but not quite ok? Array indexes do not necessarely have to be indexed by numbers, and if they are there's no guarantee that number 55 will be used (because you might delete that specific array index).
Xenatino
01-28-2007, 11:33 AM
To be honest, that answer totally escaped me, I had completely forgotten about array_rand().
Just took a quick look, and you can actually specify the number of results required as an argument. Have to make sure that makes it onto my cheatsheet!
maxymizer
01-28-2007, 12:47 PM
Yep, you can specify the number of results but then you get an array of indexes instead of an integer so you have to be careful about that.