PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 16, 2022

[FIXED] How to create tree inside forloop php

 January 16, 2022     cakephp, cakephp-3.0, php     No comments   

Issue

I am using cakephp 3.6 and I have a modules tables in mysql database.

I want to create nested tree structure inside foreach loop. But i am not getting how to build loop in tree format.

Please help me to do so.

Below is my table strucure

id    parent_id  alias
119    NULL     Controllers
120    119      Clients
121    120       add
122    120       Edit
123    119      Users
124    123       add
125    123       Edit

Expected Result

Clients
 --add
 --Edit
 Users
 --add
 --Edit

Below is my code

<?php if(!empty($modulelist)) { foreach ($modulelist as $name){  ?>
    <li class="last dd-item">
            <input type="checkbox" name="short" id="short">
            <label for="short" class="custom-unchecked">Clients</label>     
        <ul>
             <li class="dd-handle">
                 <input type="checkbox" name="short-1" id="short-1">
                 <label for="short-1" class="custom-unchecked">add</label>
             </li>
             <li class="dd-handle">
                 <input type="checkbox" name="short-2" id="short-2">
                 <label for="short-2" class="custom-unchecked">edit</label>
             </li>
        </ul>
    </li>
<?php }} ?> 

Solution

Well you can use ORM behaviors Tree.

https://book.cakephp.org/3.0/en/orm/behaviors/tree.html

Based on Parent ID it will automatically generate TREE.



Answered By - Naveed Ramzan
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing