SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Oct 2009
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Issue regarding sub categories in a PHP XML Menu system

    Hi,

    I'm creating an online catalogue of parts for my company, we are using an xml file provided by his supplier. I've got really stuck with the menu which is quite large and i am trying to split it up into sub categories. As the xml file is provided by the supplier I can't change it. I can create categories and sub categories int he menu but I can't seem to get the categories to return up 1 level once i have made a sub categorie. Please see the sample of the xml file and code below:

    XML Sample:
    Code:
    <catalogue categories="117" timestamp="1256111860" description="XXXXXX Product Catalogue for XXXXX" imagebase="">
    −
    <category id="0" name="Root" subcategories="8">
    −
    <category id="3" name="Other Products" subcategories="5">
    <category id="860" name="Installation Equipment and Accessories" subcategories="0">
    			</category>
    <category id="871" name="XXXXX Products" subcategories="0">
    			</category>
    <category id="376" name="August custom products" subcategories="0">
    			</category>
    <category id="161" name="Mobile Phones" subcategories="0">
    			</category>
    <category id="160" name="Photocopiers" subcategories="0">
    			</category>
    </category>
    −
    <category id="1" name="Audio Visual" subcategories="7">
    −
    <category id="92" name="Plasma / LCD" subcategories="1">
    <category id="456" name="LCD Screens" subcategories="0">
    				</category>
    </category>
    −
    <category id="777" name="LCD Monitors" subcategories="1">
    <category id="799" name="Accessories" subcategories="0">
    				</category>
    </category>
    And the PHP:

    Code:
    <?
    	include('domxml-php4-to-php5.php');
    	include('config.php');
    	echo "<ul id='MenuBar1' class='MenuBarVertical'><li>";
    	$PrevSubCat = 0;
    	$SubCatCount = -1;
    	function show_cats($node,$indent="") {
    		
    		// Simple recursive function to display the category in $node and then
    		// traverse it's children
    	
    		if($indent != "") {
    			$id = $node->get_attribute("id");
    			$name = $node->get_attribute("name");
    			$subcategories = $node->get_attribute("subcategories");
    				if($subcategories == 0) {
    					$SubCatCount = SubCatCount -1;
    					echo "<li><a href=showproducts.php?category=" . $id . " target=mainFrame>" . $name . "</a></li>";
    					if ($SubCatCount == 0){
    						echo "</ul></li>";
    						if ($PrevSubCat 
    						}
    				} else {
    					if  ($SubCatCount > 0) {
    						$PrevSubCat
    					$SubCatCount = $subcategories;
    					echo "<li><a class='MenuBarItemSubmenu' href=showproducts.php?category=" . $id . " target=mainFrame> " . $name . "</a><ul>";
    				}
    		} else
    			echo "<a href=showproducts.php target=mainFrame>Home</a><ul>";
    		foreach($node->child_nodes() as $child) {
    			if($child->node_type() == 1)
    				show_cats($child,$indent ."&nbsp;&nbsp;&nbsp;&nbsp;");
    		}		
    	}
    
    	$xml = domxml_open_file($XMLURL . "&category=0");
    	
    	// Locate the first category element, which will be "Root", then traverse the tree
    	$cats = $xml->get_elements_by_tagname('category');
    	$root = $cats[0];
    	show_cats($root);
    	
    ?>

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    As its not clear to me whether you are using PHP4 or 5, if you were using PHP5 could you not use SimpleXml?

    Code:
    $xmlstr = '<catalogue categories="117" timestamp="1256111860" description="XXXXXX Product Catalogue for XXXXX" imagebase="">
    <category id="0" name="Root" subcategories="8">
    
     <category id="3" name="Other Products" subcategories="5">
      <category id="860" name="Installation Equipment and Accessories" subcategories="0"></category>
      <category id="871" name="XXXXX Products" subcategories="0"></category>
      <category id="376" name="August custom products" subcategories="0"></category>
      <category id="161" name="Mobile Phones" subcategories="0"></category>
      <category id="160" name="Photocopiers" subcategories="0"></category>
     </category>
    
     <category id="1" name="Audio Visual" subcategories="7">
      <category id="92" name="Plasma / LCD" subcategories="1">
        <category id="456" name="LCD Screens" subcategories="0"></category>
      </category>
    
      <category id="777" name="LCD Monitors" subcategories="1">
       <category id="799" name="Accessories" subcategories="0"></category>
      </category>
     </category>
     </category>
    </catalogue>';
    SimpleXml hacky but works, shows how to access nodes and attributes at least.
    PHP Code:
    $xml = new SimpleXMLElement($xmlstr);

    foreach( 
    $xml->category->category as $c1 ){
      echo 
    '<h3>' $c1['name'] . '</h3>';

        foreach( 
    $c1->category as $c2 ) {
          echo 
    $c2['name'] . '<br>' 

           foreach( 
    $c2->category as $c3 ) {
             echo 
    $c3['name'] . '<br>' 
           }
        }
     }

    //var_dump( $xml ) ; 
    or is this not the result you are after?

    Other Products

    Installation Equipment and Accessories
    XXXXX Products
    August custom products
    Mobile Phones
    Photocopiers

    Audio Visual

    Plasma / LCD
    LCD Screens
    LCD Monitors
    Accessories

    If not, can you explain what is?

    Off Topic:

    I had to finish off your xml sample with some /categories and /catalogue too

  3. #3
    SitePoint Member
    Join Date
    Oct 2009
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeh, thats great thanks.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •