table('dept_category')." order&"/> table('dept_category')." order&">
Home > Backend Development > PHP Tutorial > 何位看下这个循环嵌套数组有误吗

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

WBOY
Release: 2016-06-13 12:02:07
Original
862 people have browsed it

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

function get_number_list($cat_id)<br />{<br /> $sql=$GLOBALS['db']->query("select * from ".$GLOBALS['db']->table('dept_category')." order by sort");<br /> while($row=$GLOBALS['db']->fetch_array($sql)){<br />   if($row){<br />   $result=$GLOBALS['db']->query("select * from ".$GLOBALS['db']->table('number')." where dept=".$row['cat_id']." and cat_id='$cat_id' order by sort");<br />	 while($rows=$GLOBALS['db']->fetch_array($result)){<br />	    if($rows){<br />	   $number_show[] = array(<br />	          'id' => $rows['id'],<br />			  'title' => $rows['title'],<br />			  'user' => $rows['username']<br />	   );<br />	  }<br />	 }<br />        $cat_name[]=array(<br />	      'sort' => $row['sort'],<br />	      'cat_name' => $row['cat_name'],<br />		  'topid' => $number_show<br />	 );<br />     }<br />   }<br />   return $cat_name;<br /> }
Copy after login

这个是源代码,这是截图:
这个是静态页面代码:
<table width="99%" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"><br />                             {foreach from=$dept_list name=dept_list item=dept}<br />							  <tr bgcolor="#FFFFFF"><br />                                <td    style="max-width:90%" colspan="2" class="bumen"> {$dept.cat_name}</td><br />                                </tr><br />							{foreach from=$dept.topid item=number}<br />                              <tr bgcolor="#FFFFFF"><br />                                <td width="46%" height="25" align="center">{$number.user}</td><br />                                <td width="54%" align="center">{$number.title}</td><br />                              </tr><br />							    {/foreach}<br />							  {/foreach}<br />                            </table>
Copy after login

------解决方案--------------------
因为你number_show没有清空,第一个记录获取到的一直在,所以后面就都有了。
------解决方案--------------------
function get_number_list($cat_id)<br />{<br /> $sql=$GLOBALS['db']->query("select * from ".$GLOBALS['db']->table('dept_category')." order by sort");<br /> while($row=$GLOBALS['db']->fetch_array($sql)){<br />   if($row){<br />   $result=$GLOBALS['db']->query("select * from ".$GLOBALS['db']->table('number')." where dept=".$row['cat_id']." and cat_id='$cat_id' order by sort");<br />     while($rows=$GLOBALS['db']->fetch_array($result)){<br />        if($rows){<br />       $number_show[] = array(<br />              'id' => $rows['id'],<br />              'title' => $rows['title'],<br />              'user' => $rows['username']<br />       );<br />      }<br />     }<br />        $cat_name[]=array(<br />          'sort' => $row['sort'],<br />          'cat_name' => $row['cat_name'],<br />          'topid' => $number_show<br />     );<br />    unset($number_show);<br />     }<br />   }<br />   return $cat_name;<br />   unset($cat_name);<br /> }
Copy after login
利用unset对数组进行清空。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template