Jump to content

Accessing Child Node


seblondres

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 comment
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 comment
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 comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...