Php 编码点火器集变量
我有一个名为Php 编码点火器集变量,php,arrays,codeigniter,Php,Arrays,Codeigniter,我有一个名为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
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);
}
现在,当我打印($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
)
)
现在,当我试图在查看页面的description.php
中访问$data
时,它会给出以下错误
有人知道为什么吗?CodeIgniter使用在视图页面中生成变量。因此,您必须传入一个关联数组作为$this->load->view()
调用的第二个参数。在这种情况下,如果希望将变量命名为data
,则必须传入一个数组,其中元素键为data
,值为从模型中检索到的数据
$data = array('data' => $this->userdata_model->desc($id));
$this->load->view('category/description', $data);
你可以这样做
$data["ids"] = $this->userdata_model->desc($id);
$this->load->view('category/description', $data);
<?php foreach($Posts as $item): ?>
<?= item['id'] ;?> Or if you have STD Class <?= item->id ;?>
<?php endforeach; ?>
在您的视图中,您可以使用
$ids
$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>
因为您在调用模型之前调用了视图,所以删除该行,几乎就好了
$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);
在你看来,每个人都是这样的
$data["ids"] = $this->userdata_model->desc($id);
$this->load->view('category/description', $data);
<?php foreach($Posts as $item): ?>
<?= item['id'] ;?> Or if you have STD Class <?= item->id ;?>
<?php endforeach; ?>
或者如果你有性病课
它可以工作,但仍然显示错误。。视图页面中未定义的变量数据正常工作但仍显示错误?那没有任何意义。你能解释一下你的意思吗?错误是否完全相同?还是由其他未定义变量引起的错误?遇到PHP错误严重性:注意消息:未定义变量:描述文件名:category/description.PHP行号:1。它给出了这个错误。我还得到了数组的结果在你的代码中我看到了两个查看代码调用:$this->load->view('category/description')代码>和$this->load->view('category/description',$data)代码>,先删除。当它第一次调用时,视图中没有传递$data
。在第二次调用中,$data
数组正确传递。