Reputation: 20444
I am trying to randomise the order of a series of elements for a websites homepage.
Imagine we have 3 titles to display on this page.
$title1 = 'the first title';
$title2 = 'the second title';
$title3 = 'the third title';
Now on the HTML file we want to display these but in a random order and never repeating one element.
<html>
<div id='titleholder1'> [ randomly show either $title1, $title2 or $title3 ] </div>
<div id='titleholder2'> [ randomly show either $title1, $title2 or $title3 ] </div>
<div id='titleholder3'> [ randomly show either $title1, $title2 or $title3 ] </div>
</html>
Also it must avoid ever repeating $title1 $title2 or $title 3
on the page.
Any ideas,
Marvellous
EDIT.
What would we need to do if we had more than one element.
$title1 = 'the first title'; $info1 = 'the first info';
$title2 = 'the second title'; $info2 = 'the second info';
$title3 = 'the third title'; $info3 = 'the third info';
Same principle but obviously $info1
and $title1
need to stay together and be shuffled together.
Any ideas
Upvotes: 1
Views: 1211
Reputation: 16880
Put the text into an array
and shuffle
it.
<?php
$titles[] = 'one';
$titles[] = 'two';
$titles[] = 'three';
shuffle($titles);
?>
<div class="titleholder1"><?php echo $titles[0];?></div>
<div class="titleholder2"><?php echo $titles[1];?></div>
<div class="titleholder3"><?php echo $titles[2];?></div>
Update
$webpage[] = array('info' => 'first info', 'title' => 'first title');
$webpage[] = array('info' => 'second info', 'title' => 'second title');
$webpage[] = array('info' => 'thrid info', 'title' => 'third title');
Upvotes: 2