Home > Backend Development > PHP Tutorial > Data nested loops in thinkphp, thinkphp nested loops_PHP tutorial

Data nested loops in thinkphp, thinkphp nested loops_PHP tutorial

WBOY
Release: 2016-07-13 10:23:08
Original
1347 people have browsed it

Data nested loops in thinkphp, thinkphp nested loops

When doing thinkphp, you need to use nested loops inside loops, and the second loop is related to the outside of.

The document provided by thinkphp official website is:

<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>
Copy after login

The tables I want to use here are: classification table (Table 1), data table (Table 2)

The effect to be achieved is:

<表1_1>
  <表2_1/>
  <表2_2>
</表1_1>
<表1_2>
  <表2_3/>
  <表2_4>
</表1_2>
Copy after login

In fact, the principle is to first find the classification table (Table 1), then query Table 1 and Table 2, and finally output it in the form of a two-dimensional array

Backend code:

     $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();
Copy after login

Foreground output 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>
Copy after login

The effect is:

thinkphp nested loop



{$sub}


How to loop output in thinkphp?

You need to use tag nesting, please refer to ThinkPHP3.0 Complete Development Manual 8.21 Tag nesting:


{$sub.name}


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/840768.htmlTechArticleData nested loops in thinkphp, thinkphp nested loops When doing thinkphp, you need to use nested loops inside loops. And the second loop is related to the outside. Documents given by thinkphp official website...
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