Warning: Invalid argument supplied for foreach() in /.******************************/web/wad/sbasket.php on line 196 <?php session_start (); include ('connection.php'); // define session variable $_SESSION['cart']; //if product exists in command line, add it to cart @$id = $_GET['prod_id']; function writeShoppingCart() { $cart = $_SESSION['cart']; if (!$cart) { return '<p>You have no items in your shopping basket</p>'; } else { // Parse the cart session variable $items = explode(',',$cart); $s = (count($items) > 1) ? 's':''; return '<p>You have <a href="cart.php"> '.count($items).' item '.$s.' in your shopping basket</a></p>'; } } function showCart() { global $db; $cart = $_SESSION['cart']; if ($cart) { $items = explode(',',$cart); $contents = array(); foreach ($items as $item) { $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1; } $output[] = '<form action="cart.php?action=update" method="post" id="cart">'; $output[] = '<table>'; foreach ($contents as $id=>$qty) { $sql = "SELECT * FROM products WHERE prod_id = ".$id." "; $result = $db->query($sql); $row = $result->fetch(); extract($row); $output[] = '<tr>'; $output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>'; $output[] = '<td>'.$p_name.'</td>'; $output[] = '<td>£'.$price_wk.'</td>'; $output[] = '<td><input type="text" name="qty '.$id.' "value="'.$qty.'" size="3" maxlength="3" /></td>'; $output[] = "<td>£".($price_wk * $qty)."</td>"; $total += $price_wk * $qty; $output[] = '</tr>'; } $output[] = '</table>'; $output[] = '<p>Grand total: <strong>£'.$total.'</strong></p>'; $output[] = '<div><button type="submit">Update cart</button></div>'; $output[] = '</form>'; } else { $output[] = '<p>You shopping basket is empty.</p>'; } return join('',$output); } //checks if command line has product added if($id){ //checks if cart is empty & creates it if(!isset($_SESSION['cart'])){ $_SESSION['cart'] = array(); $_SESSION['items'] = 0; $_SESSION['total_price'] = '0.00'; } //cart array contains productid & quantity if (isset($_SESSION['cart'] [$id] )) $_SESSION['cart'] [$id]++; else $_SESSION['cart'] [$id] = 1; } //checks if command line has product updated if(isset($_POST['update'])){ foreach ($_SESSION['cart'] as $id => $qty){ //value from text box name if($_POST[$id]=='0') unset($_SESSION['cart'] [$id]); else $_SESSION['cart'] [$id] = $_POST[$id]; } } ?> <?php //displays products if ($_SESSION['cart']){ echo '<div id="product">'; echo '<h3>My Basket</h3>'; echo '<table width="400" border="0" cellpadding=0 cellspacing=5>'; echo '<form name="userForm" method="post" action="sbasket.php">'; echo '<tr><th width="200" scope="col" align="center">Title</th>'; echo '<th width="100" align="right">Quantity</th>'; echo '<th width="100" align="right">Price</th></tr>'; $_SESSION['total_price'] = '0.00'; $_SESSION['items'] = 0; //place lines of products in page foreach ($_SESSION['cart'] as $id => $qty){ $query = 'select * from products where prod_id = '.$id.' '; $result = mysql_query($query); if (!$result){ mysql_close($conn); return -2; } $catNo = mysql_num_rows($result); if ($catNo == 0){ mysql_close($conn); return 0; } $product = array(); $product = mysql_fetch_array($result); $tprice = $qty*$product['price_wk']; echo '<tr><td align = "left"><span>'; echo $product['p_name']; echo '</span></td>'; echo '<td align = "right"><span>'; //name of textbox=prodid and value=qty echo '<input name="'.$id.'" type="text" size="3" maxlength="3" value="'.$qty.'">'; echo '</span></td>'; echo '<td align = "right"><span>£'; echo $tprice; echo '</span></td></tr>'; $_SESSION['items'] += $qty; $_SESSION['total_price'] += $tprice; } //starts the update qty proceedure echo '<input type="hidden" name="update" value=true>'; echo '<tr bgcolor="#999999"><td align="center"><input type="submit" name="Submit" value="Update Shopping Basket"></td><td align = "right"><b>Number of weeks: '.$_SESSION['items'].'</b></td><td align="right"><b>Total Price: £'.$_SESSION['total_price'].'</b></td></tr>'; echo '</form>'; echo '</table>'; echo '</div>'; }else{ //if no product in cart echo '<div id="information">'; echo '<h3><span>No products in Shopping basket</span></h3>'; echo '<p class="p1"><span>Please login and add products to your shopping basket</span></p>'; echo '</div>'; } //further options echo '<div id="product">'; echo '<h3>Further options</h3>'; if(isset($_SESSION['loggedUser'])){ echo '<form name="furForm" method="post" action="finalizeorder.php"><input type="submit" name="Submit" value="Finalize Order"></form>'; } else{ echo '<form name="furForm" method="post" action="register.php"><input type="submit" name="Submit" value="Please login to finalize your order"></form>'; } echo '</div>'; ?> What could be wrong with the foreach statement?
Change this: <?php //displays products if ($_SESSION['cart']){ Code (markup): to: <?php //displays products if (isset($_SESSION['cart'])) { Code (markup): or even better: <?php //displays products if (isset($_SESSION['cart']) && is_array($_SESSION['cart'])) { Code (markup):