0

I am using an API to return json that I will use in my application to display data in a html table:

$response = $api->listusers();
$data = json_decode($response, true);
echo '<table>';
foreach($data as $result){
    //print_r($result);
    echo '<tr>';
    echo '<td>'.$result->name.'</td>';
    echo '<td>'.$result->id_user.'</td>';
    echo '<td>'.$result->backup.'</td>';
    echo '<td>'.$result->status.'</td>';
    echo '</tr>';
}
echo '</table>';

However when I try to Loop Through it I get a blank page the only code that works is if I try to display variables with print_rand the result is an array:

  OKArray ( 
    [0] => Array ( [name] => James [id_user] => 3 [backup] => on [status] => active ) 
    [1] => Array ( [name] => James [id_user] => 3 [backup] => on [status] => active )
    )

How can I create a html table from this data?

Godwin Kimani
  • 55
  • 1
  • 12
  • 1
    if the 2nd parameter of [json_decode](https://www.php.net/json_decode) is **true**, an associative array is returned. So you should access properties using `$result['name']`. – Syscall Jan 21 '22 at 15:35
  • If you see an empty page, it usually means there was an error, which is probably logged somewhere. See https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/12772851#12772851 Find that error, and it will give you a hint of what to do. – IMSoP Jan 21 '22 at 15:39
  • this answered my question - https://stackoverflow.com/questions/9869150/illegal-string-offset-warning-php – Godwin Kimani Jan 21 '22 at 16:11

0 Answers0