show categories and subcategories tree from xml file in php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mrtc
Forum Newbie
Posts: 10
Joined: Sat Feb 07, 2015 1:57 pm

show categories and subcategories tree from xml file in php

Post 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!
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: show categories and subcategories tree from xml file in

Post 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.
mrtc
Forum Newbie
Posts: 10
Joined: Sat Feb 07, 2015 1:57 pm

Re: show categories and subcategories tree from xml file in

Post 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
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: show categories and subcategories tree from xml file in

Post by Celauran »

Who said anything about editing XML?
mrtc
Forum Newbie
Posts: 10
Joined: Sat Feb 07, 2015 1:57 pm

Re: show categories and subcategories tree from xml file in

Post by mrtc »

ops i'm sorry i'm replying someone other on other forum :P :P
mrtc
Forum Newbie
Posts: 10
Joined: Sat Feb 07, 2015 1:57 pm

Re: show categories and subcategories tree from xml file in

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: show categories and subcategories tree from xml file in

Post 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.
(#10850)
Post Reply