Ad module - show random image but never no image

3 posts by 2 authors in: Forums > CMS Builder
Last Post: August 6, 2015   (RSS)

Hello, I'm having trouble with an ad module I've created. I have a table of ads in which I have start and end dates and a checkbox selector for the different ad zones. I've set a limit of one image ad to appear in each of the zones, and that works fine, but sometimes NO image appears, which I don't want; I always need at least one ad to show in the zone. What can I add to my code to always have an image appear?

Thanks!

Here's my ad function:

========== start code ============

function ad_content ($zone) {

    list($banner_adsRecords, $banner_adsMetaData) = getRecords(array(
        'tableName' => 'banner_ads',
        'limit' => '1',
        'orderBy' => 'RAND()',
         'loadUploads' => true,
        'allowSearch' => false,
    ));

    list($ad_zonesRecords, $ad_zonesMetaData) = getRecords(array(
        'tableName' => 'ad_zones',
    ));

    foreach ($ad_zonesRecords as $zoneRecord) {
        if($zoneRecord['num']==$zone) {

            foreach ($banner_adsRecords as $record) {
                $start_date = strtotime($record['start_date']);
                $end_date = strtotime($record['end_date']);

                if (time() < $end_date && time() > $start_date) {
                    if(in_array($zone, $record['zones:values'])) {
                        foreach ($record['banner'] as $index => $upload) {
                            echo "<a href='$record[destination_url]'><img src='$upload[urlPath]' width='$upload[width]' height='$upload[height]' target='_blank' /></a>";
                        }
                    }
                }
            }
        }
    }
}

========== end code ============

By ross - August 6, 2015

Hi cjpritchard

Thanks for posting!

How about setting the upload field as required "user may not leave field blank" in your section editor.  That way, every record will always have an image moving forward. 

You would just need to go through previous records and upload an image if the didn't have one.

Let me know if that will work for you.

Thanks!

-----------------------------------------------------------
Cheers,
Ross Fairbairn - Consulting
consulting@interactivetools.com

Hire me! Save time by getting our experts to help with your project.
Template changes, advanced features, full integration, whatever you
need. Whether you need one hour or fifty, get it done fast with
Priority Consulting: http://www.interactivetools.com/consulting/