Jump to content

Retrieving data from xml file - using arrays


Recommended Posts

Hi There all, having an issue with simple xml... hopefully someone will be able to shed some light on this. 

I have this data in an xml feed from an API: 

<ID>job no 1 </ID>
    <Name>job name</Name>
    <State>job status</State>    
    <Tasks>
      <Task>
         <Name>task name 1</Name>
      </Task>
      <Task>
          <Name>task name 2</Name>       
                </Task>
              
<ID>job no 2</ID>
     <Name>job name</Name>
     <State>job status</State>    
     <Tasks>
        <Task>
           <Name>task name 1</Name>
        </Task>
        <Task>
           <Name>task name 2</Name>       
         </Task>

I would like to display these in a table pretty much as it is shown above.

Job no 1     job name    job status

                             job 1 task name 1

                            job 1 task name 2

Job no 2     job name    job status

                             job 2 task name 1

                            job 2 task name 2

etc

my code looks like this : $required is narrowing down specific states not shown for clarity 

 $xml_current=simplexml_load_string($jobs_task_response) or die("Error: Cannot create object");
     
    foreach($xml_current->Jobs->Job as $item_current) {
        if (in_array((string)$item_current->State, $required)) {     
            $projects_current[] = array(
                'job_no'          => (string)$item_current->ID,
                'job_name'        => (string)$item_current->Name,
                'job_status'      => (string)$item_current->State,
            );
            foreach($item_current->Tasks->Task as $current_tasks){
                $projects_task[] = array(
                'job_tasks'       => (string)$current_tasks->Name,
                );
            }
        }         
    } 

 
    foreach ($projects_current as $proj_current) {
        $job_no =$proj_current['job_no'];
        $job_name =$proj_current['job_name'];
        $job_status =$proj_current['job_status'];       
        $clr_current = $colors[$job_status];
        $project_id = $job_no;  

        $tdata_1 .= "<tr id='current' class='card-body collapse-show'>";
        $tdata_1 .= "<td class='th-sm-1 bg-white '><a href='../details/index.php?pid=$job_no' class='text-left ml-1'>$job_no</a></td>";
        $tdata_1 .= "<td data-target='#" . $job_no . "' data-toggle='collapse' class='th-sm-2 bg-white text-left ml-1'>$job_name</td>"; 
        $tdata_1 .= "<td class='th-sm-2 " . $clr_current . " text-left ml-1 '>$job_status</td>";
      
        foreach ($projects_task as $proj_tasks){
        $job_tasks =$proj_tasks['job_tasks']; 
        $tdata_1 .= "</tr>";
        $tdata_1 .= "<tr id='" . $job_no . "' class='card-body collapse'> ";
        $tdata_1 .= "<td class='th-sm bg-white text-left ml-1'></td>";
        $tdata_1 .= "<td class='th-sm bg-white text-left ml-1'></td>";
        $tdata_1 .= "<td class='th-sm " . $clr_current . " text-left ml-1'>$job_tasks</td>";
        $tdata_1 .= "</tr>";
        }

    }

 what I get with this code however is;

Job no 1     job name    job status

                             job 1 task name 1

                            job 1 task name 2

                            job 2 task name 1 

                            job 2 task name 2

 

Job no 2    job name   job status

                          job 1 task name 1

                          job 1 task name 2

                          job 2 task name 1

                          job 2 task name 2       

 

I'm sure there is a simple answer to this, I just can't seem to put my finger on it. 

Can anyone help please? Much appreciated thanks in advance!

Link to post
Share on other sites

I have had a quick read, and it looks like your problem is because of the way you are building $projects_task array 

here

            foreach($item_current->Tasks->Task as $current_tasks){
                $projects_task[] = array( //<-- HERE your array will just build up
                'job_tasks'       => (string)$current_tasks->Name,
                );

The array just added items on each loop, so you can't tell what iteration the item was added.

Try something like adding a key to separate the iteration like this

Set array

$job_no = (string)$item_current->ID; //added 
foreach($item_current->Tasks->Task as $current_tasks){
                $projects_task[$job_no] = array( //updated : Add a key
                'job_tasks'       => (string)$current_tasks->Name,
                );no =

 

Display array

$job_no = (string)$item_current->ID; //added (for illustration) 
foreach ($projects_task[$job_no] as $proj_tasks){ //added $job_no key
        $job_tasks =$proj_tasks['job_tasks']; 
        $tdata_1 .= "</tr>";
        $tdata_1 .= "<tr id='" . $job_no . "' class='card-body collapse'> ";
        $tdata_1 .= "<td class='th-sm bg-white text-left ml-1'></td>";
        $tdata_1 .= "<td class='th-sm bg-white text-left ml-1'></td>";
        $tdata_1 .= "<td class='th-sm " . $clr_current . " text-left ml-1'>$job_tasks</td>";
        $tdata_1 .= "</tr>";
        }

 

This is completely untested but i hope it make sense.

Edited by MadTechie
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.