嗨,我对 codeigniter 中的数组有一些问题,我如何将数组传递给视图,这是代码不起作用。
模型 :
public function get_all()
{
$query = $this->db->get('category');
$result = $query->result_array();
foreach( $result as $key => $row )
{
$query = $this->db->get_where('categorysub', array('categoryID'=>$row['categoryID']));
$row['childmenus'] = $query->result_array();
$result[$key] = $row;
}
return $result;
}
控制器 :
$this->load->model(array('mcategory','mcategorysub'));
$data['title']= 'Detail Kategori';
$data[] = array(
'nestedmenu' => $this->mcategory->get_all());
$data['nestedmenu'] = array($data);
$this->load->view($this->template, $data);
看法 :
foreach($nestedmenu as $nestedmenu_type)
{
echo "<h2>" . $nestedmenu_type['name'] . "</h2>";
echo "<ul>";
foreach($nestedmenu_type['childmenus'] as $childmenu)
{
echo "<li>" . $childmenu['name'] . "</li>";
}
echo "</ul>";
}
该程序应显示如下结果:
Menu 1
menu 1a
menu 1b
menu 1c
Menu 2
menu 2a
menu 2b
......
但实际上导致这样的错误:
菜单 1
遇到 PHP 错误严重性:通知消息:未定义索引:子菜单文件名:admin/vcategory_read.php 行号:34
A PHP Error was encountered
严重性:警告
消息:为 foreach() 提供的参数无效
文件名:admin/vcategory_read.php
行号:34