我在显示从数据库表中检索到的数组时遇到问题 这就是我的数组的样子。
请注意我是从数据库表中选择的
<select id="countCardsWithProgramGoal" resultType="java.util.HashMap"
parameterType="java.util.HashMap">
select pgoal, count(*) as count
from PROG
where pgoal in
<foreach collection="programGoals_list" item="item" index="index"
separator="," open="(" close=")">
#{item}
</foreach>
group by pgoal;
</select>
这是我的控制器的索引代码
"WHERE pro_cat_id = 36" ;
Array
(
Array
(
[0] => Array
(
[pro_cat_id] => 1
[cat_name] => men
)
[1] => Array
(
[pro_cat_id] => 2
[cat_name] => women
)
[2] => Array
(
[pro_cat_id] => 3
[cat_name] => Gilamlar
)
[3] => Array
(
[pro_cat_id] => 4
[cat_name] => kids
)
[4] => Array
(
[pro_cat_id] => 5
[cat_name] => others
)
[5] => Array
(
[pro_cat_id] => 27
[cat_name] => game
)
)
其中getCategory是一个模型函数,用于从数据库表中检索所有行
我尝试将 public function index()
{
$res = $this->Categories_model->getCategory();
// ;
// print_r($data);
$data = $res;
echo "<br><br><br><pre>";
print_r($data);
echo "</pre>";
$this->load->view('categories',$data);
}
设置为echo
如何在视图中使用此数据?
如何通过多维数组在此处查看和使用它?
答案 0 :(得分:1)
希望这对您有帮助:
您的index
方法应如下所示:
public function index()
{
$data['categories'] = $this->Categories_model->getCategory();
// print_r($data);
$this->load->view('categories',$data);
}
在您的categories
视图页面中,使用foreach
循环如下:
<?php
if (! empty($categories))
{
foreach ($categories as $category)
{
echo $category['pro_cat_id'];
echo $category['cat_name'];
}
}
?>