 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
11-27-2007, 02:57 AM
|
#1 (permalink)
|
|
The Acquainted
Join Date: Sep 2007
Posts: 133
Thanks: 6
|
Random Items without a Database
If you ever wanted to show something random without using the database then this is a decent method:
php Code:
<?php$banner[]= array("image.gif", "http://domain.com/"); $banner[]= array("image2.gif", "http://www.domain.domain.co.uk/"); $banner[]= array("image3.gif", "http://search.mysearch.com"); $rand = rand(0, count($banner)- 1); ?><p align= "center"> <a href= <?= $banner[ $rand][ 1]?>><img src= "<?=$banner[$rand][0]?>" alt= "" width= "468" height= "60" border= "0"></a> </p>
More of a little script than a tutorial, thanks :)
|
|
|
11-27-2007, 03:12 AM
|
#2 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
Good tip! There is also the array_rand way. However, as I was trying it just now, it seems to love its zeros, so I decided to do a little test as well. This is the array_rand code:
php Code:
$aFilms = array("V For Vendetta", "Requiem For A Dream", "Jacob's Ladder"); echo $aFilms[array_rand ($aFilms)];
And this is my test code as it appeared to be quite fond of returning zero:
php Code:
$aCount = array(0, 0, 0); $aFilms = array("V For Vendetta", "Requiem For A Dream", "Jacob's Ladder"); for($iIndex = 0; $iIndex <= 5000; $iIndex++ ){ $aCount[array_rand ($aFilms)]++; }print_r($aCount);
Results were quite surprising as I thought zero would have been much more dominant:
Quote:
Array
(
[0] => 1701
[1] => 1637
[2] => 1663
)
|
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|
11-27-2007, 03:32 AM
|
#3 (permalink)
|
|
The Acquainted
Join Date: Sep 2007
Posts: 133
Thanks: 6
|
That looks pretty cool, did a test (10,000 times in a loop):
rand();
Result 1: Page generated in 1.496 seconds.
Result 2: Page generated in 1.400 seconds.
Result 3: Page generated in 1.486 seconds.
array_rand();
Result 1: Page generated in 0.087 seconds.
Result 2: Page generated in 0.078 seconds.
Result 3: Page generated in 0.076 seconds.
Didn't realise the results would vary so much.
|
|
|
11-27-2007, 03:37 AM
|
#4 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
That is rather surprising, actually. I'd have thought rand would have been faster. In fact, I was under the impression that the array_rand function would have been a wrapper for the rand function, with 1 or 2 adaptations in that it recognised the array's upper boundary by calling the count function as well.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|
11-27-2007, 02:32 PM
|
#5 (permalink)
|
|
The Wanderer
Join Date: Nov 2007
Posts: 20
Thanks: 1
|
My favorite way of doing this is to use the shuffle function, to use the example from above:
PHP Code:
$banner = array();
$banner[]=array("image.gif","http://domain.com/");
$banner[]=array("image2.gif","http://www.domain.domain.co.uk/");
$banner[]=array("image3.gif","http://search.mysearch.com");
shuffle($banner);
$numBanners = 1;
for ($i=0; $i<$numBanners; $i++) {
print "".$banner[$i][0];
}
This way you won't duplicate ads if you are showing more than one at a time. I haven't tested its efficiency, but since you randomize the array once I'm guessing its pretty efficient.
Mubs
|
|
|
|
11-27-2007, 03:03 PM
|
#6 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
Interesting, Mubs. The only resevation I'd have about using the array_shuffle function is that it alters the arrangement of the items in the array permanantly. IE: you cannot return them to their previous state unless of course one of the *sort* functions does it - for which you would have needed to construct the array into some sort of arrangement to begin with, such as alphabetical.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|
11-27-2007, 06:59 PM
|
#7 (permalink)
|
|
The Wanderer
Join Date: Nov 2007
Posts: 20
Thanks: 1
|
Agreed, in my case I didn't need to re-sort just randomize and display so no problems there.
If its a short, simple array, it's easy to just make a copy of the array for randomizing and leave the original sorted as you need.
Mubs
|
|
|
|
11-28-2007, 11:31 AM
|
#8 (permalink)
|
|
The Acquainted
Join Date: Nov 2007
Location: Sweden
Posts: 106
Thanks: 13
|
My way of outputing a random logo
PHP Code:
function random_logo() {
$szLogo[0] = "mb_v6_logga_800x150.jpg";
$szLogo[1] = "mb_v6_logga_800x150_2.jpg";
$szLogo[2] = "mb_v6_logga_800x150_3.jpg";
$szLogo[3] = "mb_v6_logga_800x150_4.jpg";
$szLogo[4] = "mb_v6_logga_800x150_5.jpg";
$szLogo[5] = "mb_v6_logga_800x150_6.jpg";
$szLogo[6] = "mb_v6_logga_800x150_7.jpg";
$szLogo[7] = "mb_v6_logga_800x150_8.jpg";
$szLogo[8] = "mb_v6_logga_800x150_9.jpg";
$szLogo[9] = "mb_v6_logga_800x150_10.jpg";
$szLogo[10] = "mb_v6_logga_800x150_11.jpg";
$szLogo[11] = "mb_v6_logga_800x150_12.jpg";
$szNumber_of_logos = count($szLogo);
$iRand_nr = mt_rand(0, $szNumber_of_logos);
$szRandomLogo = $szLogo[$iRand_nr];
// If no logo is returned use a standard one
if ($szRandomLogo == "") {
$szRandomLogo = "mb_v6_logga_800x150.jpg";
}
return $szRandomLogo;
}
This is a little function i wrote quickly for a website i had a few years ago, were i wanted to show a random header with the site logo. It´s not wery effecient i guess neither does it check that the file is actually there so theres room for improvement.
what it does is just return a filename of a logo to display.
Tried to apply your prefered variable namning convention :D
Hope it give someone ispiration.
Can´t remember why i numbered the array thoug.
__________________
Of course the whole point of a doomsday machine, would have been lost if you keep it a secret.
|
|
|
|
12-04-2007, 12:55 PM
|
#9 (permalink)
|
|
The Contributor
Join Date: Dec 2007
Location: Durban, South Africa
Posts: 51
Thanks: 1
|
Excuse the newbie, but isn't it best practice to seed the random function before calling?
|
|
|
|
12-04-2007, 01:29 PM
|
#10 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
You're right in a way, but I believe they now seed automatically before spitting you out a number. As of around PHP version 4.2.0, apparently. Before 4.2.0 though you most certainly would seed the number.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|
12-04-2007, 01:50 PM
|
#11 (permalink)
|
|
The Contributor
Join Date: Dec 2007
Location: Durban, South Africa
Posts: 51
Thanks: 1
|
Quote:
|
Originally Posted by php.net
Note: As of PHP 4.2.0, there is no need to seed the random number generator with srand() or mt_srand() as this is now done automatically.
|
Just read that off php.net... should've checked first, my bad. 
|
|
|
|
12-05-2007, 02:50 AM
|
#12 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
The funny thing is, when I was a PHP beginner, and maybe even an intermediate as well, I used PHP versions earlier than 4.2.0, and I never remember seeding before I output a random value. Oooops!?
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|