scuttzz Posted December 6, 2013 Share Posted December 6, 2013 Hi all Im run across an error that i cant figure out a solution to.. I get this error "Warning: Illegal string offset 'quantity' when I try echo the value for quantity. This is obviously for a online cart.. <div class="cartWrapper"> <?php if(isset($_POST['submit'])){ foreach ($_POST['quantity'] as $key => $value){ if($val==0){ unset($_SESSION['cart'][$key]); }else{ $_SESSION['cart'][$key]['quantity']=$val; } } } ?> <table class="cartTable"> <tr style="text-align:left;"> <th width="20%">Product Name</th> <th width="10%">Quantity</th> <th width="15%">Unit Price</th> <th width="20%">Total</th> </tr> <?php $sql = "SELECT * FROM products WHERE id IN("; foreach ($_SESSION['cart'] as $id => $value){ $sql.=$id.","; } $sql=substr($sql, 0, -1). ") ORDER BY product_name ASC"; $query=mysql_query($sql); $totalprice=0; while($row=mysql_fetch_array($query)){ $subtotal=$_SESSION['cart'][$row['id']]['quantity']*$row['price']; $totalprice+=$subtotal; ?> <tr> <td><?php echo $row['product_name'];?></td> <td><?php echo $row['id']['quantity']?></td> //ERROR IN THIS LINE <td>R<?php echo $row['price'];?></td> <td>R<?php echo $_SESSION['cart'] [$row['id']]['quantity']* $row['price'];?></td> </tr> <?php } ?> <tr> <td>Cart Total : <?php echo $totalprice; ?> </tr> </table> <br/> <button type="submit" name="submit">Update Cart</button> </div> Have I made an error somewhere that I haven't seen.Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/284590-warning-illegal-string-offset-error/ Share on other sites More sharing options...
dalecosp Posted December 6, 2013 Share Posted December 6, 2013 Have I made an error somewhere that I haven't seen. Yes. Thanks in advance. You're welcome. What is the error? You're assuming that something exists that doesn't. $row doesn't have ['id']['quantity'], it only has $row['id']. What's in that ID field? And what are you trying to accomplish ... counting something? Quote Link to comment https://forums.phpfreaks.com/topic/284590-warning-illegal-string-offset-error/#findComment-1461524 Share on other sites More sharing options...
Solution requinix Posted December 6, 2013 Solution Share Posted December 6, 2013 Given how the quantity is used everywhere else in the script, you probably want echo $_SESSION['cart'][$row['id']]['quantity']; Quote Link to comment https://forums.phpfreaks.com/topic/284590-warning-illegal-string-offset-error/#findComment-1461527 Share on other sites More sharing options...
scuttzz Posted December 6, 2013 Author Share Posted December 6, 2013 Thanks alot, appreciate it Quote Link to comment https://forums.phpfreaks.com/topic/284590-warning-illegal-string-offset-error/#findComment-1461535 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.