Jump to content

How to read xml


intimvoiro

Recommended Posts

Hello, I'm Italian. so sorry for my English.

So I have this xml file you could help me read it?

I mean I have to pick the product code, and if these variations for each product and enter them in the database.

 

Here's the code:

<Products>
    <Product>
      <Code>13</Code>

      <Variant>
        <Size>1</Size>
        <Color>BIANCO</Color>
      </Variant>
      <Variant>
        <Size>2</Size>
        <Color>BIANCO</Color>
      </Variant>
      <Variant>
        <Size>3</Size>
        <Color>BIANCO</Color>
      </Variant>
    </Product>
</Products>
    <Product>
      <Code>20</Code>
      <Variant>
        <Size>1</Size>
        <Color>NERO</Color>
      </Variant>
      <Variant>
        <Size>2</Size>
        <Color>BIANCO</Color>
      </Variant>
      <Variant>
        <Size>3</Size>
        <Color>NERO</Color>
      </Variant>
    </Product>

 

I tried this code but inserts the data into the database twice.

Here is the php code I used:

//prelevo il file
$xml = simplexml_load_file("articoli.xml");

//eseguo il ciclo su tutti i prodotti per prelevare il codice dei prodotti
  foreach($xml->Products->Product as $product)
    {
     $codice = $product->Code;

//eseguo il ciclo sul nodo più basso "Variant" di ogni prodotto
      foreach($xml->xpath("Products/Product[Code = '".(string)$product->Code."']/Variant") as $variant)
    {
//prelevo i dati taglia e colori
     $taglia = $variant->Size;
     $colore = $variant->Color;

//prelevo l'id della taglia
$sql = "SELECT * FROM products_options_values WHERE products_options_values_name = '$taglia'";
$rows = $db->query($sql);
  while ($r = $db->fetch_array($rows)){
      $p_att= $r[products_options_values_id];
}

//prelevo l'id del prodotto
$sql = "SELECT * FROM products WHERE products_model = '$codice'";
$rows = $db->query($sql);
  while ($r = $db->fetch_array($rows)){
      $pid= $r[products_id];
}

//inserisco i dati nel database
$query = "INSERT INTO products_attributes (products_id, options_id,  options_values_id) VALUES ('$pid', '1', '$p_att')";
$insert = mysql_query($query);
   if(!$insert) die(mysql_error());



}
    
}  

 

How can I fix?

Thanks :'(

Link to comment
https://forums.phpfreaks.com/topic/194073-how-to-read-xml/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.