xml child/parent help

Discussion in 'PHP' started by Muller2, Jul 5, 2010.

  1. #1
    Hi,

    I have been trying to build a child/parent navigation for a little while now using PHP (I'm from a .net background) and I can't get close to the desired results. I am loading my data from an xml file using simplexml successfully but I am trying to workout how I can map that into arrays or variables so I can write it out.

    The xml is
    
      <Categories>
      <Category>
        <ID>1</ID>
        <Title>Days</Title>
        <Description />
        <ParentID />
        <Meta />
      </Category>
      <Category>
        <ID>2</ID>
        <Title>Monday</Title>
        <Description />
        <ParentID>1</ParentID>
        <Meta />
      </Category>
      <Category>
        <ID>3</ID>
        <Title>Tuesday</Title>
        <Description />
        <ParentID>1</ParentID>
        <Meta />
      </Category>
      <Category>
        <ID>4</ID>
        <Title>Wednesday</Title>
        <Description />
        <ParentID>1</ParentID>
        <Meta />
      </Category>
      <Category>
        <ID>5</ID>
        <Title>Thursday</Title>
        <Description />
        <ParentID>1</ParentID>
        <Meta />
      </Category>
      <Category>
        <ID>6</ID>
        <Title>Friday</Title>
        <Description />
        <ParentID>1</ParentID>
        <Meta />
      </Category>
      <Category>
        <ID>7</ID>
        <Title>Saturday</Title>
        <Description />
        <ParentID/>
        <Meta />
      </Category>
      <Category>
        <ID>8</ID>
        <Title>Sunday</Title>
        <Description />
        <ParentID/>
        <Meta />
      </Category>
    </Categories>
    
    Code (markup):
    Now my PHP
    
    foreach($categories as $category) {
    
    				if ($category->ParentID != "")
    				{
    					echo "<li><a href=index.php?Cat=$category->ID>$category->Title</a></li>";
    					echo "<ul>";
    					foreach($categories as $subcategory) {
    					if ($subcategory->ParentID == $category->ID)
    					{
    						echo "<li><a href=index.php?Cat=$subcategory->ID>$subcategory->Title</a></li>";
    					}
    					}
    					echo "</ul>";
    				}
    				else
    				{
    					echo "<li><a href=index.php?Cat=$category->ID>$category->Title</a></li>";
    				} 
    			} 
    			echo "</ul>";
    
    PHP:
    So my desired output would be something like this:
    
    <ul id="p7menubar">
    <li><a class="trigger" href="#">Days</a>
    <ul>
    <li><a href="#">Monday</a></li>
    <li><a href="#">Tuesday</a></li>
    <li><a href="#">Wednesday</a></li>
    <li><a href="#">Thursday</a></li>
    <li><a href="#">Friday</a></li>
    </ul>
    </li>
    <li><a href="index.htm">Saturday</a></li>
    <li><a href="index.htm">Sunday</a></li>
    </ul>
    
    HTML:

    If anyone could help me out it would be very much appreciated :)

    Thanks

    Al
     
    Muller2, Jul 5, 2010 IP
  2. Ali Çağatay Yüksel

    Ali Çağatay Yüksel Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    maybe this could help:
    foreach($xml->children() as $elementName => $child){
                $value = simpleXMLToArray($child, $flattenValues, $flattenAttributes, $flattenChildren, $valueKey, $attributesKey, $childrenKey);
                if(isset($children[$elementName])){
                    if($first){
                        $temp = $children[$elementName];
                        unset($children[$elementName]);
                        $children[$elementName][] = $temp;
                        $first=false;
                    }
                    $children[$elementName][] = $value;
                }
                else{
                    $children[$elementName] = $value;
                }
            }
    PHP:
    from php.net
     
    Ali Çağatay Yüksel, Jul 5, 2010 IP
  3. Muller2

    Muller2 Peon

    Messages:
    31
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Muller2, Jul 6, 2010 IP