I tried to convert json to text in PHP So my old question is closed and I didn't get answer.
this my code. Getting from Mysql:
<?php
$result_inventory = $con2->query("SELECT inventory FROM players WHERE id = '" .$selectedprofile["id"]."'");
$inventory_array = [];
while ($inventory_data = $result_inventory->fetch_assoc()) {
$inventory_array[] = $inventory_data;
}
?>
To show result:
<?php
$obj = json_encode($result_inventory);
foreach($obj as $inventory){
?>
<tbody>
<tr>
<td>
<div >
<div>
<img src="" alt="xd">
</div>
<div >
<h6 ></h6>
</div>
</div>
</td>
<td>
<div >
<?php echo $inventory['name'];?>
<h6 ><?php echo $inventory['slot']; ?></h6>
</div>
</td>
<td >
<span ><?php echo $inventory['type']?></span>
</td>
<td >
<span ><?php echo $inventory['slot']?></span>
</td>
<td >
<span ><?php echo $inventory['amount']?></span>
</td>
</tr>
<tr>
</tr>
</tbody>
<?php }?>
</table>
The Message Result:
Invalid argument supplied for foreach()
My Database:
[{"name":"phone","type":"item","slot":1,"amount":1,"info":[]},{"name":"driver_license","type":"item","slot":2,"amount":1,"info":{"birthdate":"1999-02-02","lastname":"Salem","type":"Class C Driver License","firstname":"Saleh"}},{"name":"id_card","type":"item","slot":3,"amount":1,"info":{"birthdate":"1999-02-02","gender":0,"citizenid":"TLT73227","nationality":"Saudi","lastname":"Salem","firstname":"Saleh"}},{"name":"lockpick","type":"item","slot":4,"amount":1,"info":[]}]
So I want my result like this:
<a>Name: Phone</a>
<a>Type: item</a>
<a>Slot: 1</a>
<a>Amount: 1</a>
So I tried to get result, please help me to extract.
CodePudding user response:
I see now. I assume there's only 1 row for every profile id. so
$result_inventory = $con2->query("SELECT inventory FROM players WHERE id = '" . $selectedprofile["id"] . "'");
$inventory_array = [];
while ($inventory_data = $result_inventory->fetch_assoc()) {
$inventory_array[] = $inventory_data;
}
$json = $inventory_array[0]['inventory']; // if exists
$obj = json_decode($json, true);
// then continue with
foreach ($obj as $inventory) {
//...
}