【问题标题】:Pagination Filtered XML file分页过滤的 XML 文件
【发布时间】:2012-11-22 09:02:09
【问题描述】:

我想从 xml 文件中对以下过滤结果进行分页:

<?php

//load up the XML file as the variable $xml (which is now an array)

$xml = simplexml_load_file('inventory.xml');

//create the function xmlfilter, and tell it which arguments it will be handling

function xmlfilter ($xml, $color, $weight, $maxprice)
{
    $res = array();
    foreach ($xml->widget as $w)
    {
        //initially keep all elements in the array by setting keep to 1
        $keep = 1;
        //now start checking to see if these variables have been set
        if ($color!='')
        {
        //if color has been set, and the element's color does not match, don't keep this element
            if ((string)$w->color != $color) $keep = 0;
        }
        //if the max weight has been set, ensure the elements weight is less, or don't keep this element
        if ($weight)
        {
            if ((int)$w->weight > $weight) $keep = 0;
        }
        //same goes for max price
        if ($maxprice)
        {
            if ((int)$w->price > $maxprice) $keep = 0;
        }
        if ($keep) $res[] = $w;
    }
    return $res;
}

//check to see if the form was submitted (the url will have '?sub=Submit' at the end)
if (isset($_GET['sub']))
{
    //$color will equal whatever value was chosen in the form (url will show '?color=Blue')
    $color = isset($_GET['color'])? $_GET['color'] : '';
    //same goes for these fellas
    $weight = $_GET['weight'];
    $price = $_GET['price'];

    //now pass all the variables through the filter and create a new array called $filtered
    $filtered = xmlfilter($xml ,$color, $weight, $price);
    //finally, echo out each element from $filtered, along with its properties in neat little spans
    foreach ($filtered as $widget) {
        echo "<div class='widget'>";
        echo "<span class='name'>" . $widget->name . "</span>";
        echo "<span class='color'>" . $widget->color . "</span>";
        echo "<span class='weight'>" . $widget->weight . "</span>";
        echo "<span class='price'>" . $widget->price . "</span>";
        echo "</div>";
    }
}

其中$xml-&gt;widget代表以下xml:

<hotels xmlns="">
    <hotels>
        <hotel>
            <noofrooms>10</noofrooms>
            <website></website>
            <imageref>oias-sunset-2.jpg|villas-agios-nikolaos-1.jpg|villas-agios-nikolaos-24​.jpg|villas-agios-nikolaos-41.jpg</imageref>
            <descr>blah blah blah</descr>
            <hotelid>119</hotelid>
        </hotel>
    </hotels>
</hotels>

有什么好主意吗?

【问题讨论】:

  • $xml-&gt;widget 只是一个数字索引数组吗?
  • widget is hotel .......10oias -sunset-2.jpg|villas-agios-nikolaos-1.jpg|villas-agios-nikolaos-24.jpg|villas-agios-nikolaos-41.jpgblah blah blah119
  • 我已经格式化了您的 PHP 代码并添加了您的 XML 示例。如果您认为我歪曲了您的意图,请编辑您的问题。

标签: php xml pagination


【解决方案1】:

老实说,如果您已经在使用 XML 并且想要进行分页,那么请使用 XSL。它将允许对结果进行格式化并轻松进行分页。 PHP 有一个内置的 XSL 转换器 iirc

请参阅http://www.codeproject.com/Articles/11277/Pagination-using-XSL 了解一个不错的示例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-03
    • 1970-01-01
    • 1970-01-01
    • 2015-03-14
    • 2011-05-05
    • 2018-01-13
    相关资源
    最近更新 更多