如何将html下拉菜单链接到php xpath查询到xml文件?

时间:2018-09-10 12:35:35

标签: php xml xslt xpath

我有以下php / xpath查询。我似乎找不到逻辑将该脚本链接到带有搜索框的html页面/客户端网页,供用户选择要从xml文件中看到的内容。我当然可以通过搜索按钮选择下拉菜单,但是却很难将其链接起来。

下面的php代码搜索xml文件,并选择价格在10万到20万英镑之间的元素。

<div>
<label for="xsltsearch">Choose a Price:</label>


    <select id="xsltsearch">
      <option value="">Price To</option>
      <option value="">£100,000</option>
      <option value="">£200,000</option>
      <option value="">£300,000</option>
      <option value="">£400,000</option>
    </select>


    <select>
      <option value="">Price From</option>
      <option value="">£100,000</option>
      <option value="">£200,000</option>
      <option value="">£300,000</option>
      <option value="">£400,000</option>
    </select>
    <form method="post" action="example.xml">
      <input name="search" type="text" class="input" value="" /> 
       <input type="submit" class="submit" value="Search" />
    </form>
</html>


  <?php 

    $xml =  simplexml_load_file('xml_files/example.xml') or die("can not find it");

    $a = "100000.00";
    $b = "200000.00";



    $result = $xml->xpath("//property[(numeric_price> '" . $a . "'  ) and (numeric_price< '" . $b . "')     ]/numeric_price ");


       while(list( , $node) = each($result)) {

        echo  'Asking Price: ',$node,"\n"  ;

    }


    ?>

1 个答案:

答案 0 :(得分:0)

Note that this can however use significant memory

并根据需要对数据进行排序

Maybe this question was to simple to answer on here. 
I created 

In the drop down menu 1    
<select name="price"> 
In the drop down menu 2    
<select name="prices"> 

i then posted the form like you would any other php script and

Created a variable for the values of 'price' and 'prices' which insert the chosen user £values selected from the drop down menu. 

if(isset($_POST["submit"])){
$a = $_POST['price'];
$b = $_POST['prices'];

i then queried the xml file 

$result = $xml->property->xpath("//property[(numeric_price> '" . $a . "'  ) and (numeric_price< '" . $b . "')   ]  ");

然后,您可以回显出需要显示给用户的xml文件的元素。 foreach循环将选择与价格查询匹配的每个产品:

usort($result, function ($a, $b) { return (int) $a->numeric_price - (int) $b->numeric_price; });

//function to sort price high to low.
usort($result, function ($a, $b) { return (int) $b->numeric_price - (int) $a->numeric_price; });

foreach ($result as $elements){
相关问题