Jump to content

Accessing Child Node


Recommended Posts

Hi,

 

I have this code below to retrieve the products from the XML further down, I can get to any children but the Merchant logo URL. If someone could please let me know how to reach the Merchant logo URL.

foreach ($productsResult->Products->Product as $result) {

    echo "<a href=".$result->Offer->Url." target='_blank'><img class='productImg' src=".$result->Offer->Images[0]->Image->Url."/></a>"; 
    echo "<p><b class='price'>£".$result->Offer->Price->Price."</b></p><br>";
    echo "<p><img src=".$result->Offer->Merchant[0]->Name->Logo->Url."/></p>"; // This is where I need help
    echo "<p class='title'>".$result->Offer->Title.".</p><a class='goToStore w3-btn w3-blue' href=".$result->Offer->Url." target='_blank'><b>Visit Store</b></a>";
<Products>
 <Product>	
  <Offer>
   <Title></Title>
   <Description> </Description>
   <Images>
    <Image available="true">
    <Url></Url>
    <Height></Height>
    <Width></Width>
    </Image>
   <Merchant>
    <Name></Name>
    <Logo>
     <Url>The URL I need</Url>
     <Width></Width>
     <Height></Height>
     </Logo>
     </Merchant>

Thanks,

Link to post
Share on other sites

The probable problem is you are not properly separating html text string, from php code from the foreach loop, and therefore it produces unreadable results for image, and ALSO 'Logo' is not the child of 'Name' so it will never find that image.url.

Link to post
Share on other sites

The partial xml you have provided, does not tally for what the php foreach should be, OR child images, or merchants it should target.

 

To me it should be foreach($productsResult->Product as $result)

 

If you use this code

 foreach ($productsResult->Product as $result) {

/* Original */ echo '<h3>Original - html as text</h3>';
    echo htmlspecialchars("<a href=" . $result->Offer->Url . " target='_blank'><img class='productImg' src=" . $result->Offer->Images->Image->Url . "/></a>") . "<br>";
    echo htmlspecialchars("<p><b class='price'>£" . $result->Offer->Price->Price . "</b></p><br>") . "<br>";
    echo htmlspecialchars("<p><img src=" . $result->Offer->Logo->Url . "/></p>") . "<br>"; // This is where I need help
    echo htmlspecialchars("<p class='title'>" . $result->Offer->Title . ".</p><a class='goToStore w3-btn w3-blue' href=" . $result->Offer->Url . " target='_blank'><b>Visit Store</b></a>") . "<br>";

echo '<h3>Original</h3>';
    echo "<a href=".$result->Offer->Url." target='_blank'><img class='productImg' src=".$result->Offer->Images->Image->Url."/></a>"; 
    echo "<p><b class='price'>£".$result->Offer->Price->Price."</b></p><br>";
    echo "<p><img src=".$result->Offer->Logo->Url."/></p>"; // This is where I need help
    echo "<p class='title'>".$result->Offer->Title.".</p><a class='goToStore w3-btn w3-blue' href='".$result->Offer->Url."' target='_blank'><b>Visit Store</b></a>";
}

You will see it work, but! not for the logo image, this is because the escaping from html text and php is so jumbled, it can't produce correct html img tag or URL.

Edited by dsonesuk
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...