【发布时间】:2014-07-07 10:32:47
【问题描述】:
这应该是一个简单的将数组传递给视图。控制器设置为从表中提取所有数据。我已经初始化了表库并使用 $this->load->model(array('Account') 加载了模型代码。我已经构建了完整的 crud 方法。
我的视图页面上出现了一个未定义的变量。
控制器代码是:
public function index(){
$this->load->library('table');
$children = array();
$this->load->model(array('Account'));
$children = $this->Account->get(); //retrieves all the records in the database
foreach ($children as $child){
$account = new Account();
$account->load($account->id);
$children[] = array(
$account->id,
$account->familyName,
$account->addr1,
$account->city,
$account->state,
$account->zip,
$account->phone1,
$account->email,
$account->parent_only,
);
}
$this->load->view('main_view');
$this->load->view('body_content_ma', array(
'body_content_ma' => $children,
));
}
在视图页面上,我收到一个错误,指出未定义的变量子项。您认为我在这里缺少什么:
查看代码为:
<?php
$this->table->set_heading('ID', 'Family Name', 'City', 'State', 'Zip', 'Phone', 'Email', 'Parent only Pickup');
echo $this->table->generate($children);
?>
回答:
这是最终奏效的编码。
我不得不重复几次才能找到丢失的东西。
public function index(){
$this->load->library('table');
$children = array();
$this->load->model(array('Account'));
$account = $this->Account->get(); //retrieves all the records in the database
foreach ($account as $c){
$account = new Account();
$account->load($account->id);
$children[] = array(
$c->id,
$c->familyName,
$c->addr1,
$c->city,
$c->state,
$c->zip,
$c->phone1,
$c->email,
$c->parent_only,
);
}
//echo '<tt><pre>' . var_export($children, TRUE) . '</pre></tt>';
$this->load->view('main_view');
$this->load->view('body_content_ma', array(
'body_content_ma' => $children
));
}
我有一种感觉,我多次使用 $children 并覆盖了变量。我是正确的。一旦我声明了变量。我不应该再次使用它。
当我将变量 $children 重新分配给 $children = $this->Account->get(); 我想我有效地覆盖了之前声明的数组。因此,即使数组被填充,数组引用也被破坏了。
我确实在视图代码中使用了 $body_content_ma 来生成表格。 现在一切都在渲染。
开始分页!
【问题讨论】:
标签: php codeigniter view