PHP randomisation

Associate
Joined
19 Jun 2006
Posts
162
Location
Swansea, Wales
I have a simple website which just basically produces boxes of different content repeatedly in the page.

Eg.

<span id="aBox">Some content 1</span>
<span id="aBox">Some content 2</span>
<span id="aBox">Some content 3</span>

At the moment the page is pure html. I was wondering if anyone could think of a way, perhaps using php, to randomise the layout each time the page is loaded. So one time it would be as above then you hit F5 and it would be...


<span id="aBox">Some content 1</span>
<span id="aBox">Some content 3</span>
<span id="aBox">Some content 2</span>

etc.

Any ideas?
 
The content of the boxes, they are static as opposed to being read from a db?

You could stuff each entry into an array, mix it up using shuffle() and then output the resulting mix?

Take a look at this link for an example :)
 
As above really:

Stolen from http://www.webforumz.com/php-forum/3237-random-order-of-content.htm

Code:
<?php  
$files = array ('<span id="aBox">Some content 1</span>', '<span id="aBox">Some content 2</span>', '<span id="aBox">Some content 3</span>');  
shuffle($files);  
foreach($files as $file)  
{  echo $file  }  
?>
I don't have much experience with php, somebody will probably tell me I can't have double quotes in there or something :rolleyes: :)
 
Yeh, it's all static. It really is this simplest of things and I didn't want to get into sql with it all.

What you have above seems to work well.

Thanks very much!
 
If you have bits which are common you can same a few bytes and combine the common bits, like this:
Code:
<?php  
$files = array ('Some content 1', 'Some content 2', 'Some content 3');  
shuffle($files);  
foreach($files as $file)  
{ echo "<span id='aBox'>$file</span>" }
?>
 
Is there any way to put a for loop in an array construct?

$files = array ('Some content 1', 'Some content 2', 'Some content 3');

Some content x could eventually go in to the hundreds and i really don't feel like typing out each 'Some content xxx' for each entry.
 
Since you don't want a database, the best thing would be to grab the content from an external file. I did something similar at http://forums.overclockers.co.uk/showthread.php?t=17667955&highlight=array+question

The final code for that was:
Code:
<?php
$thelinks = file('thelinks.php');
foreach ($thelinks as $a)
{
	$links = explode(", ",$a);
	echo "<p><a href='$links[0]' target='_blank'>$links[1]</a></p>";
}
?>
And thelinks.php was in the form:
Code:
http://www.link.com, Link to somewhere
http://www.another.com, Another link
If you do this you can put all your content in there, grab it, and do some jiggery pokery with it to shuffle them.
 
Cheers. I've used your idea but taken it one step further by dynamically creating the 'thelinks.php' file...

Code:
$PATH = $_ENV['DOCUMENT_ROOT'];
	
	$d=$PATH.$dirPath; 
	
	$dir = opendir($d); 
		
	$myFile = "$fileName.txt";
	$fh = fopen($myFile, 'w') or die("can't open file");
	
	while ($f = readdir($dir)) {
		$stringData = $f."\n";
		fwrite($fh, $stringData);
	}
	
	fclose($fh);

The only problem is it puts in '.' and '..' at the start of the file which then means i get two broken links when constructing my links from the array.

Any ideas how to get around this?
 
Back
Top Bottom