Which one can 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}
Reply to discussion (solution)
Because number_show is not cleared and the first record is obtained, it will be available later.
In addition
while($row=$GLOBALS['db']->fetch_array($sql)){ if($row){
$ Row is already in the While condition, and there is no need to add an if judgment, because If this if is false, it will not be able to enter while.
Because number_show is not cleared and the first record is obtained, it will be available later.
What should I do?
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.
Thank you for your answers.