Which one can check whether this nested loop array is incorrect? Function & nbsp; get_number_list ($ cat_id) {& nbsp; $ SQL $ GLOBALS [db]-& gt; query (select & nbsp; * & nbsp; from & nbsp ;. $ GLOBALS [db]-& gt; table (dept_category ). & nbsp; order & which one should check whether this nested loop array is incorrect?
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;
}
This is the source code, and this is:
This is the static page code:
{foreach from=$dept_list name=dept_list item=dept}
{$dept.cat_name} |
{foreach from=$dept.topid item=number}
{$number.user} |
{$number.title} |
{/foreach} {/foreach}
------ Solution --------------------
Because number_show is not cleared and the first record is obtained, it will be available later.
------ Solution --------------------
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);
}
Use unset to clear the array.