Php cart update problem

Hello!

I have this php cart code below. For some reason it keeps saying Notice: Undefined variable: available in C:\xampp\htdocs\test\cart.php on line 47 I think the problem source is might be the foreach loop but I’m not sure.

<?php
require_once 'core/init.php';
include 'includes/header.php';
include 'includes/navigation.php';

if($cart_id !=''){
  $cartQ = $db->query("SELECT * FROM cart WHERE id = '{$cart_id}'");
  $result = mysqli_fetch_assoc($cartQ);
  $items = json_decode($result['items'],true);
  $i = 1;
  $sub_total = 0;
  $item_count = 0;

}
 ?>
<h3>Kosár tartalma</h3>
<?php if($cart_id == ''): ?>
  <p class="text-center">A kosár üres.</p>
  <br>
  <br>

<?php else: ?>
<table class="table table-bordered table-condensed table-striped">
  <tbody>
  <thead>
    <th>#</th><th>Termék neve</th><th>Egységár</th><th>Mennyiség</th><th>Opció</th><th>Összegzés darab szerint</th>
  </thead>
  <?php foreach($items as $item){
    $product_id = $item['id'];
    $productQ = $db->query("SELECT * FROM estate WHERE id = '{$product_id}'");
    $product = mysqli_fetch_assoc($productQ);
    $sArray = explode(',',$product['sizes']);
    foreach($sArray as $sizeString){
      $s = explode(':',$sizeString);
      if($s[0] == $item['size']){
        $available = $s[1];
      }
    }
    ?>
<tr>
  <td><?=$i;?></td>
  <td><?=$product['title'];?></td>
  <td><?=$product['price'];?> Ft.</td>
  <td>
    <button class="btn btn-default btn-ms" onclick="update_cart('removeone','<?=$product['id'];?>','<?=$item['size'];?>')">-</button>
      <?=$item['quantity'];?> db
   <?php if($item['quantity'] < $available): ?>
    <button class="btn btn-default btn-ms" onclick="update_cart('addone','<?=$product['id'];?>','<?=$item['size'];?>')">+</button>
   <?php else: ?>
     <span class="text-danger">Max.</span>
   <?php endif; ?>
  </td>
  <td><?=$item['size'];?></td>
  <td><?=$item['quantity'] * $product['price'];?> Ft.</td>
</tr>
<?php
$i++;
$item_count += $item['quantity'];
$sub_total += ($product['price'] * $item['quantity']);
$grand_total = $sub_total; ?>

    <?php } ?>
  </tbody>
</table>
<table class="table table-bordered table-condensed text-right">
  <thead class="totals-table-header"><th>Összes termék</th><th>Végösszeg</th></thead>
    <tbody>
      <tr>
        <td><?=$item_count;?> db</td>
        <td><?=$grand_total;?> Ft.</td>
      </tr>
    </tbody>
  </table>
<?php endif; ?>
<button class="btn btn-primary"><span class="glyphicon glyphicon-shopping-cart"></span> Pénztár >>></button>

 <?php
include 'includes/footer.php';
  ?>

Well $available is defined with the foreach but it’s in an if condition, so won’t be defined if the condition is not met.

Looking at this, I can’t help thinking that the way you are storing data is not the most useful. But hard to say without knowing what you are working with. But the use of json_decode and explode suggest a database redesign may help.

Actually this is the first time I do something like this so I searched for some tutorial on youtube. The guy explains things very well while coding. I did the same he did but it not works for me. Link

It would help if you could tell us which is line 47 (ie the line where the error is). As it says in comments to the tutorial, if you don’t get the same results, it will usually be a spelling mistake somewhere.

The first line. And yes, I have read the comments and I noticed it but as I see i’ve done everyting correctly.

   <?php if($item['quantity'] < $available): ?>
     <button type="button" class="btn btn-xs btn-default" onclick="update_cart('addone','<?=$product['id'];?>','<?=$item['size'];?>');">+</button>
   <?php else: ?>
     <span class="text-danger">Max.</span>
   <?php endif; ?>

You only conditionally set $available

      if($s[0] == $item['size']){
        $available = $s[1];
      }

It doesn’t look as though it will be set if $s[0] is not equal to $item['size']

Umm, okay, sorry if it’s a stupid question but what should I change?

I’m tempted to say your tutorial! However, if you added one line

$available = 0;

before that if statement, it would at least mean you shouldn’t get an undefined error. Not knowing the tut I have no idea whether it will give the required result.

2 Likes

Yes, the if condition seems to accept there is a possibility that the condition may not always return true, but it makes no allowance for that happening, as if it’s a given that available will always have a value assigned.
There are a few ways to handle this. Set an initial value as @Gandalf suggests, inset an else after the if or use an if(isset($available)) condition before referencing the variable. The best way depends on the context of how the script is supposed to work.
But I could not bring myself to watch the whole of that tutorial to see exactly how it should work. Though from what I did see of it, I did not rate it very much.

ok, I’ve tried to add something like this after the if

else{ $available = $s[0];
}

the error message is gone, but it not working as it should be. if I add to the cart 3 items from one of the size option which has 7 items set in it there should be 4 items left so I should be able add 4 more in the cart with the + button. but I can’t because it shows “Max”.

By the way, here is a var_dump from my shopping cart. Some other guy pointed out for me. I’m using accentuated letters in my sizes option. “Elsu0151 opció” is should be “Első opció”. Is it possible that it causes the problem? The var_dupm says it has 21 characters but it has only 14 (15 if count the ‘space’)

array(4) {
 [0]=>
 array(3) {
 ["id"]=>
 string(2) "50"
 ["size"]=>
 string(21) "Elsu0151 opció"
 ["quantity"]=>
 string(1) "7"
 }
 [1]=>
 array(3) {
 ["id"]=>
 string(2) "50"
 ["size"]=>
 string(21) "Elsu0151 opció"
 ["quantity"]=>
 string(1) "1"
 }
 [2]=>
 array(3) {
 ["id"]=>
 string(2) "48"
 ["size"]=>
 string(8) "Elsu0151"
 ["quantity"]=>
 string(1) "3"
 }
 [3]=>
 array(3) {
 ["id"]=>
 string(2) "50"
 ["size"]=>
 string(21) "Elsu0151 opció"
 ["quantity"]=>
 string(1) "1"
 }
}

That’s because you’ve assigned the “available” value to the size, rather than to a suitable value. As you’re comparing $s[0] to $item['size'], that suggests that it’s values will be text strings, from your var_dump above. So that’s not going to work really well for calculating the number of items available. I believe it will evaluate to zero, though, so you might as well just set it to zero as @Gandalf said earlier and make things less confusing.

How does the var_dump of the shopping cart, in particular the “size” field, compare to a var_dump of the $s array that you create, or indeed the $item['size'] field? From your last post it does seem that there is a character-encoding issue, but if both arrays have the same encoding issue then that would not cause it.

The issue boils down to the fact that the text in your shopping cart in the “size” field does not match any of the options for that field that come from the database record. As I imagine that the shopping cart is created from that database, that suggests that when the data is stored in the cart, something changes. So you need to look in your “update_cart()” function to see what that is doing.

Your code should deal with the data in the shopping cart being corrupted or not matching the database though.

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.