|
Posted by Rami Elomaa on 03/23/07 21:39
lawpoop@gmail.com kirjoitti:
> Hello!
>
> I am working on a map of a rather large php project that I've been
> working on. I hope to create a map of the files of the project that
> would look like the output of the unix 'tree' command:
>
> .
> |-- index.php
> | |-- menu.php
> | |-- content.php
> | |-- admin_page.php
> | | |-- admin_function1.php.inc
> | | |-- admin_function2.php.inc
> | | |-- admin_function3.php.inc
> | | `-- admin_subpage.php
> | | |-- admin_subfunction1.php.inc
> | | |-- admin_subfunction1.php.inc
> | | `-- admin_subfunction1.php.inc
> | `-- user_page.php
> | |-- user_function1.php.inc
> | |-- user_function2.php.inc
> | |-- user_function3.php.inc
> | `-- user_subpage.php
> | |-- user_subfunction1.php.inc
> | |-- user_subfunction1.php.inc
> | `-- user_subfunction1.php.inc
> |
> |-- orphan_page.php
> `-- deprecated_page.php
>
> I can scan my php files and pull out references to php file names.
> I've created an array that looks like this:
>
> Array
> (
> [0] => ./index.php
> [1] => Array
> (
> [0] => ./clients.php
> [1] => Array
> (
> [0] => ./login_function.inc
> [1] => ./client.php
> [2] => ./client_new.php
> [3] => ./client_edit.php
> )
> [2] => ./inventory.php
> [3] => Array
> (
> [0] => ./login_function.inc
> [1] => ./inventory_item_add.php
> [2] => ./inventory_item_remove.php
> )
>
> [4] => ./daily_report.php
> [5] => ./weekly_report.php
> [6] => ./monthly_report.php
> [7] => ./catalog.php
> [8] => Array
> (
> [0] => ./login_function.inc
> [1] => ./catalog_item_add.php
> [2] => ./catalog_item_remove.php
> )
> [9] => ./orders.php
> [10] => Array
> (
> [0] => ./login_function.inc
> [1] => ./view_order.php
> [2] => ./order_new.php
> [3] => ./order_edit.php
> )
> [11] => ./invoices.php
> [12] => Array
> (
> [0] => ./login_function.inc
> [1] => ./view_invoice.php
> [2] => ./invoice_new.php
> [3] => ./invoice_edit.php
> [4] => ./invoice_print.php
> [5] => ./invoice_email.php
> )
> )
> [2] => ./clients_old.php
> [3] => ./old_inventory.php
> )
>
> However, I'm having problems with the function that reads the array
> and outputs the tree-like results. This is what I'm getting:
>
> - ./index.php
> - - ./clients.php
> - - - ./login_function.inc
> - - - ./client.php
> - - - ./client_new.php
> - - - ./client_edit.php
> - - - ./inventory.php
> - - - - ./login_function.inc
> - - - - ./inventory_item_add.php
> - - - - ./inventory_item_remove.php
> - - - - ./daily_report.php
> - - - - ./weekly_report.php
> - - - - ./monthly_report.php
> - - - - ./catalog.php
> - - - - - ./login_function.inc
> - - - - - ./catalog_item_add.php
> - - - - - ./catalog_item_remove.php
> - - - - - ./orders.php
> - - - - - - ./login_function.inc
> - - - - - - ./view_order.php
> - - - - - - ./order_new.php
> - - - - - - ./order_edit.php
> - - - - - - ./invoices.php
> - - - - - - - ./login_function.inc
> - - - - - - - ./view_invoice.php
> - - - - - - - ./invoice_new.php
> - - - - - - - ./invoice_edit.php
> - - - - - - - ./invoice_print.php
> - - - - - - - ./invoice_email.php
> - - ./clients_old.php
> - - ./old_inventory.php
>
>
> And this is my code:
>
> <?
>
> show_array( $array );
>
> function show_array( $array, $level = 0, $counter = 0 ) {
>
> $count = count($array);
>
>
> foreach ( $array as $key => $value ) {
>
> if ( is_array($value) ) {
>
> $level++;
>
> $counter = 0;
>
> show_array( $value, $level, $counter );
>
> } else {
>
> $counter++;
>
> for ( $i = 0; $i <= $level; $i++ ) {
> echo " - ";
> }
// a nice little function to replace
// the quite unnecessary loop:
str_repeat(' - ', $level);
> echo "$value\n";
>
> if ( $counter == $count ) {
>
> $level--;
$level is decremented here, but $counter won't reach $count on all
occasions, since it's only incremented if the branch is a leaf, yet
$count is the number of all nodes, not just leaves, but branches and
leaves (arrays and values). Well, I must admit that I don't understand
the functionality of $counter on the whole, as it's also set to zero if
the node is a branch, but I think the roots of your problems may have
something to do with how you handle $counter. I may be wrong just as
well, though :)
> $counter = 0;
> }
> }
> }
> }
>
>
> -------------------
> As far as I can tell, the $level counter is not getting decremented
> correctly . Can someone help me out?
>
> Steve Lefevre
>
Nobody said writing recursive functions is easy, but it's the most
commonly used solution for drawing a hierarchical tree. Usually, when
done right (after many trial and error), it's the best solution. You've
got a very good start here, all it needs is a little fine-tuning.
--
Rami.Elomaa@gmail.com
"Olemme apinoiden planeetalla."
[Back to original message]
|