简体   繁体   中英

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?

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

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

$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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM