Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
PHP
  1  <?php
  2  
  3  if (!defined('BASEPATH')) exit('Нет доступа к скрипту');
  4  session_start();
  5  class Banners
  6  
  7    {
  8      private $core;
  9      public $on = TRUE;
 10      private $shows = 2;
 11      public $banners = array();
 12      public $array_shows = array();
 13      public $positions = array();
 14  
 15      
 16      
 17      
 18      function __construct()
 19        {
 20  
 21          $this->core = $CI = & get_instance();
 22          //unset($_SESSION['BANNERS']); die();
 23          $this->Check(); 
 24          $this->GetAllBanners();
 25  
 26         
 27        }
 28  
 29    protected function Check()
 30        {
 31          if (empty($_SESSION['BANNERS']) && $this->on == TRUE)
 32            {
 33  
 34              $_SESSION['BANNERS'] = $this->Get();
 35        
 36            }
 37        }
 38  
 39    public function GetAllBanners(){
 40  
 41      foreach ($_SESSION['POSITIONS'] as $position) {
 42  
 43          $this->GetCurrentBanner($position);
 44      }
 45    }
 46  
 47  
 48    public function Get()
 49        {
 50          $query = $this->core->db->query('SELECT * FROM banners WHERE status = 1 AND showes>0 AND timeout >= CURDATE() ORDER BY position DESC')->result();
 51          return $this->SortList($query);
 52        }
 53  
 54    private function SortList($array)
 55        {
 56          foreach($array as $value)
 57            {
 58              
 59              $this->banners[$value->position]['id'][] = $value->id;
 60              $this->banners[$value->position]['image'][] = $value->image;
 61              $this->banners[$value->position]['code'][] = $value->code;
 62              $this->banners[$value->position]['link'][] = $value->link;
 63              $this->banners[$value->position]['showes'][] = $value->showes;
 64              $this->array_shows[$value->id] = $this->shows;
 65              $this->positions[$value->position] = $value->position;
 66              
 67              
 68            }
 69            $_SESSION['POSITIONS'] = $this->positions;
 70            $_SESSION['SHOWS'] = $this->array_shows;
 71  
 72          return $this->banners;
 73        }
 74    
 75    private function GetCurrentBanner($id_position, $i = 0){ 
 76         echo $_SESSION['SHOWS'][$first_banner]; //HERE GET 1 ALWAYS
 77         $first_banner = $_SESSION['BANNERS'][$id_position]['id'][$i];
 78  
 79          if ($_SESSION['SHOWS'][$first_banner] !== 0){
 80  
 81                $int = $_SESSION['SHOWS'][$first_banner] - 1;
 82                $_SESSION['SHOWS'][$first_banner] = $int;
 83                //HERE GET 0 
 84                $_SESSION['BANNERS'][$id_position]['showes'][$i] = ($_SESSION['BANNERS'][$id_position]['showes'][$i]) - 1;
 85              
 86  
 87                $this->banners[$id_position] = array(
 88  
 89                'id' => $_SESSION['BANNERS'][$id_position]['id'][$i],
 90                'image' => $_SESSION['BANNERS'][$id_position]['image'][$i],
 91                'code' => $_SESSION['BANNERS'][$id_position]['code'][$i],
 92                'link' => $_SESSION['BANNERS'][$id_position]['link'][$i]);
 93              
 94           
 95          } else {
 96  
 97            if($_SESSION['BANNERS'][$id_position]['showes'][$i] !== 0) {
 98  
 99              array_push($_SESSION['SHOWS'], $_SESSION['SHOWS'][$first_banner] = $this->shows);
100  
101            }
102              array_shift($_SESSION['SHOWS']);
103              $this->GetCurrentBanner($id_position, $i++);
104  
105          }
106  
107        }
108  
109    }
110  ?>


After reload page is called function GetCurrentBanner and check Session for !==0. But after last reload page my $_SESSION['SHOWS'] was equal 0 (//comment), why after next reload Session again is 1?
Posted
Updated 27-Apr-13 13:12pm
v5
Comments
[no name] 27-Apr-13 19:15pm    
Task is to show for user banners no more than three times in a row and!
All banners are stored in the session after SQL query. At web page there is banners places (top, right, left) are three position.
So, from SESSION i wanna get one banners for every position and show this baners no more that three times for current user. If was showes 3 ones, then will show next banner for every position and etc until common number of showing not equal zero.

1 solution

Change Line 77 to 76. Interchange those lines.

The variable $first_banner should come before when you were trying to use it on line 76.
 
Share this answer
 
Comments
[no name] 28-Apr-13 3:46am    
So, it is good note, but question is not at line 77
Akinmade Bond 28-Apr-13 4:42am    
What do you mean by, "the question is not on line 77?" You said you were always getting the same value on a certain line and I suggested you interchange line 76 and 77.
[no name] 28-Apr-13 6:50am    
I must give details! I want create bannests sistem, what shows banners no more that one times for current user entered at web page and changes at another banners if 3 ones.
Akinmade Bond 28-Apr-13 12:04pm    
:doh:

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