Jump to content

echo data from array converted from xml


Plugnz13

Recommended Posts

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.

Link to comment
Share on other sites

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";

image.png.6a4eea8c9af09d03e4b1a6e1ee23f275.png

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.