search
HomeBackend DevelopmentPHP Tutorial 40分求ThinkPHP循环嵌套打印出父类与子类的列表解决方法

40分求ThinkPHP循环嵌套打印出父类与子类的列表
大家好,我近段时间在学习thinkPHP框架,我想在模板页面实现循环打印出一个数据列表,格式是:先是父类类别名称,再接着打印出对应的子类列表,再接着打印另外一个父类名称,再打印该父类的对应的子类,以此类推。。。我是用表格排版,格式如:

   
  类别名    简介

  父类名称1   ......  
  子类名称1   .....  
  子类名称2   .....  
  子类名称3   .....  
  父类名称2   ......  
  子类名称1   .....  
  子类名称2   .....  
  子类名称3   .....  


  请问用volist该怎样实现,或者有没有更好的方法,请指教,感激不尽!!



------解决方案--------------------
使用地归来进行遍历就可以了

------解决方案--------------------
可以一试:第一次我都环找出所有parent_id然后记下该记录集的总量parent_count,然后以parent_count往下循环,以此类推……
------解决方案--------------------

HTML code
<tbody>

            <foreach name="list" item="vo">
                <tr rel="{$vo.depa_id}" target="depa_id">
                    <td>{$vo.depa_id}</td>
                    <td>
<?php if ($vo['depa_default'] == 1)
    echo '<b>[主页面] '; ?><a href="__BASEURL__/mudidi/%7B%24vo.dein_id%7D/%7B%24vo.depa_id%7D.html" target="_blank">{$vo.depa_title}</a>
</td>
                    <td>{$vo.label_count}</td>
                    <td>{$vo.model_name}</td>
                    <td>{$vo.depa_order}</td>
                </tr>
                <foreach name="vo.son" item="son">
                    <tr rel="{$son.depa_id}" target="depa_id">
                        <td>{$son.depa_id}</td>
                        <td>
<img src="/static/imghwm/default1.png" data-src="http://bbs.phpchina.com/images/default/tree_linemiddle.gif" class="lazy"   style="max-width:90%" alt="|-" title="|-"><a href="__BASEURL__/mudidi/%7B%24vo.dein_id%7D/%7B%24vo.depa_id%7D.html" target="_blank">{$son.depa_title}</a>
</td>
                        <td>{$son.label_count}</td>
                        <td>{$son.model_name}</td>
                        <td>{$son.depa_order}</td>
                    </tr>
                </foreach>
            </foreach>
            </tbody>
<br><font color="#e78608">------解决方案--------------------</font><br><br><br>模板里面不好递归,比较好的方式是在程序逻辑里递归出大致的树结构,模板里控制样式。
<br><font color="#e78608">------解决方案--------------------</font><br>用关联模型<br>
PHP code

<volist name="list" id="vo">
  <tr>
<td>{$vo.typeName}</td>
<td>{$vo.typeDescription}</td>
</tr>
  <volist name="vo.depat" id="value">
      <tr>
<td>{$value.typeName}</td>
<td>{$value.typeDescription}</td>
</tr>
  </volist>
</volist>
<br><font color="#e78608">------解决方案--------------------</font><br> 这个一般是配合MYSQL做的,通常在数据库设置好的,先是设置父类跟子类同一字段VAL ,然后子类再添加个保存父类ID的字段,然后WEB页面循环出来就行了 <div class="clear">
                 
              
              
        
            </div>
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
PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment