Rajat Goyal
Rajat Goyal

Reputation: 59

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?

Upvotes: 3

Views: 112

Answers (4)

Dev
Dev

Reputation: 74

Because you call your View before call model, remove that row, and almost be good

$this->load->helper(array('form', 'url'));
$this->load->library('session');
$this->load->model('your model');
$data['Posts'] = $this->YourModel->ModelMethod();
$this->load->view('Your view', $data);

In your view foreach like this

<?php foreach($Posts as $item): ?>
  <?= item['id']   ;?> Or if you have STD Class <?= item->id   ;?>
<?php endforeach; ?>

Upvotes: 0

doktorgradus
doktorgradus

Reputation: 627

$data is an array, contains all information, which you want show on view page.

In controller:

$this->load->model( 'userdata_model' );
// .. other code
$data = array(
        'description' => $this->userdata_model->desc($id)
    );
$this->load->view('category/description', $data );

In view:

<div><?php echo $description; ?></div>

Upvotes: 0

WatsMyName
WatsMyName

Reputation: 4478

You can do like this

 $data["ids"] = $this->userdata_model->desc($id);
 $this->load->view('category/description', $data);

in your view you can access the variable using

$ids

Upvotes: 0

Fad
Fad

Reputation: 9858

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

Upvotes: 2

Related Questions