Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Help randomizing image array

    Hello guys, 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:
    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:
    Code:
    <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

  • #2
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts
    nevermind, got it.

    Code:
    <?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
    ?>
    <div class="ad">
        <?php echo implode('<br />', $images); ?>
    </div>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •