Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter Set variable

I have a controller named categories in which I have

function description($id=null) {
    $this->load->helper(array('form', 'url'));
    $this->load->library('session');
    $this->load->view('category/description');
    $this->load->model('userdata_model');
    $data = $this->userdata_model->desc($id);
    $this->load->view('category/description', $data);
}

Now when I do print_r($data); it gives me the following result

Array
(
    [0] => stdClass Object
        (
            [id] => 4481
            [title] => Dvd Recorder Post
            [image_name] => 
            [image_name_work] => 
            [video_name] => 
            [work_type] => tape-recrder
            [description] => Test first..
            [agency] => 
            [services] => 
            [photographer] => 
            [company] => 
            [director] => 
            [dop] => 
            [userid] => 
            [display_order] => 99999
            [latest_work] => No
            [status] => Active
            [post_adddate] => 2012-03-26 10:14:07
            [date] => 2012-03-26 10:14:07
            [sub_cat] => dvd-reorder
            [views] => 75
            [url] => Dvd-recorder-post
            [post_cache] => 
            [post_cat_id] => 0
            [post_link] => 
            [post_pubdate] => 
            [post_seo_url] => 
            [post_status] => 0
            [post_type] => 0
        )

)

Now when I try to access $data in description.php in the view page it gives me following error

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: data

Filename: category/description.php

Line Number: 2

Anybody has an idea why?

like image 868
Rajat Goyal Avatar asked Nov 17 '25 17:11

Rajat Goyal


1 Answers

CodeIgniter uses extract to generate the variables in the view page. So, you'll have to pass in an associative array as the second argument of the $this->load->view() call. In this case, if you want the variable to be named data, you'll have to pass in an array with the element key being data and the value being the data you retrieved from the model.

$data = array('data' => $this->userdata_model->desc($id));
$this->load->view('category/description', $data);
like image 182
Kemal Fadillah Avatar answered Nov 19 '25 09:11

Kemal Fadillah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!