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

Data nested loops in thinkphp, thinkphp nested loops_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:23:081224browse

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>

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>

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();

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>

The effect is:

thinkphp nested loop

349c7b40bddaf0f4fd36d3e3788a599a
fd6718f05f5768cd4a402064e17fe5c0
{$sub}
0c2cdd09f5f1098aaa1356781bc5fc07
0c2cdd09f5f1098aaa1356781bc5fc07

How to loop output in thinkphp?

You need to use tag nesting, please refer to ThinkPHP3.0 Complete Development Manual 8.21 Tag nesting:
2c7953a8f6f793cfefe6eaa0b0257de7
3f159fb971701dde7c2ceb90f845e835
{$sub.name}
0c2cdd09f5f1098aaa1356781bc5fc07
0c2cdd09f5f1098aaa1356781bc5fc07

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...
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