Display categories and sub categories php from xml file

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

		<categories_name> NAme 1</categories_name>
		<categories_name>NAme 2</categories_name>
		<categories_name>Name 3</categories_name>
		<categories_name>Name 4</categories_name>

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

$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!

None of the categories have subcategories, they have parent ids.

If you mean
parent ids = category
category = subcategory
I would put together an array before you echo the results.

yea they have parent id locating its category id …
i want to show like this from xml

name 1 - id 2

  • Name 2 - id 3
    name 2 is subcategory of name 1 …
    does it make sense

I think I understand, but if your real XML is like the example XML you’ll have problems.

eg. the XML has
name1 id = 2 pid = 0
name2 id = 3 pid = 2
name3 id = 4 pid = 0
name4 id = 5 pid = 4

But there is no categories_id = 0 so you would end up with something like
pid 0 undefined | name1
name1 | name2
pid 0 undefined | name3
name3 | name4

Where not existent, only show the one name?

so is there any solution for that because i cannot change that xml file …
and can you update that php code please

This task is a bit tricky.
You have to use recursion to calculate level of each category in tree.

I made this for you. It works and produces correct nested list with categories.

$xml=simplexml_load_file("cc.xml") or die("Error: Cannot create object");

function buildTreeRecursive($items, &$result_tree, $parent_id=0, $level=1){

    foreach($items as $num=>$item){
        if ($item['parent_id']==$parent_id){
            $item['level'] = $level-1;
            if (!isset($result_tree[$item['id']])){
                $result_tree[$item['id']] = $item;
            if (isset($result_tree[$item['parent_id']])){
            buildTreeRecursive($items, $result_tree, $item['id'], $level);

$cats = array();
$tree = array();

foreach($xml->children() as $category) {  
    $cats[] = array(
        'id' => (string)$category->categories_id, 
        'parent_id' => (string)$category->parent_id,
        'name' => (string)$category->categories_name,
        'order' => (string)$category->sort_order,
        'childs_count' => 0

buildTreeRecursive($cats, $tree);

$last_level = 0; 


    <?php foreach($tree as $id=>$item){ ?>
        <?php for ($i=0; $i<($last_level - $item['level']); $i++) { ?>
        <?php } ?>
        <?php if ($item['level'] <= $last_level) { ?>
        <?php } ?>
            <span><?php echo($item['name']); ?></span>                            

            <?php if ($item['childs_count'] > 0) { ?><ul><?php } ?>

        <?php $last_level = $item['level']; ?>
    <?php } ?>

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.