Home  >  Article  >  Backend Development  >  foreach循环

foreach循环

WBOY
WBOYOriginal
2016-06-20 12:30:091001browse

        //获取格式化组合分类json数组        $class  =  $this->tclass->get_all_class($this->tbname);        $json_class = array();        $json_class2 = array();//怎么能让foreach这段无限循环?        foreach ($class as $key=>$value) {			  if($class[$key]['sonclass'] && $class[$key]['bclassid'] == 0) //如果有子分类            {                $json_class[$key]['id'] = $value['classid'];                $json_class[$key]['text'] = $value['classname'];                $son_arr = $this->tclass->get_son_class($value['sonclass'],$this->tbname);                foreach ($son_arr as $k=>$v) {                   $json_class1[$k]['id'] = $v['classid'];                   $json_class1[$k]['text'] = $v['classname'];                   }               $json_class[$key]['children'] = $json_class1;            }           else if($class[$key]['bclassid'] == 0)            {                $json_class2[$key]['id'] = $value['classid'];                $json_class2[$key]['text'] = $value['classname'];            }        }        $root_class = array('id'=>0,'text'=>'根栏目');   //根栏目数据        $json_class_data = array_merge($json_class,$json_class2); //合并分类数组        array_unshift($json_class_data,$root_class);  //插入数组到最前端        echo json_encode($json_class_data);


这段是输出分类的  怎么能让foreach无限循环呢? 现在只能输出父类和父下的一个子类,但子类也有子类就出不来了 怎么办?


回复讨论(解决方案)

这个你要写一个 寻找儿子的方法 然后发现他有儿子 就掉那个方法 ,至于数据怎么显示 看你自己了 。。。。。

参考下面的写法,表名改成自己的。

function countsubcat($pid){   $r=mysql_query("select count(bclassid) AS say from meskat where bclassid='$pid' limit 1");   $rw=mysql_fetch_array($r);   return $rw['say'];}function listmenu($pid = 0){   $res = mysql_query("select classid,classname,bclassid from meskat where bclassid='$pid'");   while($cat=mysql_fetch_array($res))   {     echo '<li>';     print'<a href="#">'.$cat['classname'].'</a>';     if(countsubcat($cat['classid'])>0)     {      print'<ul>';         listmenu($cat['classid']);      print'</ul>';     }   echo '</li>';   }}echo '<ul>';listmenu(0);  //从一级分类开始echo '</ul>';`

Statement:
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