James Paul
James Paul

Reputation: 167

For loop in viewing xml output php

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

Upvotes: 0

Views: 37

Answers (1)

Syscall
Syscall

Reputation: 19780

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;
        }
    }
}

Upvotes: 2

Related Questions