Dynadot Aftermarket: discover domain gems worth investing in. Visit our aftermarket.

Randomize code?

SpaceshipSpaceship
Spacemail by SpaceshipSpacemail by Spaceship
Namecheap AuctionsNamecheap Auctions
Watch

I Brian

New Member
Impact
0
Sorry to ask a simple question, but I'm looking to randomise a display of content, which are called up with basic PHP include files.

So if there are 5 images, I'm calling them up as:

Code:
<!-- site content above --> 
<?PHP include("/home/user/public_html/section1.php"); ?> 
<?PHP include("/home/user/public_html/section2.php"); ?> 
<?PHP include("/home/user/public_html/section3.php"); ?> 
<?PHP include("/home/user/public_html/section4.php"); ?> 
<?PHP include("/home/user/public_html/section5.php"); ?>
<!-- footer below -->


How would I randomise with a script within the body itself?

Many thanks for any replies.
 
0
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
do you want to randomize the section1, section2, section3, etc?

or do you want to randomize something INSIDE section1, section2, etc?

here's a link to the php random manual anyway: http://us3.php.net/rand
 
0
Close
That's it rand() should do the trick.
I would do something like... (untested)
PHP:
<!-- site content above --> 
<?PHP include("/home/user/public_html/section".rand(1,5).".php"); ?> 
<!-- footer below -->
 
0
Close
The trouble is trying to get the files 1-5 to be returned in a random order -

ie,

page load: file 5, file 3, file 1, file 4, file 2
refresh: file 3, file 2, file 5, file 1, file 4
refresh again: file 2, file 4, file 1, file 3, file 5
 
0
Close
Something like that maybe...

PHP:
<?php
srand((float) microtime() * 10000000);
$input = array("section1.php", "section2.php", "section3.php", "section4.php", "section5.php");
//$input = array_rand($input, sizeof($input));
foreach (array_rand($input, sizeof($input)) as $i) {
            include("/home/user/public_html/".$input[$i]); 
}
?>
 
0
Close
That worked dgaussin - many thanks for that. :)
 
0
Close
AIVikingsAIVikings

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

New: watch any domain
You can now watch domains on NamePros. We check each one and notify you when something changes. Search for a domain and press Watch on...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back