【问题标题】:How do i link a html drop down menu to php xpath query to xml file?如何将 html 下拉菜单链接到 php xpath 查询到 xml 文件?
【发布时间】:2019-02-14 21:32:02
【问题描述】:

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

    }


    ?>

【问题讨论】:

  • 你不想从xml文件中找到每个property标签都有numeric_price属性的特殊值吗?
  • 人们何时会停止使用可怕的简单 xml?使用 DOM 类!
  • 有趣的评论 delboy。我可以使用 dom 来显示我需要的内容。这不能回答我的问题。有没有机会解决如何将网页链接到我的 xml 文件,以便用户可以根据 2 个条件查询它?

标签: php xml xslt xpath


【解决方案1】:
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";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多