Click here to Skip to main content
15,898,628 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello!
I have some banners at DB. Every banners has params:

PHP
position //1 - top banners, 2 - right banners, 3 - left banners
id
showes //2000 Common number of showing banners
showed //A number of showing banner for current user, default = 3


I try to make system, that will shows banners for current user not more that 2 times.

For example, i entered to site, and i see at top position 1 banner, at right side 1 banner and at left 1 banner.

When i make reload page i will do:

1) Will check if common number of banner from position 1 is larger that 0 and parametr showed > 0, then to reduce "showed" parametr for banner on 1 minus (will be 2).

And this operation must do for each banners. If "showed" == 0 need show any banner from position and check it same (item 1)

Now i have a bad code, may by you could help me make it thoroughly

PHP
<?
function GetBanners($options = array('showes > '=>'0')){

      $showes = 3;
      $settings = array(

          1 => array('height' => 90, 'width' => 1149),
          2 => array('height' => 50, 'width' => 408),
          3 => array('height' => 234,'width'=> 304),
          4 => array('height' => 50, 'width' => 408),
          5 => array('height' => 50, 'width' => 560)
        );

        $banners_new = array();
        $new_banners = array();
        $ci=&get_instance();
        $banners = $ci->banners_model->GetBanners($options);


        if (!empty($banners)) {

        foreach ($banners as $key => $value) {

            $banners_new[$value->position][$value->id]['status'] = $value->status;
            $banners_new[$value->position][$value->id]['showes'] = $value->showes;
            $showes_array[$value->id] = $showes;

            if ($value->type == 0) {

                $banners_new[$value->position][$value->id]['code'] = (empty($value->code))? '<a title="'.$value->name.'" target="_blank" href="'.$value->link.'"><img width="'.$settings[$value->position]["width"].'" height="'.$settings[$value->position]["height"].'" src="'.base_url().$value->image.'"/></a>' : $value->code;
                $banners_new[$value->position][$value->id]['type'] = 0;

                } else {

                    $banners_new[$value->position][$value->id]['type'] = 1;
                    $banners_new[$value->position][$value->id]['code'] = '

                    <object height="'.$settings[$value->position]["width"].'" width="'.$settings[$value->position]["height"].'">
                        <param name="movie" value ="'.$value->image.'"></param>
                        <param name="quality" value="high"></param>
                        <param name="wmode" value="window"></param>
                        <param name="menu" value="false"></param>
                        <param name="bgcolor" value="#ffffff"></param>
                        <param name="flashwars" value=""></param>
                        <embed type="application/x-shockwave-flash" height="'.$settings[$value->position]["height"].'" width="'.$settings[$value->position]["width"].'" src="'.$value->image.'" quality="high" bgcolor="#ffffff" wmode="window" menu="false" flashwars="">
                        </embde>
                    </object>';

            }
        }

      }

      if (!isset($_SESSION['showed_banners']) && empty($_SESSION['showed_banners'])){ $_SESSION['showed_banners'] = $showes_array;}
         $temp = $_SESSION['showed_banners'];


        foreach ($banners_new as $key => $value) {
               //$key - position

          foreach ($value as $keys => $val){

            if (array_key_exists($keys, $temp) && $_SESSION['showed_banners'][$keys] >0) {

                $_SESSION['showed_banners'][$keys] = $temp[$keys]-1;

                switch ($key) {

                    case '1':
                     $new_banners[$key][] = $val['code'];
                    break;

                    case '2':
                    # code...
                    break;

                    case '3':
                     $new_banners[$key] = $val['code'];
                    break;

                    case '4':
                    # code...
                    break;

                  default:
                    # code...
                    break;
                }


          } else {

            $ci->banners_model->Update(array('showes' => $val['showes'] - $showes), $keys);

          }

        }

        }


        if (empty($new_banners)) {
            unset($_SESSION['showed_banners']);
            GetBanners();
        }




        return $new_banners;

}
?>
Posted
Comments
Sergey Alexandrovich Kryukov 25-May-13 20:14pm    
Can anyone explain to me how banners could be useful for a site users? Aren't they are just to meet the requirements of advertisers? It looks like they merely irritates the readers; but it so, can it have any advertising effect then?..
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900