Which one can check whether this nested loop array is incorrect?

Source: Internet
Author: User
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}  
  {foreach from=$dept.topid item=number}                              
       {/foreach}  {/foreach}                            
  
 {$dept.cat_name}
{$number.user} {$number.title}


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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.