首页 > 解决方案 > 如何将 html 下拉菜单链接到 php xpath 查询到 xml 文件?

问题描述

我有以下 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"  ;

    }


    ?>

标签: phpxmlxsltxpath

解决方案


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 . "')   ]  ");

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

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){

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

echo 'Asking Price:',$elements->numeric_price."  / " ;"\n";
echo 'Tenure:', $elements->tenure." / "; "\n";
echo 'Address:'." ", $elements->advert_heading; "\n";

推荐阅读