哪位看下这个循环嵌套数组有误吗?
function get_number_list($cat_id)
{
$sql=$globals['db']->query(select * from .$globals['db']->table('dept_category'). order by sort);
while($row=$globals['db']->fetch_array($sql)){
if($row){
$result=$globals['db']->query(select * from .$globals['db']->table('number'). where dept=.$row['cat_id']. and cat_id='$cat_id' order by sort);
while($rows=$globals['db']->fetch_array($result)){
if($rows){
$number_show[] = array(
'id' => $rows['id'],
'title' => $rows['title'],
'user' => $rows['username']
);
}
}
$cat_name[]=array(
'sort' => $row['sort'],
'cat_name' => $row['cat_name'],
'topid' => $number_show
);
}
}
return $cat_name;
}
这个是源代码,这是截图:
这个是静态页面代码:
{foreach from=$dept_list name=dept_list item=dept}
{$dept.cat_name}
{foreach from=$dept.topid item=number}
{$number.user}
{$number.title}
{/foreach}
{/foreach}
------解决方案--------------------
因为你number_show没有清空,第一个记录获取到的一直在,所以后面就都有了。
------解决方案--------------------
function get_number_list($cat_id)
{
$sql=$globals['db']->query(select * from .$globals['db']->table('dept_category'). order by sort);
while($row=$globals['db']->fetch_array($sql)){
if($row){
$result=$globals['db']->query(select * from .$globals['db']->table('number'). where dept=.$row['cat_id']. and cat_id='$cat_id' order by sort);
while($rows=$globals['db']->fetch_array($result)){
if($rows){
$number_show[] = array(
'id' => $rows['id'],
'title' => $rows['title'],
'user' => $rows['username']
);
}
}
$cat_name[]=array(
'sort' => $row['sort'],
'cat_name' => $row['cat_name'],
'topid' => $number_show
);
unset($number_show);
}
}
return $cat_name;
unset($cat_name);
}
利用unset对数组进行清空。