Self-calling loops

    Date: 01/07/08 (PHP Community)    Keywords: html

    I have a script that calls to itself, though it's initiated from a parent script. Each subsequent child record is indented x spaces for visual representation, but I'm having trouble resetting the value of the counter at certain points. Example:


    I have 5 records as so, and should look like this:

    Record 1
    -Subrecord 1
     -Sub Subrecord 1
    -Subrecord 2
    -Subrecord 3

    However, this is what my script currently shows:

    Record 1
    -Subrecord 1
     -Sub Subrecord 1
     -Subrecord 2 <- This should be to the left one more space
    -Subrecord 3



    //Get all top-level (parent) records here
    while ($row = $d->dbFetchObject($result)) {
     ..... html output here....
     $oddRow = ($oddRow) ? 0 : 1;
     if (db::hasChildren($row->id)) {
    	createMenuHierarchyTable($row->id);
    	$oddRow = ($oddRow) ? 0 : 1;
     }
    }
    
    function createMenuHierarchyTable( $pid, $depth = 1) {
      global $d, $oddRow;
    
      ...execute query here to get children...
      while ($row = $d->dbFetchObject($result)) { ?>
        echo str_repeat(" ", $depth) . 'L ' . $row->menu_item_title;
        $oddRow = ($oddRow) ? 0 : 1;
        if (db::hasChildren($row->id)) {
    	$depth += 1;
    	createMenuHierarchyTable($row->id, $depth);
    	$oddRow = ($oddRow) ? 0 : 1;
        } else
    	$depth--;	
    	}
        }
    }
    



    I'm not sure how to subtract from the $depth variable otherwise. It resets correctly by Subrecord 3.

    Source: http://community.livejournal.com/php/609012.html

« DATETIME || XML file never appears when... »


antivirus | apache | asp | blogging | browser | bugtracking | cms | crm | css | database | ebay | ecommerce | google | hosting | html | java | jsp | linux | microsoft | mysql | offshore | offshoring | oscommerce | php | postgresql | programming | rss | security | seo | shopping | software | spam | spyware | sql | technology | templates | tracker | virus | web | xml | yahoo | home