SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Feb 2013
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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

    Code XML:
    <?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>

    Code PHP:
    $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;

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,401
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by fellboy View Post
    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
    What?

  3. #3
    SitePoint Member
    Join Date
    Feb 2013
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

    Code PHP:
    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')");

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,401
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by fellboy View Post
    the prob is [...] all produtcs get duplcated in all the categorys
    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
    PHP Code:
    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
    PHP Code:
    $items $doc->getElementsByTagName('PRODUCT');
    $cat $doc->getElementsByTagName('CATEGORY'); 

  5. #5
    SitePoint Member
    Join Date
    Feb 2013
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks
    Do you know what code i can use to get it working

  6. #6
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,401
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    Maybe tweaking your code a bit like this?

    PHP Code:
    $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


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •