Re: Simple recursion function not working. Please help newbie

From: Date: Tue, 29 Apr 2003 11:15:26 +0000
Subject: Re: Simple recursion function not working. Please help newbie
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-145621@lists.php.net to get a copy of this message
You have it all broken. You operate only on $subdirs and don't open the directory. Use function get_dirs($path) { if(!is_dir($path)) {
       echo "<!-- Warning: $path is not a directory -->";
       return array();
} $resp=array($path); $d=opendir($path); while($f=readdir($d)) {
       if(is_dir($path.'/'.$f) && !preg_match('/^\./',$f) ) {
           $resp=array_merge($resp,get_dirs($path.'/'.$f));
       }
} closedir($d); return $resp; } I took it from my code, it prints the whole path and does not do any indenting, so adjust it to your needs. David Wilson wrote:
Hey folks. I've got a small recursive function to display the tree hierarchy of directories. My problem is that the function will go until it gets to the end of the first branch. Then it seems like that is it. The subdirs array is an [n][3] array. function show_tree($parent_ID, $indent) { global $subdirs; echo "<td>In show_tree. parent = $parent_ID</td><br>\n"; foreach ($subdirs as $item) { echo "<td>$item[2]</td><br>\n";
       #[0] == ID
       #[1] == Parent Dir. ID
       #[2] == Filename/Directory
       if ($item[1] == $parent_ID)
       {
           echo "<option value='$item[0]'>";
           for ($i=0; $i < $indent; $i++)
               echo "&nbsp;";
           echo "$item[2]</option>\n";
           $indent++;
echo "<td>Calling show_tree with $item[0]</td><br>\n";
           show_tree($item[0], $indent);
       }
} Thanks for any help that you can shed on this for me. -David Wilson Below is the output... <html><head><link rel=stylesheet type='text/css' href='../layout/default/css/common.css'> <script language='JavaScript' src='../lib/chkform.js' type='text/javascript'></script> </head><body bgcolor="#E0E0E0"> <table bgcolor=#D5D5D5 cellspacing=0 cellpadding=3 border=1 width=500> <td width=230><a href='../help/en/files.html' target=_blank><b>File management</b></a></td> <td>&nbsp;New&nbsp;:</td> <td width=50><a href='filemanager.php?mode=forms&sort=&up=&action=upload'>Upload</a></td> <td width=50><a href='filemanager.php?mode=forms&sort=&up=&action=dir'>Directory</a></td> <td width=50><a href='filemanager.php?mode=forms&sort=&up=&action=link'>Link</a></td> </td></table><br> <table border=1 cellspacing=0 cellpadding=3 bgcolor=#D5D5D5 width=500> <form method=post action='filemanager.php' name='frm' onSubmit="return chkForm('frm','filename','Please specify a description!!')"> <input type=hidden name=typ value='d'> <input type=hidden name='action' value='update'> <tr><td colspan=3><b>Directory</b><br> <tr><td>Name:</td><td colspan=2><input type=text name=filename size=20 value='e-one'></td></tr> <tr><td>Directory: </td><td> <input type=radio name='c_m' value='c'>Copy <input type=radio name='c_m' value='m'>Move<br>with new values </td> <td>In show_tree. parent= 0</td><br> <td>Developer Dir</td><br> <option value='5'>Developer Dir</option> <td>Calling show_tree with 5</td><br> <td>In show_tree. parent= 5</td><br> <td>Developer Dir</td><br> <td>src</td><br> <option value='14890'>&nbsp;src</option> <td>Calling show_tree with 14890</td><br> <td>In show_tree. parent= 14890</td><br> <td>Developer Dir</td><br> <td>src</td><br> <td>web</td><br> <option value='14891'>&nbsp;&nbsp;web</option> <td>Calling show_tree with 14891</td><br> <td>In show_tree. parent= 14891</td><br> <td>Developer Dir</td><br> <td>src</td><br> <td>web</td><br> <td>cgi-bin</td><br> <option value='16908'>&nbsp;&nbsp;&nbsp;cgi-bin</option> <td>Calling show_tree with 16908</td><br> <td>In show_tree. parent= 16908</td><br> <td>Developer Dir</td><br> <td>src</td><br> <td>web</td><br> <td>cgi-bin</td><br> <td>wise</td><br> <option value='16911'>&nbsp;&nbsp;&nbsp;&nbsp;wise</option> <td>Calling show_tree with 16911</td><br> <td>In show_tree. parent= 16911</td><br> <td>Developer Dir</td><br> <td>src</td><br> <td>web</td><br> <td>cgi-bin</td><br> <td>wise</td><br> <td>e-one</td><br> <option value='17008'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e-one</option> <td>Calling show_tree with 17008</td><br> <td>In show_tree. parent= 17008</td><br> <td>Developer Dir</td><br> <td>src</td><br> <td>web</td><br> <td>cgi-bin</td><br> <td>wise</td><br> <td>e-one</td><br> <td>immucor</td><br> <td>sqm</td><br> <td>std-02.00.00</td><br> <td>std-02.01.00</td><br> <td>std-02.02.00</td><br> <td>std-02.03.00</td><br> <td>CVS</td><br> <td>lib</td><br> <td>msg</td><br> <td>static</td><br> <td>template</td><br> <td>contract</td><br> <td>CVS</td><br> <td>menu</td><br> <td>CVS</td><br> <td>CVS</td><br> <td>salescontractdetail</td><br> <td>salescontractinq</td><br> <td>CVS</td><br> <td>CVS</td><br> <td>CVS</td><br> <td>images</td><br> <td>CVS</td><br> <td>en</td><br> <td>CVS</td><br> <td>CVS</td><br> <td>contract</td><br> <td>CVS</td><br> <td>sales</td><br> <td>CVS</td><br> <td>config</td><br> <option value='17252'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;config</option> <td>Calling show_tree with 17252</td><br> <td>In show_tree. parent= 17252</td><br> <td>Developer Dir</td><br> <td>src</td><br> <td>web</td><br> <td>cgi-bin</td><br> <td>wise</td><br> <td>e-one</td><br> <td>immucor</td><br> <td>sqm</td><br> <td>std-02.00.00</td><br> <td>std-02.01.00</td><br> <td>std-02.02.00</td><br> <td>std-02.03.00</td><br> <td>CVS</td><br> <td>lib</td><br> <td>msg</td><br> <td>static</td><br> <td>template</td><br> <td>contract</td><br> <td>CVS</td><br> <td>menu</td><br> <td>CVS</td><br> <td>CVS</td><br> <td>salescontractdetail</td><br> <td>salescontractinq</td><br> <td>CVS</td><br> <td>CVS</td><br> <td>CVS</td><br> <td>images</td><br> <td>CVS</td><br> <td>en</td><br> <td>CVS</td><br> <td>CVS</td><br> <td>contract</td><br> <td>CVS</td><br> <td>sales</td><br> <td>CVS</td><br> <td>config</td><br> <td>CVS</td><br> <td>data</td><br> <td>lib</td><br> <td>msg</td><br> <td>static</td><br> <td>template</td><br> <td>common</td><br> <td>CVS</td><br> <td>inventory</td><br> <td>menu</td><br> <td>sales</td><br> <td>shop</td><br> <td>CVS</td><br> <td>ead</td><br> <td>CVS</td><br> <td>CVS</td><br> <td>iteminq</td><br> <td>prodspec</td><br> <td>CVS</td><br> <td>CVS</td><br> <td>bombrowser</td><br> <td>category</td><br> <td>CVS</td><br> <td>select</td><br> <td>splitframe</td><br> <td>CVS</td><br> <td>CVS</td><br> <td>CVS</td><br> <td>CVS</td><br> <td>cart</td><br> <td>CVS</td><br> <td>orders</td><br> <td>CVS</td><br> <td>CVS</td><br> <td>CVS</td><br> <td>bom</td><br> <td>CVS</td><br> <td>images</td><br> <td>shop</td><br> <td>CVS</td><br> <td>splitframe</td><br> <td>CVS</td><br> <td>CVS</td><br> <td>CVS</td><br> <td>CVS</td><br> <td>common</td><br> <td>CVS</td><br> <td>inventory</td><br> <td>sales</td><br> <td>shop</td><br> <td>CVS</td><br> <td>CVS</td><br> <td>CVS</td><br> <td>CVS</td><br> <td>CVS</td><br> <td>CVS</td><br> <option value='17254'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CVS</option> <td>Calling show_tree with 17254</td><br> <td>In show_tree. parent= 17254</td><br> <td>Developer Dir</td><br> <td>src</td><br> <td>web</td><br> <td>cgi-bin</td><br> <td>wise</td><br> <td>e-one</td><br> [[CLIPPED. Finishes searching through the array without finding the 17254 and exits]] </tr><tr><td width=150>Category:</td> <td width=200>&nbsp;New&nbsp;: <br> <input type=text name=kat size=20></td> <td width=150>or:<br><select name=kat2>&nbsp;<option value=''> <option value='' selected> <option value='Wise Category'>Wise Category <option value='' selected> </select></td></tr> <tr><td>Access:<br>(multiple select with the 'Ctrl'-key):</td> <td><input type=radio name=acc value='private' > me &nbsp; <input type=radio name=acc value='group' checked > all <br> <input type=radio name=acc value='same_as_dir' > same as directory <br> <img src='../img/s.gif' width=160 height=1 vspace=2><br><input type=checkbox name='acc_write' value='w' > Write access</td><td><input type='radio' name='acc' value='3'>some:<br> <select name=persons[] multiple size=5 > <option value=test>Test, Test </select></td></tr> <tr><td colspan=3>Comment:<br><textarea name=remark cols='65' rows='6'></textarea></td></tr> <td colspan=2><input type=checkbox name=notify> Notify all group members</td> <td><input type=checkbox name=versioning value='v' > Version management</td></tr> <input type=hidden name='name' value='Wilson'> <input type=hidden name=sort value=> <input type=hidden name='ID' value='17008'> <input type=hidden name='mode' value='data'> <tr><td colspan=2><input name='update' value='update' type=submit></form> <td colspan=2><form action='filemanager.php' method='post'> <input type='hidden' name='mode' value='view'> <input type='hidden' name='sort' value=''> <input type='hidden' name='up' value=''> <input type='submit' value='back'></form></td> </form></td></tr></table> </body> </html>


« previous php.general (#145621) next »