How do you get the code to do the produtcs in the first category then the next

how do you get the code to do the produtcs in the first category then the next products in the next category the dabase seem to want them in sequence

<?xml version="1.0" encoding="iso-8859-1"?>
 <STOREITEMS>
 <CREATED value="Fri Feb 22 1:01:02 GMT 2013">
  <CATEGORY id="441" name=" > test1">
  <PRODUCT ITEM="12796">
   <NAME>test1</NAME>
   <MODEL>bb2018</MODEL>
  <PRICE>2.28</PRICE>
   <RRP>3.99</RRP>
   <THUMB>bb2018s.jpg</THUMB>
    <IMAGE>bb2018.jpg</IMAGE>
     <DESCRIPTION>
       test1
      </DESCRIPTION>
      <POWER/>
      <SIZE/>
      <ATTRIBUTES NAME="Size" ATTRIBUTEID="2">
        <ATTRIBUTEVALUES VALUE="16" TITLE="Small" PRICEADJUST="0.00"/>
         <ATTRIBUTEVALUES VALUE="17" TITLE="Medium" PRICEADJUST="0.00"/>
          <ATTRIBUTEVALUES VALUE="18" TITLE="Large" PRICEADJUST="0.00"/>
          </ATTRIBUTES>
         </PRODUCT>
       </CATEGORY>
    <CATEGORY id="442" name=" > test2">
     <PRODUCT ITEM="12805">
    <NAME>test2</NAME>
     <MODEL>bb2034</MODEL>
     <PRICE>0.58</PRICE>
     <RRP>1.50</RRP>
      <THUMB>bb2034s.jpg</THUMB>
      <IMAGE>bb2034.jpg</IMAGE>
      <DESCRIPTION>
        test2
       </DESCRIPTION>
       <POWER/>
      <SIZE/>
        </PRODUCT>
         </CATEGORY>
        <CATEGORY id="4423" name=" > test3">
        <PRODUCT ITEM="13719">
         <NAME>test3?</NAME>
         <MODEL>BCPG02</MODEL>
         <PRICE>2.83</PRICE>
         <RRP>4.95</RRP>
       <THUMB>bcg02s.jpg</THUMB>
   <IMAGE>bcpg02.jpg</IMAGE>
     <DESCRIPTION>
     test3
    </DESCRIPTION>
    </PRODUCT>
     </CATEGORY>
     </CREATED>
     </STOREITEMS>
$doc = new DOMDocument();
$var = $doc->load('shop.xml');

$root = $doc->documentElement;   //root node
$items = $doc->getElementsByTagName('PRODUCT');
$cat = $doc->getElementsByTagName('CATEGORY');

foreach ($cat as $cats){
foreach ($items as $bar)
if ($cats > $bar)

     $categoriesid = $cats->getAttribute('id');
  $categoriesname = $cats->getAttribute('name');
	$productsid = $bar->getAttribute('ITEM');
	$modelcode = $bar->getElementsByTagName('MODEL')->item(0)->nodeValue;
    $rrp = $bar->getElementsByTagName('RRP')->item(0)->nodeValue;
    $productsdescription = $bar->getElementsByTagName('DESCRIPTION')->item(0)->nodeValue;
   $prodname = $bar->getElementsByTagName('NAME')->item(0)->nodeValue;
   

What?

what is happing is the data is being loaded into the data base ok all the category and the products the prob is there are over 3000 products and 30 categorys all produtcs get duplcated in all the categorys this ic the mysql code
hope you help
thanks

mysql_query("INSERT INTO categories_description (categories_id,categories_name) VALUES ('$categoriesid','$categoriesname')");
  mysql_query("INSERT INTO products_to_categories (products_id,categories_id) VALUES ('$productsid','$categoriesid')");
mysql_query("INSERT INTO categories (categories_id) VALUES ('$categoriesid')");
   mysql_query("INSERT INTO products (products_id,products_model,products_price,products_status) VALUES ('$productsid','$modelcode','$rrp','1')");
mysql_query("INSERT INTO products_description (products_id,products_name,products_description) VALUES ('$productsid','$prodname','$productsdescription')");

Yes, that’s what you coded. You are extracting a list of categories and a list of products from the xml file, and then do this


foreach ($cat as $cats){
  foreach ($items as $bar) ...

So whatever you do inside that second foreach, you do it for each category-product combination.

Whatever link there was between certain categories and products in the xml file was lost when you did this

$items = $doc->getElementsByTagName('PRODUCT');
$cat = $doc->getElementsByTagName('CATEGORY');

thanks
Do you know what code i can use to get it working

Maybe tweaking your code a bit like this?


$cat = $doc->getElementsByTagName('CATEGORY');  
foreach ($cat as $cats) {
    $items = $cats->getElementsByTagName('PRODUCT');
    foreach ($items as $bar) ...  

This way, you loop through the categories, and for each category, you then loop through only the products for that category