简体   繁体   中英

Find an attribute in an XML document and output children in 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>
$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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM