简体   繁体   中英

How to import XML feed with multiple images

I have this xml feed ...

<products>
  <product>
    <title>Mini Piscina</title>
    <description>Mini piscina penru copii pana la 6 ani.</description>
    <price>22.00</price>
    <images>
      <image1>produs_1402038969.jpg</image1>
      <image2>produs_1402382460.jpg</image2>
    </images>
  </product>
</products>

... and this part of script ...

$xml=simplexml_load_file("../feed/feed") or die("Error: Cannot create object");
$partener = 'toys';

foreach($xml -> product as $row) {
    $magazin = $partener;
    $titlu = $row -> title;
    $descriere = $row -> description;
    $pret = $row -> price;
    $imagine1 = $row -> images1;
    $imagine2 = $row -> images2;

    $sql = "INSERT INTO toys (magazin,titlu,descriere,pret,imagine1,imagine2)
            VALUES ('$magazin','$titlu','$descriere','$pret','$imagine1','$imagine2') ";
        mysqli_query($conn, $sql);
}

... the result is: title, descrition and price are imported but image1 and images2 can not be imported. Why? I'm a beginner. Please, can anybody help me?

The two images are not the children of product but its grandchildren. Simply, add the parent images-> to the path to obtain their values:

foreach($xml -> product as $row) {
    $magazin = $partener;
    $titlu = $row -> title;
    $descriere = $row -> description;
    $pret = $row -> price;

    $imagine1 = $row->images->image1;
    $imagine2 = $row->images->image2;

    $sql = "INSERT INTO toys (magazin,titlu,descriere,pret,imagine1,imagine2)
            VALUES ('$magazin','$titlu','$descriere','$pret','$imagine1','$imagine2') ";
    mysqli_query($conn, $sql);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM