Note that there are some explanatory texts on larger screens.

plurals
  1. PORecursive function with only one parent and one request
    primarykey
    data
    text
    <p>I want to display a tree of folders. My folders have security access. I define the security only for the first folder, after each subfolder herit.</p> <p>I want to build a recursive function to display all folders available.</p> <p>Here is my data :</p> <pre><code>Folder 1 : {parent, id, label, security_access} - Folder 2 : {parent, id, label} - Folder 3 : {parent, id, label} - Folder 4 : {parent, id, label} Folder 5 : {parent, id, label, security_access} - Folder 6 : {parent, id, label} ... </code></pre> <p>So I get first level folders with security_access (considering $free_folders variable), but how to loop through subfolders ? With only 1 database request, I have a lot of folders and I can't have 200 requests. Here is my code and I can't find when to use recursion :</p> <pre><code>$folders = $this-&gt;db-&gt;get_where('file', array('mime' =&gt; 'directory'))-&gt;result(); $folders_output = array(); foreach($folders as $folder) { $folders_all[$folder-&gt;parent_id][] = array( 'label' =&gt; $folder-&gt;name, 'parent' =&gt; $folder-&gt;parent_id, 'id' =&gt; $folder-&gt;id, ); } foreach ($folders as $folder) { // Get first level folders (parent_id = 1) // $folder_free contains files accessible to users if ($folder-&gt;parent_id == 1 &amp;&amp; in_array($folder-&gt;id, $folders_free)) { $folders_output[$folder-&gt;id]-&gt;parent = $folder-&gt;parent_id; $folders_output[$folder-&gt;id]-&gt;label = $folder-&gt;name; $folders_output[$folder-&gt;id]-&gt;id = $folder-&gt;id; } } // Loop each first level folder $_parent = 0; foreach($folders_output as $id =&gt; $folder) { do { // ... 14th infinite loop // How can I push each subfolders of my first level folder ? $parent = $folders_all[$id][0]['parent']; } while ($_parent != 1); } // Show unordored list with folders - works ! $treeview = $this-&gt;treeview(1, 0, $folders_output); </code></pre> <p>I really can't find the easiest solution.. Thanks ! </p> <p>Ok I think it's ok .. I used recursive function, not really a smart way I think when I see my results... :</p> <pre><code>foreach($folders_free as $folder) { $_results[] = $this-&gt;_dossiers($folders_all, $folder); } ------------------------------------------------ private function _dossiers($array, $parent) { $results = array(); foreach ($array AS $entry) { if ($entry-&gt;parent == $parent) { $object = new stdClass(); $object-&gt;parent = $entry-&gt;parent; $object-&gt;id = $entry-&gt;id; $object-&gt;label = $entry-&gt;label; $results[] = $object; // Recursive call $results[] = $this-&gt;_dossiers($array, $entry-&gt;id); } } return $results; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload