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);
    }
#2

[eluser]CI_avatar[/eluser]
hello sb05. by default, when Ads controller is called it will execute the index function, right? so before you call
Code:
this->load->view('left_column_view', $data);
you must initialize
Code:
$data['ads']
.
as what I've observed in index function, you did not initialize
Code:
$data['ads']
variable
Code:
//include this code the check if your $data['ads'] is initialized
print_r($data['ads']);

// 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);
#3

[eluser]sb05[/eluser]
Thanks for the quick reply CI.

I tried it. But the error of 'undefined variable: ads' stays and I get an extra error of 'undefined index: ads'.

Sad

Any alternatives?
#4

[eluser]Atharva[/eluser]
I don't see in your index function
Code:
// get ad details
        $data['ads'] = $this->adModel->get_by_id($ad_id)->row();
anywhere
#5

[eluser]sb05[/eluser]
I also tried this post from 'cahva' in this topic:
Passing controller variable to more then one view

This person had something similar with my issue. In the end this code worked for him:
Code:
class Filesystems extends Controller {

    var $data;

    function __construct();
    {
        parent::__construct();
        
        $this->load->model('Links_model');
        $results = $this->Links_model->getlinks();
        $this->data['links'] = $results['rows'];
        $this->data['num_results'] = $results['num_rows'];
    }
    
    function index()
    {
        #
        # Some code here
        #
        
    
    
        # ..and in the end of method
    
        // Either this
        $this->load->view('includes/template', $this->data);
        
        // Or like this
        $this->load->vars($this->data);
        $this->load->view('includes/template');
    }
}

//Now in the template you would have $links and $num_results. If you load views inside includes/template view, use the load->vars //option and those variables will work also in the nested views.

So I tried to do this with my controller:
Code:
class Ads extends Controller {

    
    var $data;
    // num of records per page
    private $limit = 10;
    
    function __construct()
    {
        parent::__construct();
        
        $this->load->model('adModel');
        $results = $this->adModel->get_by_id($ad_id);
        $this->data['ads'] = $results['rows'];
        $this->data['num_results'] = $results['num_rows'];
    }

function index()
{
//some code

//at the end of the method

$this->load->vars($this->data);

        
            $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);
    }

}

But then I get an error 'undefined variable: ad_id' at
Code:
$results = $this->adModel->get_by_id($ad_id);

And I get also this error:
ErrorException [ Fatal Error ]: Cannot use object of type CI_DB_mysql_result as array
on line:
Code:
$this->data['ads'] = $results['rows'];

I'm so stuck :S
#6

[eluser]Atharva[/eluser]
Code:
$results = $this->adModel->get_by_id($ad_id);

This will obviously show error as $ad_id is not defined in constructor.

As i mentioned earlier, you need to call
Code:
$results = $this->adModel->get_by_id($ad_id);
in index function.
#7

[eluser]sb05[/eluser]
Hi atharva,

Just saw ur post. Gonna try it right now...
#8

[eluser]sb05[/eluser]
@ atharva


In your first post you said add
Code:
$data['ads'] = $this->adModel->get_by_id($ad_id)->row();

in my index function. So here is my index function. I added your line it's the 4th line under function index(){ as you see below.

Code:
&lt;?php
class Ads extends Controller {

    
    var $data;
    // 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();
        
        $data['ads'] = $this->adModel->get_by_id($ad_id)->row();
    //    $results = $this->adModel->get_by_id($ad_id);
        
        // 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);
                
    

           print_r($data['ads']);
          
          

        // load view
            $this->load->vars($this->data);

        
            $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);
    }

So now instead of 'undefined index: ads', I get again 'undefined variable: ads'

In your second post I should add this line in my index function:
Code:
$results = $this->adModel->get_by_id($ad_id);

Maybe it's basically the same as in your first post, but written differently, because I get the same error.

And if I do both, it's the same, only the error is displayed twice.

What am I doing wrong?

Did I put the line in the wrong place in my index function? I know PHP reads from top to bottom, but I think I put it in the right position.
#9

[eluser]Atharva[/eluser]
Sorry pal, ignore my first and second post. First of all tell me, in left_column_view.php , you want to display all ads or only one ad? The problem is that, you are loading the view for each function, but passing the $data['ads'] only in view function. You have to somehow pass $data['ads'] from each function where you are loading the left_column_view.php view. Got the point?
#10

[eluser]sb05[/eluser]
Hey Atharva,

Yes I know exactly what you mean, that's what I was trying to point out in the first post. Anyway I'm half-way there.

This line in my 'view function' of my 'ads controller' deals with displaying my ad correctly:

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

See where it's located in the function:

Code:
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);    }

So I figured I just paste that line to all my functions, because the ad has to be visible in all the pages. So in some functions it works and in others not.

Here is one function where it does work. The 'update function':

Code:
function update($ad_id){
        // set validation properties
        $this->_set_fields();
    //    echo 'here is update';
        // prefill form values
        $ad = $this->adModel->get_by_id($ad_id)->row();
        $this->validation->ad_id = $ad_id;
        $this->validation->name = $ad->name;
        $this->validation->image_url = $ad->image_url;
        $this->validation->link = $ad->link;
        $this->validation->client_name = $ad->client_name;
        
        
    
        
        // set common properties
        $data['title'] = 'Update News ad';
        $data['message'] = '';
        $data['action'] = site_url('ads/updateAd');
        $data['link_back'] = anchor('ads','Back to list of News ads',array('class'=>'back'));
    
            $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('adEdit', $data);
            $this->load->view('footer_view', $data);    }

As you can see I pasted that line right before loading the views.
But when I paste it in my index function right before loading the views like the function above, it doesn't work.
I get 2 errors: 1 - Undefined Variable: ad_id , 2 - Trying to get property of non object (error displayed where the ad should be displayed).
Here is my index function:
Code:
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();
        
    //    $data['ads'] = $this->adModel->get_by_id($ad_id)->row();
    //    $results = $this->adModel->get_by_id($ad_id);
        
        // 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);
                
    

         //  print_r($data['ads']);

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


        // load view
            $this->load->vars($this->data);

        
            $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);
    }

So I can't see what I'm missing. In the view function apart from
Code:
$data['ads'] = $this->adModel->get_by_id($ad_id)->row();
There are lines that define links, and the rest are loading view lines. Nothing more. But in that function there is no error that ad_id is not defined. But with the index function it is.
I really dont understand.




Theme © iAndrew 2016 - Forum software by © MyBB