When I run the code I get the error:
Fatal error: Uncaught Error: Cannot use object of type stdClass as array in C:\xampp\htdocs\MyShoppingList\list.php:487 Stack trace: #0 {main} thrown in C:\xampp\htdocs\MyShoppingList\list.php on line 487;
I'm not sure why?
function updateList() {
//? Retrieve items from form POST, decode & unserialize using json. Empty array if not available.
$items = $_POST['items']
? json_decode(base64_decode($_POST ['items'], true))
: [];
//? Check if food and category was supplied.
if(filter_input(INPUT_POST, 'food') !== "" && filter_input(INPUT_POST, 'category') !== "") {
//? Add food/category as item to Shopping List
$items[] = [
"food" => filter_input(INPUT_POST, 'food'),
"category" => filter_input(INPUT_POST, 'category'),
];
}
return $items;
}
<input type="hidden" name="items" value="<?php echo base64_encode(json_encode($items)) ?>">
<div id="table">
<table width="100%">
<?php if ($items) { ?>
<?php foreach($items as $item){?>
<tr>
<td id="dairy">
<?php echo $item['category'];?>
</td>
</tr>
<form>
<tr>
<td id="data">
<?php echo'';?>
<label class="container">
<input
id="<?php echo "check${k}"; ?>"
type="checkbox"
name="<?php echo "check${k}"; ?>"
value="<?php echo $item['food']; ?>">
<span class="checkmark"></span>
</label>
</td>
</tr>
</form>
<?php } ?>
<?php } else { ?>
<tr>
<td align="center" style="padding: 20px">No items in list.</td>
</tr>
<?php } ?>
</table>
It's a json_decode
problem: It should have true
as second parameter in order to get a result as array. In your code this function is used once, but without a second parameter.
This means: Instead of
json_decode(base64_decode($_POST ['items'], true))
It should be:
json_decode(base64_decode($_POST ['items']), true)
Or if the second parameter for base64_decode is needed:
json_decode(base64_decode($_POST ['items'], true), true)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.