php html display hierarchical data -
i have array ($title, $depth)
$title($depth) //////////////////////////////////// electronics(0) televisions(1) tube(2) lcd(2) plasma(2) portable electronics(1) mp3 players(2) flash(3) cd players(2) 2 way radios(2) //////////////////////
how display structure <ul><li>
the basics of it...keep track of depth, , print out <ul>
, </ul>
tags nudge depth toward current depth. keep in mind html not require </li>
tags, , makes life easier. can print out <li>
before each item, , let elements close needed.
now, specifics of going on list, depends on structure (which, @ time of edit, haven't cared share). there 2 sensible ways can think of structure such list, though.
$depth = -1; // may foreach($arr $title => $itemdepth), depending on structure foreach ($arr $item) { // if did 'other' foreach, rid of list($title, $itemdepth) = $item; // note, works decently if depth increases // @ 1 level each time. code won't work if // jump 1 5 (the intervening <li>s won't // generated), there's no sense in pretending cover // case `while` or `str_repeat`. if ($depth < $itemdepth) echo '<ul>'; elseif ($depth > $itemdepth) echo str_repeat('</ul>', $depth - $itemdepth); echo '<li>', htmlentities($title); $depth = $itemdepth; } echo str_repeat('</ul>', $depth + 1);
this won't generate valid xhtml. people shouldn't using xhtml anyway.
Comments
Post a Comment