>  기사  >  백엔드 개발  >  thinkphp里数据嵌套循环,thinkphp嵌套循环_PHP教程

thinkphp里数据嵌套循环,thinkphp嵌套循环_PHP教程

WBOY
WBOY원래의
2016-07-13 10:23:081314검색

thinkphp里数据嵌套循环,thinkphp嵌套循环

做thinkphp时要用到循环里面嵌套循环的,并第二个循环是和外面的有关联的。

thinkphp官网给出的文档为:

<volist name=<span>"</span><span>list</span><span>"</span> id=<span>"</span><span>vo</span><span>"</span>>
    <volist name=<span>"</span><span>vo['sub']</span><span>"</span> id=<span>"</span><span>sub</span><span>"</span>><span>
        {$sub.name}
    </span></volist>
</volist>

我这里要用的表为:分类表(表1),数据表(表2)

要实现的效果为:

<表1_1>
  <表2_1/>
  <表2_2>
</表1_1>
<表1_2>
  <表2_3/>
  <表2_4>
</表1_2>

其实原理也是先查出分类表(表1),再把表1和表2相关联查询,最后以二维数组的方式输出

后台代码:

     $m=M(<span>'</span><span>表1</span><span>'</span><span>);
        $m1</span>=M(<span>'</span><span>表2</span><span>'</span><span>);

        $parent</span>=$m-><span>select</span><span>();

        </span><span>foreach</span>($parent <span>as</span> $n=><span> $val){
            $parent[$n][</span><span>'</span><span>voo</span><span>'</span>]=$m1-><span>where</span>(<span>'</span><span>表2里和表1相关联的字段=</span><span>'</span>.$val[<span>'</span><span>表1id</span><span>'</span>].<span>''</span>)-><span>select</span><span>();
        }
        $</span><span>this</span>->assign(<span>'</span><span>list</span><span>'</span><span>,$parent);
        $</span><span>this</span>->display();

前台输出显示:

        <volist name=<span>"</span><span>list</span><span>"</span> id=<span>"</span><span>vo</span><span>"</span>>
                <li><b>{$vo.id}</b></li>
                <volist name=<span>"</span><span>vo['voo']</span><span>"</span> id=<span>"</span><span>sub</span><span>"</span>>
                    <li>{$sub.title}</li>
                </volist>
            </volist>

效果为:

thinkphp嵌套循环



{$sub}


 

thinkphp中怎循环输出?

你这个需要用到标签嵌套,请参考ThinkPHP3.0完全开发手册 8.21 标签嵌套:


{$sub.name}


 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/840768.htmlTechArticlethinkphp里数据嵌套循环,thinkphp嵌套循环 做thinkphp时要用到循环里面嵌套循环的,并第二个循环是和外面的有关联的。 thinkphp官网给出的文档...

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.