Plugnz13 0 Posted April 6 Share Posted April 6 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! Quote Link to post Share on other sites
Barand 1,650 Posted April 6 Share Posted April 6 You are doing better than I did. All I got was errors from your posted XML. Quote Link to post Share on other sites
Plugnz13 0 Posted April 6 Author Share Posted April 6 Sorry, Barand, I stripped the XML so there wasn't so much detail in the post. I'll repost tonight. Quote Link to post Share on other sites
MadTechie 1 Posted Friday at 11:50 PM Share Posted Friday at 11:50 PM (edited) 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 Friday at 11:50 PM by MadTechie Quote Link to post Share on other sites
MadTechie 1 Posted Friday at 11:52 PM Share Posted Friday at 11:52 PM Oh you may want to check if the key is set as well, the code above was just to give you the general idea Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.