Plugnz13 Posted July 28, 2019 Share Posted July 28, 2019 Hi There, I'm feeling really dumb but i can't figure out how to get the data from an array that has been converted from xml so I can put it into a table. I've tried looping through based on simple array code but i just get array to string errors. my code looks like this $projects = array(); $xml=simplexml_load_string($response) or die("Error: Cannot create object"); foreach($xml->Jobs->Job as $item) { $projects[] = array( 'job_no' => (string)$item->ID, 'job_name' => (string)$item->Name, 'job_due' => (string)$item->DueDate, ); //array_sort_by_column($projects, 'job_due'); print_r($projects); print_r just dumps the whole lot on the page. Id like to grab each of those items and put them in a table cell then sort them by the due date etc. $job_no =""; $job_name =""; $job_due =""; //$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"; $date = new DateTime($job_due); $due_date = $date->format('d-m-Y'); echo "<tr>"; echo "<td><a href='managejob.php?job_id=$job_no'><strong>$job_no</strong></td>"; echo "<td>$job_name</a></td>"; echo "<td>$job_priority</td>"; echo "<td>$job_status_1</td>"; echo "<td>$job_status_2</td>"; echo "<td>$job_status_3</td>"; echo "<td>$job_status_4</td>"; echo "<td>$job_status_5</td>"; echo "<td>$job_status_6</td>"; echo "<td>$job_status_7</td>"; echo "<td>$due_date</td>"; echo "</tr>"; } } Any help on this would be much appreciated. Thanks in advance. Quote Link to comment Share on other sites More sharing options...
Barand Posted July 28, 2019 Share Posted July 28, 2019 With an array of data like the one you described $projects = [ [ 'job_no' => '101', 'job_name' => 'Job 101', 'job_due' => '2019-08-15' ], [ 'job_no' => '102', 'job_name' => 'Job 102', 'job_due' => '2019-07-31' ], [ 'job_no' => '103', 'job_name' => 'Job 103', 'job_due' => '2019-08-31' ], [ 'job_no' => '104', 'job_name' => 'Job 104', 'job_due' => '2019-07-29' ], [ 'job_no' => '105', 'job_name' => 'Job 105', 'job_due' => '2019-08-15' ] ]; First sort the array with a custom sort function comparing the due dates usort($projects, function($a,$b) { return $a['job_due'] <=> $b['job_due']; } ); then you can loop through the array elements outputting each in its own row echo "<table style='width:500px;' > <tr> <td>Job No</td><td>Name</td><td>Date Due</td> </tr>\n"; foreach ($projects as $proj) { $formatted = date('d/m/Y', strtotime($proj['job_due'])); echo "<tr><td>{$proj['job_no']}</td><td>{$proj['job_name']}</td><td>$formatted</td></tr>\n"; } echo "</table>\n"; Quote Link to comment Share on other sites More sharing options...
Plugnz13 Posted July 28, 2019 Author Share Posted July 28, 2019 That looks exactly like what I need. Thanks Barand! Much appreciated! Quote Link to comment Share on other sites More sharing options...
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.