SitePoint Sponsor

User Tag List

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

    i cant seem to get the foreach statment to work

    i am try to wright some code the code wrights to the data base ok but only one entry shows up i cant seem to get the foreach statment to work it dont loop here is my code
    thank you

    Code:
    <?php
    
    ini_set('display_errors','On');
    
    echo "starting";
    
    //mysql connection
    $con2 = mysql_connect("localhost","user","pass");
    if (!$con2)  {  
    die('Could not connect: ' . mysql_error());  
    }
    
    $selectdb = mysql_select_db("database", $con2);
    if (!$selectdb)  { 
    die('Database not used: ; ' . mysql_error());  
    }
    
    echo "connected to DB<br /><br />";
    
    
    //simplexml load xml file 
    { 
    
    $xml = simplexml_load_file('myproduct.xml');
    mysql_query('DELETE FROM products WHERE 1');
    mysql_query('DELETE FROM categories_description WHERE 1');
    mysql_query('DELETE FROM products_to_categories WHERE 1');
    }
    {
    foreach($xml->CREATED->CATEGORY->PRODUCT as $product);
    
    
    foreach($xml->xpath('//PRODUCT/@ITEM') as $productitemid);
    
    
    foreach($xml->CREATED->CATEGORY as $categories);
    
    
    foreach($xml->xpath('//CATEGORY/@id') as $id);
    
    
    foreach($xml->xpath('//CATEGORY/@name') as $name);
    
    
    
    
    
    
    mysql_query("INSERT INTO products (products_model,products_id) VALUES ('$product->MODEL','$productitemid')");
    mysql_query("INSERT INTO products_to_categories (products_id,categories_id) VALUES ('$productitemid','$id')");
    mysql_query("INSERT INTO categories (categories_id) VALUES ('$id')");
    mysql_query("INSERT INTO categories_description (categories_id,categories_name) VALUES ('$id','$name')");
    mysql_query("INSERT INTO categories_description (categories_name) VALUES ('$name')");
    
    
    
    //echo "$categories\n<br /><br />";
    
    
    
     // or die(mysql_error());
    
     echo "inserted into mysql<br /><br />";
    
    //show updated records            
    printf ("Records inserted: %d\n", mysql_affected_rows());
    
    }
    //close connection
    
    mysql_close($con2);
    ?>
    Last edited by Force Flow; Feb 23, 2013 at 16:10. Reason: added code tags

  2. #2
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    515
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    foreach is a construct:

    PHP Code:
    foreach ($array AS $value) {

      
    // your code goes here
      // it will be executed for each array element


    Denny Schlesinger
    web services

  3. #3
    SitePoint Member
    Join Date
    Feb 2013
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i have put in code like you now i get all the right products_id but all the products_model are the same number

    foreach($xml->xpath('//PRODUCT/@ITEM') as $productitemid){
    foreach($xml->CREATED->CATEGORY->PRODUCT as $product)

    mysql_query("INSERT INTO products (products_id,products_model) VALUES ('$productitemid','$product->MODEL')");

    }

  4. #4
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    515
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by fellboy View Post
    i have put in code like you now i get all the right products_id but all the products_model are the same number
    PHP Code:
    foreach($xml->xpath('//PRODUCT/@ITEM') as $productitemid){


    foreach(
    $xml->CREATED->CATEGORY->PRODUCT as $product)
    mysql_query("INSERT INTO products (products_id,products_model) 
    VALUES ('
    $productitemid','$product->MODEL')");



    You still have a bad foreach inside a good foreach.
    Denny Schlesinger
    web services

  5. #5
    SitePoint Member
    Join Date
    Feb 2013
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    can you tell me how to fix it
    thanh you

  6. #6
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    515
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Same as before!
    PHP Code:
    foreach($xml->xpath('//PRODUCT/@ITEM') as $productitemid){ 

        foreach(
    $xml->CREATED->CATEGORY->PRODUCT as $product) {

            
    mysql_query("INSERT INTO products (products_id,products_model)  
            VALUES ('
    $productitemid','$product->MODEL')")

        }


    But, are you sure you need to nest two foreach()? To know that I would have to know the structure of the array or arrays you are working with.

    If they don't need to be nested, this might work

    PHP Code:
    foreach($xml->xpath('//PRODUCT/@ITEM') as $productitemid){ 

        
    mysql_query("INSERT INTO products (products_id,products_model)  
        VALUES ('
    $productitemid','$xml->CREATED->CATEGORY->PRODUCT')")


    Or you might need
    PHP Code:
    foreach($xml->xpath('//PRODUCT/@ITEM') as $key=>$productitemid){ 

        
    mysql_query("INSERT INTO products (products_id,products_model)  
        VALUES ('
    $productitemid',
             '
    $xml->xpath('//PRODUCT/@ITEM')[$key]['something']')")


    It depend on how the array()s are organized.
    Denny Schlesinger
    web services

  7. #7
    SitePoint Member
    Join Date
    Feb 2013
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try them know luck
    this is the xml

    Code XML:
    <STOREITEMS>
    <CREATED value="Fri Feb 22 1:01:02 GMT 2013">
    <CATEGORY id="442" name=" > test">
    <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>

  8. #8
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    515
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Sorry, I don't know how to work with XML.
    Denny Schlesinger
    web services


Tags for this Thread

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
  •