如题所述
ThinkPHP实现二级循环读取的方法如下:
$Category = D('Category')->where('category_pid=0')->findAll();
//dump($Category);
if(!empty($Category)){ //判断一级是否为空
foreach($Category as $key=>$value){ //循环读取
$pid = $value['category_id'];//字段赋值
$Category[$key]['child'] = D('Category')->where("category_pid =$pid")->select();
//echo D('Category')->getLastSql(); //打印sql语句的写法
}
}
//dump($Category);
$this->assign('Category',$Category);//映射值
$Category = D('Category')->where('category_pid=0')->findAll();
//dump($Category);
if(!empty($Category)){ //判断一级是否为空
foreach($Category as $key=>$value){ //循环读取
$pid = $value['category_id'];//字段赋值
$Category[$key]['child'] = D('Category')->where("category_pid =$pid")->select();
//echo D('Category')->getLastSql(); //打印sql语句的写法
}
}
//dump($Category);
$this->assign('Category',$Category);//映射值
温馨提示:答案为网友推荐,仅供参考