如何使用php对XML价格进行排序

时间:2017-09-11 04:57:48

标签: php xml sorting

我是学习如何使用XML的新手,我知道一些编程语言。我已经尝试过搜索这个答案,但我感到非常困惑,无法回答我将如何在我的代码中实现这一点。目前我希望能够对价格进行分类

这是一个当前看起来如何的例子

Here an example of how it currently looks

<?php
    $xmlFile = "hotel.xml";
    $HTML = "";
    $count = 0;
    $dom = DOMDocument::load($xmlFile);
    $hotel = $dom->getElementsByTagName("hotel"); 

foreach($hotel as $node) 
{ 
    $city = $node->getElementsByTagName("City");
    $city = $city->item(0)->nodeValue;

    $type = $node->getElementsByTagName("Type");
    $type = $type->item(0)->nodeValue;

    $name = $node->getElementsByTagName("Name");
    $name = $name->item(0)->nodeValue;

    $price = $node->getElementsByTagName("Price");
    $price = $price->item(0)->nodeValue;

    if (($type == $_GET["type"]) && ($city == $_GET["city"]) )
    {
        $HTML = $HTML."<br><span>Hotel: ".$name."</span><br><span>Price: ".$price."</span><br>";
        $count++;
    }
} 

if ($count ==0)
{
    $HTML ="<br><span>No hotels available</span>";
}

echo $HTML;   
?>

0 个答案:

没有答案