Welcome Guest, Not a member yet? Register   Sign In
Variable in view not the same in all pages.
#1

[eluser]sb05[/eluser]
Hi Guys,

This is almost the last thing I need and then I'm done for my web application for school. I'm still new at PHP and Codeigniter so bear with me.

I have a view called left_column_view.php where I display ads. The ad basically consists of a header with the ad name, the ad image and a link once you click the image. So basically 3 variables $ads->name, $ads->image_url and $ads_link. See below.
Code:
<h1>&lt;?php echo $ads->name; ?&gt;</h1>
<p>&lt;?php echo $ads->image_url; ?&gt;</p>
<p>&lt;?php echo $ads->link; ?&gt;</p>

This works, but the problem is it only works on one page, where I call the function view($ad_id) of my ads.php Controller

But at my index function, my add, update, delete and tons of other functions, instead of the ad, I get undefined variabe: ads

Code:
&lt;?php
class Ads extends Controller {  
    // num of records per page
    private $limit = 10;
    
    
    function Ads(){
        parent::Controller();
        
        // load library
        $this->load->library(array('table','validation'));
        
        // load helper
        $this->load->helper('url');
        
        // load model
        $this->load->model('adModel','',TRUE);

    }
    
    function index($offset = 0){
        // offset
        $uri_segment = 3;
        $offset = $this->uri->segment($uri_segment);
        
        // load data
        $ads = $this->adModel->get_paged_list($this->limit, $offset)->result();
        
        // generate pagination
        $this->load->library('pagination');
        $config['base_url'] = site_url('ads');
         $config['total_rows'] = $this->adModel->count_all();
         $config['per_page'] = $this->limit;
        $config['uri_segment'] = $uri_segment;
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        
        // generate table data
        $this->load->library('table');
        $this->table->set_empty("&nbsp;");
        $this->table->set_heading('ID','Name', 'Image_url','Link', 'Client Name');
        $i = 0 + $offset;
        foreach ($ads as $ad){
            $this->table->add_row(++$i, $ad->name, $ad->image_url, $ad->link, $ad->client_name,
                anchor('ads/view/'.$ad->ad_id,'view',array('class'=>'view')).' '.
                anchor('ads/update/'.$ad->ad_id,'update',array('class'=>'update')).' '.
                anchor('ads/delete/'.$ad->ad_id,'delete',array('class'=>'delete','onclick'=>"return confirm('Are you sure want to delete this News ad?')"))
            );
        }
        $data['table'] = $this->table->generate();
                $this->load->vars($data);

        
        // load view
            $this->load->view('banner_view', $data);
            $this->load->view('left_column_view', $data);
            $this->load->view('right_column_view', $data);
            $this->load->view('adList', $data);
            $this->load->view('footer_view', $data);
    }

function view($ad_id){
        // set common properties
        $data['title'] = 'ad Details';
        $data['link_back'] = anchor('ads','Back to list of News ads',array('class'=>'back'));
        
        // get ad details
        $data['ads'] = $this->adModel->get_by_id($ad_id)->row();
        
        // load view
            $this->load->view('banner_view', $data);
            $this->load->view('left_column_view', $data);
            $this->load->view('right_column_view', $data);
            $this->load->view('adView', $data);
            $this->load->view('footer_view', $data);    }
I think that this line gets the value from the variable I need

$data['ads'] = $this->adModel->get_by_id($ad_id)->row();

However when I paste this line in my other functions I still get the same error that ads is an undefined variable.
Also I prefer not to do this way cuz it's highly inefficient cuz I have so many controllers with so many functions.

So how can the variables in my left_column_view be displayed in all my other pages? (By the way all controllers load the left_column_view.php)
Just in case here is my adModel.php
Code:
&lt;?php
class AdModel extends Model {
    
    private $ads= 'ads';
     var $upload_data ;

function ad(){
        parent::Model();
    }

function get_by_id($ad_id){
        $this->db->where('ad_id', $ad_id);
        return $this->db->get($this->ads);
    }


Messages In This Thread
Variable in view not the same in all pages. - by El Forum - 01-13-2011, 07:35 PM
Variable in view not the same in all pages. - by El Forum - 01-13-2011, 09:14 PM
Variable in view not the same in all pages. - by El Forum - 01-14-2011, 04:24 AM
Variable in view not the same in all pages. - by El Forum - 01-14-2011, 04:40 AM
Variable in view not the same in all pages. - by El Forum - 01-14-2011, 04:49 AM
Variable in view not the same in all pages. - by El Forum - 01-14-2011, 04:52 AM
Variable in view not the same in all pages. - by El Forum - 01-14-2011, 04:52 AM
Variable in view not the same in all pages. - by El Forum - 01-14-2011, 05:44 AM
Variable in view not the same in all pages. - by El Forum - 01-14-2011, 06:01 AM
Variable in view not the same in all pages. - by El Forum - 01-15-2011, 06:47 PM
Variable in view not the same in all pages. - by El Forum - 01-15-2011, 08:46 PM
Variable in view not the same in all pages. - by El Forum - 01-16-2011, 05:35 PM
Variable in view not the same in all pages. - by El Forum - 01-16-2011, 05:48 PM
Variable in view not the same in all pages. - by El Forum - 01-17-2011, 06:54 AM
Variable in view not the same in all pages. - by El Forum - 01-17-2011, 09:10 AM
Variable in view not the same in all pages. - by El Forum - 01-17-2011, 11:01 AM
Variable in view not the same in all pages. - by El Forum - 01-17-2011, 11:08 AM
Variable in view not the same in all pages. - by El Forum - 01-17-2011, 11:31 AM



Theme © iAndrew 2016 - Forum software by © MyBB