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 ============

Hi Ross,

Thanks for your message. That worked!

Cheers!