Random banners with priority

We have 3 Ads

1-Every Ad can Enable/Disable

2-Every Ad has its priority

3-Every Ad may be Banners OR Html/Javascript code

Need to

1-If Ad is enabled run it

2-run rotate banners depend on it’s priority

3-if Ad type is Banner >> show banner

4-if Ad type is Html/Javascript code >> show Html/Javascript code

Thanks

So what have you done to try this? Where’s your code?
What’s your database structure?
What’s the logic behind “depend on it’s priority”?
What specifically do you need help with - Sitepoint is a help site, not a “do this for me” site.

1 Like

now We need to write

###############################

if (( dig('banner_kind_1') == 1) AND ((empty($dig['banner_image_1']['url']) OR (empty ($dig['banner_url_1']))))) $AD_weight_1 = 0;
if (( dig('banner_kind_2') == 1) AND ((empty($dig['banner_image_2']['url']) OR (empty ($dig['banner_url_2']))))) $AD_weight_2 = 0;
if (( dig('banner_kind_3') == 1) AND ((empty($dig['banner_image_3']['url']) OR (empty ($dig['banner_url_3']))))) $AD_weight_3 = 0;

if (( dig('banner_kind_1') == 2) AND ((empty($dig['banner_html_1'])))) $AD_weight_1 = 0;
if (( dig('banner_kind_2') == 2) AND ((empty($dig['banner_html_2'])))) $AD_weight_2 = 0;
if (( dig('banner_kind_3') == 2) AND ((empty($dig['banner_html_3'])))) $AD_weight_3 = 0;

###############################

how we can do it by loop, array or by function but in little code

[quote=“StarLion, post:2, topic:111360”]
What specifically do you need help with - Sitepoint is a help site, not a “do this for me” site.
[/quote]Except for that crazy Drummin guy… lol

I don’t know if you are familiar with the term “normalization” but any time you start mixing numbers with names, e.g. ‘banner_kind_1’, ‘banner_kind_2’ etc your code becomes harder and harder to use. You complicate it more by adding different names as in ‘banner_image_1’, ‘banner_html_1’ etc.

Most of the time, you are dealing with a result set of fields from a database, where you would have a fields such as id, name etc. Each row would have the same array KEYS such as ‘name’ and not name1, name2 and so coding in this way even if you are not using a database makes sense as it is easier to loop through and evaluate and identify records as the field names don’t change.

For example: A simple KEY or field like ‘type’ would let you know if this is an image or code thus there is no need to use ‘banner_image’, ‘banner_html’. In my example I simply used ‘content’ to hold what is to be displayed. Again thinking in terms of DB result set, the field ‘enabled’ with 0 or 1 would let you know if this record (banner) is to be shown or not. You said you only have three banners. What if you had 300 or more? Can you imagine writing unique fields for this many records? ‘banner_kind_1’, ‘banner_kind_2’ and on and on.

The code you provided also gives little information as to the intent or use of the variable $AD_weight and the function dig() is also unknown. For this reason, I formulated this sample based on Ad impressions and clicks with the result being that if an Ad is shown and NOT clicked, the “weight” or chances of it being shown goes down and if the Ad IS clicked, the chances of the Ad being shown goes up.

The sample for testing purposes uses session for holding and updating impressions and clicks but also has mysql samples in the comments. Hopefully I’ve added enough comments so you can follow what is going on. Again this is just a sample you might tailor to your needs.

<?php
session_start(); 
/*
This sample page has two versions, a Session version and a mysql version.


Sample banners array from DB or hard coded. 3 Ads, 100 Ads, it doesn't matter

NOTE: DB fields impressions and clicks should set at DEFAULT 1 NOT 0 as they will be used in division
         
$sql = "SELECT
          id
        , type
        , name
        , content
        , url
        , enabled
        , impressions    
        , clicks 
        FROM banners";
$query = $db->prepare($sql);
$query->execute();
// Build banners array
$banners = array();
while($row = $query->fetch(PDO::FETCH_ASSOC)){
    $banners[$row['id']] = $row;
}
*/

