Page 1 of 1

show categories and subcategories tree from xml file in php

Posted: Thu Feb 12, 2015 8:16 am
by mrtc
Hi guyz
i want to show categories and subcategories like this
categoriery 1
-- subcategory
-- subcategory
categoriery 2
-- subcategory
-- subcategory

i got xml data like this

Code: Select all

<categories>
	<category>
		<categories_name> NAme 1</categories_name>
		<categories_id>2</categories_id>
		<parent_id>0</parent_id>
		<sort_order>90</sort_order>
	</category>
	<category>
		<categories_name>NAme 2</categories_name>
		<categories_id>3</categories_id>
		<parent_id>2</parent_id>
		<sort_order>5</sort_order>
	</category>
	<category>
		<categories_name>Name 3</categories_name>
		<categories_id>4</categories_id>
		<parent_id>0</parent_id>
		<sort_order>20</sort_order>
	</category>
	<category>
		<categories_name>Name 4</categories_name>
		<categories_id>5</categories_id>
		<parent_id>4</parent_id>
		<sort_order>40</sort_order>
	</category>
</categories>
parent id is locating main category id hope u can understand this

and i'm using simplexml load file to for fetching information from xml file . with that code i able to fetch every category information but i want to show category first then under there show its subcategory

Code: Select all

<?php 
$xml=simplexml_load_file("cc.xml") or die("Error: Cannot create object");
foreach($xml->children() as $category) {  
$catname = $category->categories_name;
$catid = $category->categories_id;
$parentid = $category->parent_id;
$sortorder = $category->sort_order;
echo " <div class='products'>
$catname  | $catid | $parentid | $sortorder
</div> "; 
} ?>
anyone who can help me

Any help would be greatly appreciated!

Re: show categories and subcategories tree from xml file in

Posted: Thu Feb 12, 2015 8:24 am
by Celauran
Looks like you're going to have to iterate over the results and create a multidimensional array so you can nest your subcategories under the correct parent. Once that's done, you can iterate over the resulting array to produce your output.

Re: show categories and subcategories tree from xml file in

Posted: Thu Feb 12, 2015 9:13 am
by mrtc
thats the problem i can't edit this xml because its a live xml feed coming from partner site so is there any other solution for that

Re: show categories and subcategories tree from xml file in

Posted: Thu Feb 12, 2015 9:31 am
by Celauran
Who said anything about editing XML?

Re: show categories and subcategories tree from xml file in

Posted: Thu Feb 12, 2015 10:39 am
by mrtc
ops i'm sorry i'm replying someone other on other forum :P :P

Re: show categories and subcategories tree from xml file in

Posted: Thu Feb 12, 2015 10:45 am
by mrtc
Celauran wrote:Looks like you're going to have to iterate over the results and create a multidimensional array so you can nest your subcategories under the correct parent. Once that's done, you can iterate over the resulting array to produce your output.
hmm can you please give me a example or update that code please

Re: show categories and subcategories tree from xml file in

Posted: Thu Feb 12, 2015 2:22 pm
by Christopher
Well, as you loop through the elements, if the parent is 0 then create a category element in the array e.g., $data[$category->categories_id]['name'] = $category->categories_name;. If it has a parent, then add it to its parent level element (which you may want to initialize if it does not exist yet) e.g., $data[$category->parent_id]['children'][$category->categories_id]['name'] = $category->categories_name;. Then output the data in the array.