xml getting children and parents

Hello Experts, I have been trying but I cannot get it right. I have two tasks to accomplish with xml and php and I am stucked now with these two.

I am building a web page that combines xml from a third party (mercadolibre.com, an eBay clone) and my app's db. Right now I need to do two things. First, I need to display all the items that are stored in my database which are children of a given category. Second, I need to place the corresponding breadcrumbs before the current category.

This is the XML_url: http://www.mercadolibre.com.mx/jm/categsXml?as_site_id=MLM 
Let us say that my_db_table looks like this:

item_num; mercadolibre_item_num; mercadolibre_category;
1; 000000001; 9728;
2; 000000002; 7655;
3; 000000003; 123151;
4; 000000004; 39161;

So if I access the page http://www.mywebapp.com/products.php?cat_id=1658

I would like to display something like this:

Start > Computación > Tarjetas de TV // 'Tarjetas de TV' (is cat= 1658) and Computación is (is cat= 1648) which is the parent of 1658. Notice there are no grandparents in which case I would like to display before Computación.

Products:
   000000004 //from category 39161 is a child of 1658
   000000002 //from category 7655 which is a child 9728 and grandson of 1658
   000000001 //from category 1658 which is the cat being subject
   //since cat 123151 is not a sub category of 1658 I do not want to show 000000003

Please help, I am getting crazzzzy.
LVL 4
degarayAsked:
Who is Participating?
 
dnzone88Commented:
Maybe this is what you are looking for? Just pass in the cat_id.
<?php
function getChildNode($node){
	$return = array();
	
	foreach($node as $child){
		if($child->nodeType==XML_ELEMENT_NODE){
			$return[] = $child->getAttribute('id');			
			if($child->hasChildNodes()){
				$return = array_merge($return, getChildNode($child->childNodes));
			}
		}
	}
	return $return;
}

$request='http://www.mercadolibre.com.mx/jm/categsXml?as_site_id=MLM';
$xmldoc = new DOMDocument();
$xmldoc->load($request) or die("Could not get XML data");

$item= $_REQUEST["cat_id"];
$string= '';
$parentlist= array();
$childlist= array();

if ($xmldoc->documentElement){
	$categories = $xmldoc->getElementsByTagName("categories");
	if ($categories->length!=0){
		foreach($categories as $cat){
			$category = $cat->getElementsByTagName("category");
			foreach($category as $node){
				$id= $node->getAttribute('id');
				if($id==$item){
					$string= $node->getAttribute('name');
					if($node->hasChildNodes()){
						$childlist = getChildNode($node->childNodes);
					}
					
					$parent= $node->parentNode;
					while($parent->getAttribute('name')!=''){
						$parentlist[] = $parent->getAttribute('id');
						$string = $parent->getAttribute('name')." >> ".$string;
						$parent = $parent->parentNode;
					}
					break;
				}
			}
		}
	}
}
echo "Breadcrumb <br />";
echo "Start >> ".$string;
echo "<br />Parent ID";
echo "<pre>";
print_r($parentlist);
echo "</pre>";
echo "<br />Child ID";
echo "<pre>";
print_r($childlist);
echo "</pre>";
?>

Open in new window

0
 
Beverley PortlockCommented:
So what exactly do you need help with? You've told us what you want to do but not which part of it is causing you all the trouble.
0
 
degarayAuthor Commented:
100%, thanks you really made my day!!!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.