How to randomize a upload display

5 posts by 3 authors in: Forums > CMS Builder
Last Post: January 5, 2010   (RSS)

By drewh01 - January 4, 2010

I have a site that will utilize website banner ads and they want 1 to appear randomly each time the page is reloaded.

How can this be done?

http://vps.fp2marketing.com/~chamberm

Here is my current code loading the uploads:

<?php foreach ($home_pageRecord['ad_space_1'] as $upload): ?>
<?php if ($upload['hasThumbnail']): ?>
<img src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" alt="" /><br/>

<?php elseif ($upload['isImage']): ?>
<img src="<?php echo $upload['urlPath'] ?>" width="<?php echo $upload['width'] ?>" height="<?php echo $upload['height'] ?>" alt="" /><br/>

<?php endif ?>
<?php endforeach ?>

Re: [drewh01] How to randomize a upload display

By Chris - January 5, 2010

Hi drewh01,

I think the simplest way to do this would be to upload all your banner ads into the same upload field in your "home_page" single record section, and use an info field for the URL to be linked to. Then, you'd simply need to shuffle the array and break after displaying the first ad:

<?php shuffle($home_pageRecord['ad_space_1']) ?>
<?php foreach ($home_pageRecord['ad_space_1'] as $upload): ?>
<?php if ($upload['isImage']): ?>
<a href="$upload['info1']"><img src="<?php echo $upload['urlPath'] ?>" width="<?php echo $upload['width'] ?>" height="<?php echo $upload['height'] ?>" alt="" /></a>
<?php break ?>
<?php endif ?>
<?php endforeach ?>


I hope this helps. Please let me know if you have any questions.
All the best,
Chris

Re: [chris] How to randomize a upload display

By drewh01 - January 5, 2010

you are my hero!

Re: [equinox69] How to randomize a upload display

By drewh01 - January 5, 2010

good to know, I appreciate it. May come in handy .