Jump to content
Plugnz13

Selecting XML object by node in php

Recommended Posts

Hi All,

I've been struggling to get the following code to work. I have an API feed that looks like this:

 

<Jobs>
        <Job>
            <ID>1805</ID>
            <Name>House Additions/Alterations </Name>
            <Description>House</Description>
            <Client>
                <ID>16178470</ID>
                <Name>client name 1</Name>
            </Client>
            <ClientOrderNumber></ClientOrderNumber>
            <State>In Construction</State>
            <StartDate>2018-03-26T00:00:00</StartDate>
            <DueDate>2020-03-31T00:00:00</DueDate>
            <Contact>
                <ID>10195532</ID>
                <Name>Clent Name1</Name>
            </Contact>
            <InternalID>25026686</InternalID>
            <Manager>
                <ID>610781</ID>
                <Name>Manager Name 1</Name>
            </Manager>
            <Partner>
                <ID>610781</ID>
                <Name>patner name</Name>
            </Partner>
            <Assigned>
                <Staff>
                    <ID>610781</ID>
                    <Name>Staff 1</Name>
                </Staff>
              .........
              

I'm trying to work out how to show all the job ID, Job Name, Client Name, Start Date etc that are specific to the assigned staff member by selecting a specific staff member. $Staff is populated by a dropdown earlier in the code.

here's my code so far:

if ($err) {
  echo "cURL Error #:" . $err;
} else {
        $xml=simplexml_load_string($response) or die("Error: Cannot create object"); 
    $jobs = $xml->xpath("Jobs/Job/Assigned/Staff[Name= '$Staff']");
    foreach($jobs as $item) {
    //foreach($xml->Jobs->Job as $item) {
        $item1 = $item->Client->Name;
        $job_no = "$item->ID";
        $job_name = "$item->Name";
        $job_client = "$item1";
        $job_start = "$item->StartDate";
        $job_due = "$item->DueDate";
        $job_status = "$item->State";
            
    echo "<tr><td>$job_no</td><td><a href='activecp/managejob.php?job_id=$job_no'>$job_name</a></td><td>$job_client</td><td>$job_status</td><td>$job_start</td><td>$job_due</td></tr>";
     } 
}

This currently shows the ID and name of the selected staff member but not the info i need. All of the data I want to show is before the Assigned/Staff . 

Any help would be much appreciated. 

Thanks in advance.

Share this post


Link to post
Share on other sites

According to the XML provided, assigned staff only have a id and name. Where are the other data coming from?

<Assigned>
    <Staff>
        <ID>610781</ID>
        <Name>Staff 1</Name>
    </Staff>

 

Share this post


Link to post
Share on other sites

Just a guess based on the XML but I think you need top process it based on job ID. Check the staff ID for that job and if it is what you want, use the associated data from that job ID.

Share this post


Link to post
Share on other sites
2 hours ago, Plugnz13 said:

$jobs = $xml->xpath(" Jobs / Job / Assigned / Staff [ Name = '$Staff' ] ");

Your XPath query is asking for "Staff" nodes - the lowest part of the path, before the square brackets - where the "Name" element within that equals the value stated. 

What you want is all the "Job" nodes where the Name element within the Staff element within the Assigned element equals the value stated, or, more succinctly: 

$jobs = $xml->xpath(" Jobs / Job [ Assigned / Staff / Name = '$Staff' ] ");

I think of the bits in brackets like SQL's "where" clauses.   

Regards,
   Phill  W.

 

Share this post


Link to post
Share on other sites
Posted (edited)

Thanks Phil,

 

That is a super easy fix! much appreciated!

Edited by Plugnz13

Share this post


Link to post
Share on other sites

Hi All, Sorry to be a pain, but further to the above query, which worked wonders by the way, i now have extended it to select the data based on three situations happening.

Basically I want to select the job name and id by the staff member that is assigned to a specific task but only if the state of the job equals that task. 

     echo "<div id='col_3' class='bg-white col-sm-1 border'>";
     
            $jobs_2 = $xml->xpath("Jobs/Job[State= 'Model Drawing' and Type= '$Category' or State= 'Model Drawing' and Tasks/Task/Assigned/Staff/Name= '$Staff' and Tasks/Task/Name= 'Model Drawing']");
            foreach($jobs_2 as $item) {

            $job_no = "$item->ID";
            $job_name = "$item->Name";
             
            echo "<button type='button' class='btn btn-outline-light waves-effect'><a href='activecp/managejob.php?job_id=$job_no'><strong>$job_no</strong><br> $job_name</a></button>";
            }

ignore the part before "or" as that's all good. So I'd like to show the job name where the Job State is"Model Drawing' but only if Staff "Member 1" is assigned to the task "Model Drawing". So it is working except that if Staff "Member2" is assigned to a later task (that doesn't match the state task) the job name also shows up on their page even though they are not assigned to that task. 

 

Hope that makes sense? 

 

Thanks again in advance.

Share this post


Link to post
Share on other sites

I haven't studied you code in detail but, in general when dealing with a mix of AND and OR, use parentheses to define the desired logic.

"AND" and "OR" usualy have different binding values.

EG :

A AND B OR C

Is that

A AND (B OR C)

or is it

(A AND B) OR C

(The latter would be the default, "AND" having a greater binding)

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.