0

I am trying to display XML values in php output. Everything works as expected except image below is the code what I am doing for achieve the output in PHP

    <table>
    <thead>
    <th>Property Ref No</th>
    <th>Property title</th>
    <th>Property type</th>
    <th>Property Description</th>
    <th>Bathroom</th>
    <th>Bedroom</th>
    <th>Price</th>
    <th>Property created on</th>
    <th>Property updated on</th>
    <th>Property Images</th>
    </thead>
<?php
$url    = 'http://api.pafilia.com/feed/datafeed?resales=1&lang=EN&limit=5000';
$html   = file_get_contents($url);
$invalid_characters = '/[^\x9\xa\x20-\xD7FF\xE000-\xFFFD]/';
$html = preg_replace($invalid_characters, '', $html);
$xml = simplexml_load_string($html);
/*
//test purpose part 
$encode = json_encode($xml);
$decode = json_decode($encode, true);
print_r($decode)
*/
foreach($xml->properties->property  as $properties)
{
?>
<tr>
<td> <?php  echo $properties->propertyref; ?></td>
<td><?php echo $properties->propertyname; ?></td>
<td> <?php  echo $properties->propertyType; ?></td>
<td> <?php  echo $properties->description .'<br/>'; 
echo "Features :".'<br/>';
echo $properties->features; ?>
</td>
<td> <?php  echo $properties->bathrooms; ?></td>
<td> <?php  echo $properties->bedrooms; ?></td>
<td> <?php  echo $properties->price; ?></td>
<td> <?php  echo $properties->propertyCreated; ?></td>
<td> <?php  echo $properties->propertyUpdated; ?></td>
<td> 
<?php
foreach($properties->media->mediagroup->mediaitem  as $mediagroup){
    echo $mediagroup->link;
    }
?>
</td>
</tr>
<?php   } ?>
<tbody>
</tbody>
</table> 

What I am not getting is I need only large images <link type="largephoto-link"> from every <link type="largephoto-link"> but what I am getting is some images. I need the large images alone displayed. in one <td> and small need to come in other <td>. Is there any way to achieve like that.

Here is the XML which I am working with http://api.pafilia.com/feed/datafeed?resales=1&lang=EN&limit=5000

1
  • What data exactly does the field $mediagroup contain? Provide an example please. Commented Mar 9, 2018 at 10:23

1 Answer 1

2

You could use ['type'] to compare the attribute :

foreach($properties->media->mediagroup->mediaitem  as $mediagroup) {
    foreach ($mediagroup->link as $link) {
        if ((string)$link['type'] == 'largephoto-link') {
            echo (string) $link;
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

if i call image outside for loop it only shows only one image and others are not shown please help me. Below is my code. foreach($properties->media->mediagroup->mediaitem as $mediagroup) { foreach ($mediagroup->link as $link) { if ((string)$link['type'] == 'largephoto-link') { $out_images="$link"."\n"; } } } echo " $out_images";
@JamesPaul Change this line $out_images.="$link"."\n"; (note the .= to concatenate).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.