[英]For loop in viewing xml output php
I am trying to display XML values in php output. 我正在尝试在php输出中显示XML值。 Everything works as expected except image below is the code what I am doing for achieve the output in PHP
一切都按预期工作,除了下面的图像是我为实现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. 我没有得到的是,我只需要每个
<link type="largephoto-link">
大图像<link type="largephoto-link">
,但是我得到的只是一些图像。 I need the large images alone displayed. 我只需要显示大图像。 in one
<td>
and small need to come in other <td>
. 一个
<td>
中的一个,很少需要进入另一个<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 这是我正在使用的XML http://api.pafilia.com/feed/datafeed?resales=1&lang=EN&limit=5000
You could use ['type']
to compare the attribute : 您可以使用
['type']
比较属性:
foreach($properties->media->mediagroup->mediaitem as $mediagroup) {
foreach ($mediagroup->link as $link) {
if ((string)$link['type'] == 'largephoto-link') {
echo (string) $link;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.