dqu92800 2017-02-16 18:54
浏览 30
已采纳

在XML文档中查找属性并在PHP中输出子项

I have included my PHP code with a snippet of the XML file I am retrieving data from.

I want to display the data from the XML file similar to how I used it as an ID. Within the ID it uses the ID as an index however I see that it won't work as a date.

My first idea was to just use xPath and leave it as this but I would like to format my data similar for consistency.

As a beginner with PHP and XML any insight or documentation that could further my studying with either the language or the format style would be appreciated.

The problem I am having is to find the index value of the relevant attribute that has been inputted and outputting it individually in a format such as the ID Search.

<?php
if($_GET["SearchChoice"] == "ID Search"){ #WORKING FINDS THE VALUE OF DATA NEEDED
    $xml=simplexml_load_file("XMLtest.xml") or die("Error: Cannot create object");
    $passID = intval($_GET["IDsend"]);
    if ($passID !== 0){
        $passID = $passID - 1;
    }
    #echo $xml['date'];
    #echo var_dump((string)$xml['date']);
    $attr = $xml->finances[$passID]->attributes();
    echo "Date: ";
    echo $attr['date'];
    echo $xml->finances[$passID]->day . ",<br>Projection: ";
    echo $xml->finances[$passID]->projection . ",<br>Recommended Staff: ";
    echo $xml->finances[$passID]->recommendedStaff . ",<br>Staff Wages: ";
    echo $xml->finances[$passID]->staffWages . ",<br>Actual: ";
    echo $xml->finances[$passID]->actual . "<br>";
}
elseif($_GET["SearchChoice"] == "Date Search") { 
    $xml=simplexml_load_file("XMLtest.xml") or die("Error: Cannot create object");
    $passDate = strval($_GET["DateSend"]);
    $nodes = $xml->xpath("/january/finances[@date='". $passDate . "']"); # Instead of xpath possiblity: loop through to find date then output it similar to ID Search?
    print_r($nodes);
}
else {
    echo "Error 1 <br>";
    echo $_GET["SearchChoice"];
}

Here is a portion of the XML:

<?xml version="1.0" encoding="UTF-8"?>
<january>
    <finances id="1" date="01-01-2016">
        <projection>414</projection>
        <recommendedStaff>20</recommendedStaff>
        <staffWages>100</staffWages>
        <actual>250</actual>
    </finances>
    <finances id="2" date="02-01-2016">
        <year>2016</year>
        <month>01</month>
        <day>02</day>
        <projection>124</projection>
        <recommendedStaff>8</recommendedStaff>
        <staffWages>150</staffWages>
        <actual>250</actual>
    </finances>
    <finances id="3" date="03-01-2016">
        <projection>687</projection>
        <recommendedStaff>20</recommendedStaff>
        <staffWages>150</staffWages>
        <actual>250</actual>
    </finances>
    <finances id="4" date="04-01-2016">
        <projection>587</projection>
        <recommendedStaff>15</recommendedStaff>
        <staffWages>150</staffWages>
        <actual>250</actual>
    </finances>
    <finances id="5" date="05-01-2016">
        <projection>124</projection>
        <recommendedStaff>5</recommendedStaff>
        <staffWages>150</staffWages>
        <actual>250</actual>
    </finances>
    <finances id="6" date="06-01-2016">
        <projection>874</projection>
        <recommendedStaff>22</recommendedStaff>
        <staffWages>150</staffWages>
        <actual>250</actual>
    </finances>
</january>
  • 写回答

1条回答 默认 最新

  • doubeng3412 2017-02-16 20:01
    关注
    $xml=simplexml_load_file("XMLtest.xml") or die("Error: Cannot create object");
    
    switch ($_GET["SearchChoice"])
    {
      case 'ID Search':
        $nodes = $xml->finances[$passID];
        break;
      case 'Date Search':
         $passDate = strval($_GET["DateSend"]);
         $nodes = $xml->xpath("/january/finances[@date='". $passDate . "']");
         if  ($nodes)
             $nodes = array_shift($nodes);   //  First match
         break;
      default:
        $nodes = NULL;
    }
    
    if  ($nodes)  //  Don't care how we got them
    {
        echo $nodes->day . ",<br>Projection: ";
        //  etc
    }
    

    This loads the file at the start and then creates a node (stored in $nodes) based on your search choice (familiarize yourself with switch statements - very useful, especially the "break" command when it's used or not used).

    After the switch statement, you have a node regardless of how you got it. That's good practice in coding - the condition is "taken care of" and from here on out is not an issue.

    In actuality, it might be a better idea to just use xpath for both options - minimal difference between the different code paths makes for easier debugging and maintenance.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看