0

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>
0

1 Answer 1

2

You are overwritting $x

Replace

$x = $item;

with

$x[] = $item;

And initialize $x = []; before the foreach loop;

Sign up to request clarification or add additional context in comments.

5 Comments

If I replace $x = $item with $x[] = $item; I get Undefined index: for quantity and size. What do you mean exactly by "initialize $x = [];" ?
what version of php are you using? if you are using an older version than 7.0 you will have to initalize the variable as $x = array();
I'm using 5.6. Okay, I see but before which foreach loop? And what happens is I switch to php 7.1.7? Do I have to use arrays?
The issue here is that $x is different now. You need to debug and see why array_merge isn't giving you the correct data.
I made a var_dump: $products[] = array_merge($x,$p);var_dump($x); It only shows one item. But if i do a var_dump "$products = array();" it shows the two item. So for some reason $x is only giving me just one item from the string

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.