开发者

displaying data of xml file

i want to display the cheapest 3 pairs from pairs.xml file , now i am confused how to use a condition for it .can anybody help me开发者_开发百科 out

pairs.xml------------

<pairs>
    <pair>
        <name>cups</name>
        <price>50</price>
    </pair>
    <pair>
        <name>mugs</name>
        <price>60</price>
    </pair>
    <pair>
        <name>plates</name>
        <price>40</price>
    </pair>
    <pair>
        <name>spoons</name>
        <price>10</price>
    </pair>
</pairs>

pairs.php----------

$xmlFile = "pairs.xml";
$doc = DOMDocument::load($xmlFile);
$pair = $doc->getElementsByTagName("pair");
echo "<table border=1><tr><th>Name</th><th>Price</th></tr>";
foreach ($pair as $node) {
    $name = $node->getElementsByTagName("name");
    $name = $name->item(0)->nodeValue;
    $price = $node->getElementsByTagName("price");
    $price = $price->item(0)->nodeValue;

    if ()
        echo "<tr><td>{$name}</td><td>{$price}</td><tr>";
}  


$pairs = array();
foreach($pair as $node)
{
    $name = $node->getElementsByTagName("name")->item(0)->nodeValue;
    $price = $node->getElementsByTagName("price")->item(0)->nodeValue;
    $pairs[$price] = $name;
}
ksort($pairs, SORT_NUMERIC);
$i = 0;
foreach($pairs AS $p => $n)
{
     echo("<tr><td>{$n}</td><td>{$p}</td></tr>");
     $i++;
     if( $i == 3 )
          break;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