Randomize HTML Snippets

Posted: May 16th, 2009 | Author: Troy | Filed under: PHP | Tags: |

This one is very useful for randomizing html snippets such as image tags or just plain quotes.


/** Randomizes the order of the html in the $html array below.
* Don't forget the comma (,) at the end of each line (minus the last line) when adding more items!!
**/

$numItems = 2; // Change this value to add or decrease the number of elements in the $html array to be displayed.

$html = array (

'<img src="/images/imageOne.jpg" alt="" />' ,
'<img src="/images/imageTwo.jpg" alt="" />' ,
'<img src="/images/imageThree.jpg" alt="" />'

);

srand((float)microtime() * 1000000);
shuffle($html);
$i=0;
foreach ($html as $output)
{
if($i&lt;$numItems)
echo $output;
$i++;
}


Leave a Reply

You must be logged in to post a comment.