so i was going thru my php books for a function to dynamically build a html list, only problem is that it spits out invalid HTML here's the php code: <?php $data = array( "brand", "blog", "clothing" => array( "classic", "lax", "sport" ), "contact" ); function arrayTraverse($arr){ if(!is_array($arr)) { die ("nope, its not an ARRAY"); } echo '<ul>'; foreach ($arr as $key => $value){ if (is_array($value)){ print '<li><a href="link.php">'.$key.'</a></li>'; arrayTraverse($value); } else { print '<li><a href="link.php">'.$value.'</a></li>'; } } echo "</ul>"; } ?> PHP: and it spits out this html code: <ul class="secondary"> <li><a href="link.php">bread</a></li> <li><a href="link.php">eggs</a></li> <li><a href="link.php">meat</a></li> <ul class="secondary"> <li><a href="link.php">fish</a></li> <li><a href="link.php">chicken</a></li> <li><a href="link.php">pig</a></li> </ul> <li><a href="link.php">milk</a></li> </ul> HTML: but it should look something like this: <ul class="secondary"> <li><a href="link.php">link</a></li> <li><a href="link.php">link</a></li> <li><a href="link.php" class="clothing">link</a> <ul> <li><a href="link-link.php">link</a></li> <li><a href="link.php">linka></li> <li><a href="link-link.php">link</a></li> </ul> </li> <li><a href="link.php">link</a></li> </ul> HTML: so does anyone have a script that does this?
sorry, it took longer to post this then it did to trouble shoot it lol well here is the valid html, php code if anyone wants it: <?php $data = array( "bread", "meat" => array( "fish", "pig" ), "milk" ); function arrayTraverse($arr){ if(!is_array($arr)) { die ("nope, its not an ARRAY"); } echo '<ul class="secondary">'; foreach ($arr as $key => $value){ if (is_array($value)){ print '<li><a href="link.php">'.$key.'</a>'; arrayTraverse($value); print '</li>'; } else { print '<li><a href="link.php">'.$value.'</a></li>'; } } echo "</ul>"; } ?> PHP:
<?php $data = array("bread", "meat" => array("fish", "pig"), "milk"); function arrayTraverse($arr) { if (!is_array($arr)) { return; } echo '<ul class="secondary">'; foreach ($arr AS $key => $value) { echo '<li><a href="link.php">'.$key.'</a>'; if (is_array($value)) { arrayTraverse($value); } echo '</li>'; } echo "</ul>"; } ?> PHP: a little bit smaller! b.t.w why using print and echo why not only echo or print?
As EricBruggema said, Why use both? your just making it more confusing. They both do the same thing the only difference is that print is a function so it takes slightly longer to execute.
No, print is actually a language construct which behaves similarly to a function in the fact that it has a return value, whereas echo does not. (which is always int 1) That is the only difference. Frustrating you with minor details as always, Dan
I don't know what your talking about, I'm talking about my post. "print is a function" = "print behaves like a function"