need a VALID html unorder list built via php

Discussion in 'PHP' started by mz906, Jul 7, 2008.

  1. #1
    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 :mad: 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?
     
    mz906, Jul 7, 2008 IP
  2. mz906

    mz906 Peon

    Messages:
    101
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    sorry, it took longer to post this then it did to trouble shoot it lol :D 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:
     
    mz906, Jul 7, 2008 IP
  3. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #3
    <?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?
     
    EricBruggema, Jul 12, 2008 IP
  4. mlkshake

    mlkshake Peon

    Messages:
    73
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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.
     
    mlkshake, Jul 12, 2008 IP
  5. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #5
    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
     
    Danltn, Jul 12, 2008 IP
  6. mlkshake

    mlkshake Peon

    Messages:
    73
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Great job Dan ;)

    replace "is" with "behaves like"
     
    mlkshake, Jul 12, 2008 IP
  7. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #7
    No, print IS a language construct.
     
    Danltn, Jul 12, 2008 IP
  8. mlkshake

    mlkshake Peon

    Messages:
    73
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I don't know what your talking about, I'm talking about my post.


    "print is a function" = "print behaves like a function"
     
    mlkshake, Jul 12, 2008 IP
  9. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #9
    Apologies, I thought you were referring to my post.

    Dan
     
    Danltn, Jul 12, 2008 IP