//TEST Sample         
$banners = array(
    1 => array(
          'id' => '1'
        , 'type' => 'image'
        , 'name' =>    'banner1'
        , 'content' => 'banner1.jpg'
        , 'url' => 'link1.com'
        , 'enabled' => '1'
        , 'impressions' => '22'    
        , 'clicks' => '3'
    ),
    2 => array(
          'id' => '2'
        , 'type' => 'code'
        , 'name' =>    'code1'
        , 'content' => '<div class="sample_code1">Bla</div>'
        , 'url' => 'link2.com'    
        , 'enabled' => '1'
        , 'impressions' => '13'    
        , 'clicks' => '4'    
    ),
    3 => array(
          'id' => '3'
        , 'type' => 'image'
        , 'name' =>    'banner3'
        , 'content' => 'banner3.jpg'
        , 'url' => 'link3.com'
        , 'enabled' => '0'    
        , 'impressions' => '11'    
        , 'clicks' => '3'    
    ),
    4 => array(
          'id' => '4'
        , 'type' => 'image'
        , 'name' =>    'banner4'
        , 'content' => 'banner4.jpg'
        , 'url' => 'link4.com'
        , 'enabled' => '1'
        , 'impressions' => '17'    
        , 'clicks' => '4'        
    ),
    5 => array(
          'id' => '5'
        , 'type' => 'code'
        , 'name' =>    'code5'
        , 'content' => '<div class="sample_code5">Bla5</div>'
        , 'url' => 'link5.com'
        , 'enabled' => '1'
        , 'impressions' => '16'    
        , 'clicks' => '6'        
    ),
    6 => array(
          'id' => '6'
        , 'type' => 'image'    
        , 'name' =>    'banner6'
        , 'content' => 'banner6.jpg'
        , 'url' => 'link6.com'    
        , 'enabled' => '1'
        , 'impressions' => '15'    
        , 'clicks' => '5'    
    ),
    7 => array(
          'id' => '7'
        , 'type' => 'code'
        , 'name' =>    'code7'
        , 'content' => '<div class="sample_code7">Bla7</div>'
        , 'url' => 'link7.com'
        , 'enabled' => '1'    
        , 'impressions' => '13'    
        , 'clicks' => '4'    
    )
);

//echo "<pre>";
//print_r($banners);        
//echo "</pre>";

//*** ONLY USED ON SESSION VERSION ***\\
    //unset($_SESSION['banners']);
    if(!isset($_SESSION['banners'])):
        foreach($banners as $k => $arr):
            $_SESSION['banners'][$k]['impressions'] = 1;
            $_SESSION['banners'][$k]['clicks'] = 1;    
        endforeach;
    endif; 
//*** END ONLY USED ON SESSION VERSION ***\\


// IF banner is clicked, update

//Make sure $_GET['id'] is a valid banner id
if(isset($_GET['id']) && array_key_exists($_GET['id'],$_SESSION['banners'])):

    //mysql version
    //$sql = "UPDATE banners SET clicks = clicks + 1 WHERE id = :id";
    
    //Session version
    $_SESSION['banners'][$_GET['id']]['clicks']++;
    
    
    //if you wish to redirect user to url, do it here
    //header("location: " . $banners[$_GET['id']]['url']);
    //exit;
    
    //else for testing reload page
    header("location: " . $_SERVER['PHP_SELF']);
    exit;
    
endif;
// END UPDATE

/* 
Build banner_chances array based on ad performance
The chances of an ad being shown goes up if it is clicked. Down if it is not.
*/
$banner_chances = array();
foreach($banners as $k => $arr):

    // Make sure ad is enabled
    if($arr['enabled'] == 1):
        
        /*
        Divide clicks by impressions to get perfomance percent value of that ad
        The better the ad, the more times it is listed and chances it gets shown
        Because the perfomance value is in fractions, e.g. 0.416666666666674
        multiply by 10 then floor() to get number of "chances" the ad will be listed, e.g. 4
        SO in this example the ad KEY (Ad ID) will be listed 4 times in the chances array.                
        */
        
        //mysql array version
        //$times = floor(($arr['clicks'] / $arr['impressions']) * 10);
        
        //Session version  
        $times = floor(($_SESSION['banners'][$k]['clicks'] / $_SESSION['banners'][$k]['impressions']) * 10);
        
        //Just in case IF $times < 1 make it 1 so ad at least has 1 chance of showing  
        $times = ($times < 1 ? 1 : $times);
        //echo $times."<br />";    
        
        //set the ad KEY (Ad ID) as the value X number of times
        for($t=0;$t<$times;$t++): 
            $banner_chances[] = $k;        
        endfor;
    
    endif;    
endforeach;
    
//echo "<pre>";
//print_r($banner_chances);    
//print_r($_SESSION['banners']);    
//echo "</pre>";

    // Because $banner_chances keys start at zero, subtract 1 from count()
    $option_cnt = count($banner_chances)-1;
    $r = rand(0, $option_cnt);
    
    //We want the VALUE from banner_chances with random key $r
    $banner_KEY = $banner_chances[$r];

    //*** Update banner impressions ***\\

    //mysql version
    //$sql = "UPDATE banners SET impressions = impressions + 1 WHERE id = :id";
    
    //Session version
    $_SESSION['banners'][$banner_KEY]['impressions']++;

// Build display.  IF "type" is image, put "content" in image tag, ELSE display content
$banner = '<a href="?id=' . $banner_KEY . '">'; 
if($banners[$banner_KEY]['type'] == 'image'){
    $banner .= '<img src="' . $banners[$banner_KEY]['content'] . '" alt="' . $banners[$banner_KEY]['name'] . '" />'; 
}else{
    $banner .= $banners[$banner_KEY]['content'];
}
$banner .= '</a>';
?>
<html>
<body>
<?php
echo $banner;
?>
</body>
</html>

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.