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: [drewh01] How to randomize a upload display

By Codee - January 5, 2010

Chris's method would be very stable, simple and fast (less server delay).

If you wanted the banners to rotate but on a timed increment instead of page refresh you could also incorporate some of the free code from dynamicdrive.com . I've done this for several clients to show thumbnails of product listings rotating every 2 seconds across the top of the page. I've also set up an "ad center" section inside CMSB for a client that allows them to change banner ads or banner code on the fly and appear automatically in certain pages and positions. CMSB makes this fairly easy and simple to do.

Re: [equinox69] How to randomize a upload display

By drewh01 - January 5, 2010

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