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} | {$dept.cat_name} |
{foreach from=$dept.topid item=number} | {$number.user} | {$number.title} |
{/foreach} {/foreach}
回複討論(解決方案)
因為你number_show沒有清空,第一個記錄擷取到的一直在,所以後面就都有了。
另外
while($row=$GLOBALS['db']->fetch_array($sql)){ if($row){
$row已經是在While的條件裡了,沒必要再加個if判斷了,因為如果這個If是假的話,根本就進不了while了。
因為你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對數組進行清空。
謝謝兩位的回答