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条回答 默认 最新

相关推荐 更多相似问题