2

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

3
  • 1
    Is this homework? If so, please add homework to the tag list. Commented Feb 19, 2012 at 20:26
  • 2
    It sounds like you should start with the PHP tutorial, and work from there. Commented Feb 19, 2012 at 20:26
  • 1
    put "array random" in the php manual search box and that would be a good start. Commented Feb 19, 2012 at 20:31

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

9 Comments

Well, thanks. But as I said I suggest reading a tutorial before starting any PHP. Having a solution is always a good point to start understanding something.
+1 because it do not support -1 if someone comes with the right solution. It doesn't matter if this is a homework or not!
Thank you Satoshi! How do I use a return statement with this code? I will also check out some PHP tutorials.
So this is what I tried to allow the return code. I believe I do not need to wrap the code in <?php ?> since the sidebar does it for you when you select the php option for it.
$output = echo implode('<br />', $images); return $output;
|
0

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.

4 Comments

Sorry : ( It's where I've taught myself HTML/PHP/JS and the likes back in the days and I've turned out fine. (I don't mind reading 20 page long design theory/paradigm articles though) What would be a good alternative to point new people to?
Sorry, but if you follow that sites example your code will be a lot less than "fine"
That sites examples have long since been replaced by my own. Nevertheless your link has proven very helpful in removing w3schools from my search results so I can more easily find php.net's entries. (I don't see why W3schools is above that, annoying as hell).
Thanks guys. No, this is not homework. Its for my site, I just don't know much about PHP, well any really. I will check out some links and learn some more.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.