欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

何位看下这个循环嵌套数组有误吗

程序员文章站 2023-12-30 12:12:22
...
哪位看下这个循环嵌套数组有误吗?
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}



{foreach from=$dept.topid item=number}




{/foreach}
{/foreach}
{$dept.cat_name}
{$number.user} {$number.title}

------解决方案--------------------
因为你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对数组进行清空。
何位看下这个循环嵌套数组有误吗

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

相关文章

相关视频


上一篇:

下一篇: