SCREENSHOTIn this case, i am getting the first image of every Article, but only the same price for all Articles, see the screenshot. i want to display every product image with the relevant price under. i am using array unique to remove images duplications, which i believe that it is wrong. i am getting data from xml.
<PRODUCTDATA>
<PRODUCT>
<P_NAME>
<VALUE>Lether Jacket</VALUE>
</P_NAME>
<ARTICLEDATA>
<ARTICLE>
<A_PRICEDATA>
<A_PRICE>
<A_VK>34.95</A_VK>
</A_PRICE>
</A_PRICEDATA>
<A_MEDIADATA>
<A_MEDIA>https://1.jpg</A_MEDIA>
<A_MEDIA>https://2.jpg</A_MEDIA>
<A_MEDIA>https://3.jpg</A_MEDIA>
<A_MEDIA>https://4.jpg</A_MEDIA>
</A_MEDIADATA>
</ARTICLE>
<ARTICLE>
<A_PRICEDATA>
<A_PRICE>
<A_VK>40.95</A_VK>
</A_PRICE>
</A_PRICEDATA>
<A_MEDIADATA>
<A_MEDIA>https://11.jpg</A_MEDIA>
<A_MEDIA>https://22.jpg</A_MEDIA>
<A_MEDIA>https://33.jpg</A_MEDIA>
<A_MEDIA>https://44.jpg</A_MEDIA>
</A_MEDIADATA>
</ARTICLE>
</ARTICLEDATA>
</PRODUCT>
<PRODUCT>
<P_NAME>
<VALUE>Jeans</VALUE>
</P_NAME>
<ARTICLEDATA>
<ARTICLE>
<A_PRICEDATA>
<A_PRICE>
<A_VK>29.95</A_VK>
</A_PRICE>
</A_PRICEDATA>
<A_MEDIADATA>
<A_MEDIA>https://111.jpg</A_MEDIA>
<A_MEDIA>https://222.jpg</A_MEDIA>
<A_MEDIA>https://333.jpg</A_MEDIA>
<A_MEDIA>https://444.jpg</A_MEDIA>
</A_MEDIADATA>
</ARTICLE>
<ARTICLE>
<A_PRICEDATA>
<A_PRICE>
<A_VK>19.95</A_VK>
</A_PRICE>
</A_PRICEDATA>
<A_MEDIADATA>
<A_MEDIA>https://1111.jpg</A_MEDIA>
<A_MEDIA>https://2222.jpg</A_MEDIA>
<A_MEDIA>https://3333.jpg</A_MEDIA>
<A_MEDIA>https://4444.jpg</A_MEDIA>
</A_MEDIADATA>
</ARTICLE>
</ARTICLEDATA>
</PRODUCT>
</PRODUCTDATA>
$xml = simplexml_load_file($datei);
foreach($xml->PRODUCTDATA->PRODUCT as $p)
{
$prod = (string) $p->P_NAME->VALUE;
$articles = "";
foreach ($p->ARTICLEDATA->ARTICLE as $article)
{
$price = (string)$article->A_PRICEDATA->A_PRICE->A_VK;
$firstImage = (string)$article->A_MEDIADATA->A_MEDIA;
$articles ;
}
}
$articles .= '<div ><img src="' . $firstImage . '" style="width: 120px margin:5px;"><div ><h3 >' . $prod . '</h3><span >€' . $price . ' EUR</span></div></div>';
as $p is defined before as a Parent element in XMl as Product with many Articles inside. The Variable $articlesss will then be inserted in a php long string that contain the full HTML page, so this is why i am trying to get them together in this variable.
CodePudding user response:
You don't have to keep track with counters and loops to tell if it is the first (index 0) entry.
Assuming all fields for the articles are present, you can use SimpleXML, loop all the article entries, and take the price (as there is only a single price) and first image per article using the index 0.
Then per entry, you can append the string with the variables to $articles
Note that $produkt
is not present in the example code, so I have added a hardcoded value just for the example.
$articles = "";
foreach ($xml->ARTICLEDATA->ARTICLE as $article) {
$price = (string)$article->A_PRICEDATA->A_PRICE->A_VK;
$firstImage = (string)$article->A_MEDIADATA->A_MEDIA[0];
$produkt = "Product 1";
$articles .= '<div ><img src="' . $firstImage . '" style="width: 120px margin:5px;"><div ><h3 >' . $produkt . '</h3><span >€' . $price . ' EUR</span></div></div>';
}
echo $articles;
See a PHP demo
CodePudding user response:
$m=0;
foreach($p->ARTICLEDATA->ARTICLE as $articless)
{
if($m==0)
{
$price = (string) $articless->A_PRICEDATA->A_PRICE->A_VK;
$x=0;
foreach($articless->A_MEDIADATA as $bild)
{
$firstImage = (string) $bild->A_MEDIA[2];
$articles .= '<div ><img src="' . $firstImage . '" style="width: 120px margin:5px;"><div ><h3 >' . $prod . '</h3><span >€' . $price . ' EUR</span></div></div>';
}
}
$m ;
}