3

我有一个名为我的categories控制器

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

现在,当我这样做时,print_r($data);它会给我以下结果

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
        )

)

现在,当我尝试在视图页面中访问$datadescription.php,它给了我以下错误

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: data

Filename: category/description.php

Line Number: 2

有人知道为什么吗?

4

4 回答 4

2

CodeIgniter 用于extract在视图页面中生成变量。因此,您必须传入一个关联数组作为$this->load->view()调用的第二个参数。在这种情况下,如果您希望将变量命名为data,则必须传入一个数组,其中元素键为data,值是您从模型中检索到的数据。

$data = array('data' => $this->userdata_model->desc($id));
$this->load->view('category/description', $data);
于 2012-09-23T10:06:18.257 回答
0

你可以这样做

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

在您看来,您可以使用访问变量

$ids
于 2012-09-23T10:11:12.497 回答
0

$data是一个数组,包含您希望在查看页面上显示的所有信息。

在控制器中:

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

鉴于:

<div><?php echo $description; ?></div>
于 2012-09-23T10:14:02.170 回答
0

因为你在调用模型之前调用你的视图,所以删除那一行,几乎是好的

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

在你看来 foreach 像这样

<?php foreach($Posts as $item): ?>
  <?= item['id']   ;?> Or if you have STD Class <?= item->id   ;?>
<?php endforeach; ?>
于 2016-04-21T06:39:33.707 回答