Jump to content

Warning: Illegal string offset error


scuttzz

Recommended Posts

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 :)

 

Link to comment
https://forums.phpfreaks.com/topic/284590-warning-illegal-string-offset-error/
Share on other sites

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.