In my "cart" table "items" column I store json_encoded strings. I decode, loop trough and grab them as an array. It works fine as long as the items has different product ID-s:
[{"id":"56","size":"Small","quantity":"1"},{"id":"53","size":"Medium","quantity":"2"}]
But when the items have the same product ID it only shows the last one in the json string, in this case "Large":
[{"id":"53","size":"Small","quantity":"1"},{"id":"53","size":"Large","quantity":"2"}]
I made a var_dump for the products and it says that the two items are there, but like I said it only shows the last one in the output.
$txn_id = sanitize((int)$_GET['txn_id']);
$txnQuery = $db->query("SELECT * FROM transactions_alternative WHERE id = '{$txn_id}'");
$txn = mysqli_fetch_assoc($txnQuery);
$cart_id = $txn['cart_id'];
$cartQ = $db->query("SELECT * FROM cart WHERE id = '{$cart_id}'");
$cart = mysqli_fetch_assoc($cartQ);
$items = json_decode($cart['items'],true);
$idArray = array();
$products = array();var_dump($items);
foreach($items as $item){
$idArray[] = $item['id'];
}
$ids = implode(',',$idArray);
$productQ = $db->query("SELECT i.id as 'id', i.title as 'title', c.id as 'cid', c.category as 'child', p.category as 'parent'
FROM products i
LEFT JOIN categories c ON i.categories = c.id
LEFT JOIN categories p ON c.parent = p.id
WHERE i.id IN ({$ids})");
while($p = mysqli_fetch_assoc($productQ)){
foreach($items as $item){
if($item['id'] == $p['id']){
$x = $item;
continue;
}
}
$products[] = array_merge($x,$p);var_dump($products);
}
?>
<h2 class="text-center">Rendelés részletei</h2>
<div class="col-md-12">
<h3 class="text_center">Rendelt termékek</h3>
<table class="table table-condensed table-bordered table-striped">
<thead>
<th>Rendelt mennyiség</th><th>Termék neve</th><th>Kategória</th><th>Opció</th>
</thead>
<tbody>
<?php foreach($products as $product): ?>
<tr>
<td><?=$product['quantity'];?> db</td>
<td><?=$product['title'];?></td>
<td><?=$product['parent'].' / '.$product['child'];?></td>
<td><?=$product['size'];?></td>
</tr>
<?php endforeach ?>
</tbody>
</table>