Reputation: 21
I am after a little help here. I need to randomize an image array's order.
For example, if I have 3 images, I need them to reload each time in a different order.
Also, this is the instructions given to me on how to implement the code:
If this is PHP code, use a return statement to send your output to the block. For example:
$my_output = 'Hello, world.';
return $my_output;
My current code is HTML, and goes like this:
<div class="ad">
<a href="http://www.linkedsite.com/" target="_blank"><img src="myimagesdirectpath/img.png" alt="linkedsitename" /></a>
<br />
<a href="http://www.linkedsite.com/" target="_blank"><img src="myimagesdirectpath/img.png" alt="linkedsitename" /></a>
<br />
<a href="http://www.linkedsite.com/" target="_blank"><img src="myimagesdirectpath/img.png" alt="linkedsitename" /></a>
</div>
What I need to do is somehow turn my HTML code into a PHP code that allows the images order to be randomized, while maintaining the original instructions to "Use an output statement" to send my code to the sidebar.
Thanks! and I wish I knew more
Upvotes: 2
Views: 3685
Reputation: 4103
If you really need a solution before really understanding the PHP basics, here it is:
<?php
$images = array(
'<a href="http://www.linkedsite.com/" target="_blank"><img src="myimagesdirectpath/img.png" alt="linkedsitename" /></a>',
'<a href="http://www.linkedsite.com/" target="_blank"><img src="myimagesdirectpath/img.png" alt="linkedsitename" /></a>',
'<a href="http://www.linkedsite.com/" target="_blank"><img src="myimagesdirectpath/img.png" alt="linkedsitename" /></a>'
);
shuffle($images); // Randomize images array
return '<div class="ad">'.implode('<br />', $images).'</div>';
?>
But to be honest the suggestion is always to read some PHP tutorial like this.
Upvotes: 4
Reputation: 2083
After finding a nice introductory course to PHP (Google "php tutorial -w3schools" for a good tutorial on PHP).
Take a look at http://www.php.net/manual/en/function.shuffle.php
Google can tell you how to put your values in an array and neatly output that array using a foreach loop.
Upvotes: 0