Hi I am new to PHP but have managed to get this far in my requirements:
I am trying to display only one part of a JSON decoded Object. I have called the object $Results
.
I can successfully use var_dump ($Results);
and then get the full results as follows:
object(stdClass)[2]
public '0' =>
object(stdClass)[3]
public 'forename_1' => string 'JAMES' (length=5)
public 'middle1_1' => string '' (length=0)
public 'middle2_1' => string '' (length=0)
public 'middle3_1' => string '' (length=0)
public 'surname_1' => string 'TURNER' (length=7)
public 'Status' => int 100
I then insert this into a table using the following code:
<html>
<form id="client-details" action="/details.php" method="post">
<table>
<thead>
<tr>
<th>First Name</th>
<th>Surname</th>
<th>Search</th>
</tr>
</thead>
<?php foreach($Results as $o):?>
<tr>
<td id="forename"><?= $o->forename_1 ?></td>
<td id="surname"><?= $o->surname_1 ?></td>
<td><button type="submit" >More Info</button></td>
</tr>
<?php endforeach; ?>
</table></form>
</html>
Heres the Problem When I display the results I get the following error: "Notice: Trying to get property of non-object.."
This seems to be because I am trying to run the public 'Status' => int 100
part of the object.
So my question is: How do I either stop the table from trying to populate that 'status' or how do I ignore it completely?
EDIT:
If I wanted to I could get the results from the json_decode
as an associative array instead of as objects... would this help me to ignore the 'status' array/object?
I think you got it wrong. What you are doing is iterate throught all variables of the object, i.e. firstly you get the public variable 0 (which is also an object) and in teh second run of the statement foreach you get the variable Status and because the value of 'Status' is int and has no property named 'forename_1' and so on you get the error that the property doesnt exist.
If you really want this to work you have to change the structure of the JSON object so you can iterate throught the list of people you want to display, like:
object(stdClass)[2]
public 'list' =>
array(0 =>
object(stdClass)[3]
public 'forename_1' => string 'JAMES' (length=5)
public 'middle1_1' => string '' (length=0)
public 'middle2_1' => string '' (length=0)
public 'middle3_1' => string '' (length=0)
public 'surname_1' => string 'TURNER' (length=7)
public 'Status' => int 100,
1 =>
object(stdClass)[3]
public 'forename_1' => string 'JAMES' (length=5)
public 'middle1_1' => string '' (length=0)
public 'middle2_1' => string '' (length=0)
public 'middle3_1' => string '' (length=0)
public 'surname_1' => string 'TURNER' (length=7)
public 'Status' => int 100,
2 =>
object(stdClass)[3]
public 'forename_1' => string 'JAMES' (length=5)
public 'middle1_1' => string '' (length=0)
public 'middle2_1' => string '' (length=0)
public 'middle3_1' => string '' (length=0)
public 'surname_1' => string 'TURNER' (length=7)
public 'Status' => int 100
)
EDIT:
if you are unable or dont want to change the data structure then get the result of the function call json_decode as an associative array and in the foreach statement you then check if the required field exists:
$Result = json_decode($data, true);
<?php foreach($Results as $o):?>
<?php if(isset($o['forename_1']) && isset($o['surname_1'])): ?>
<tr>
<td id="forename"><?= $o['forename_1'] ?></td>
<td id="surname"><?= $o['surname_1'] ?></td>
<td><button type="submit" >More Info</button></td>
</tr>
<?php endif; ?>
<?php endforeach; ?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With