cyrilsnodgrass Posted April 14, 2007 Share Posted April 14, 2007 Hi guys, Noob to php and I have a problem with a college assignment. Basically I am writing a shopping cart from scratch. I can get my first item looking good but I can never add to my cart. If I choose another item it seems to overwrite the first one. Can anyone suggest what I am doing wrong please ????? Each page calls this function :- function session_check() { /** First use session_start function to (a) check for session existence and (b) start a session if **/ /** no session exists **/ session_start(); } Then my form to add to the cart calls this function :- function addtocart() { /** Add to cart **/ $item = $_POST[item]; If (!$_SESSION[cart][$item] ) { $_SESSION[cart][$item] = 1; } Else { echo "in here"; $_SESSION[cart][$item] = $_SESSION[cart][$item]+1; } /** just for testing print out all values in array **/ foreach ($_SESSION[cart] as $key => $value) { echo "$key >= $value"; } /** Build string to display cart items **/ $disp_str = "<h2>Added to your cart: "; $disp_str .= $_POST[item]; $disp_str .= "</h2><hr><p>"; $disp_str .= "<table><tr><td colspan='3'><h2>Your Shopping Cart:</h2></td></tr>"; /** Loop to retrieve and build table values from cart **/ foreach ($_SESSION[cart] as $key => $value) { $disp_str .= "<tr><td><img src='images/"; $disp_str .= $key; $disp_str .= ".jpg' alt='"; $disp_str .= $key; $disp_str .= "'></img></td>"; $disp_str .= "<td>$key qty = "; $disp_str .= "$value </td>"; $disp_str .= "<td><input type='submit' value='Remove One'></input></td>"; $disp_str .= "</table>"; } echo $disp_str; } /** **/ /** End Of Library Functions **/ /** **/ Any help would be really appreciated !!!!! EDITED BY WILDTEEN88: Please use the code tags ( ) when including code in your posts. This helps to identity text from code Quote Link to comment Share on other sites More sharing options...
Barand Posted April 14, 2007 Share Posted April 14, 2007 Always use quoted strings for array keys, not just the string Check if the item is in the cart using isset() eg <?php session_start(); function addtocart() { /** Add to cart **/ $item = $_POST['item']; if (!isset($_SESSION['cart'][$item]) ) { $_SESSION['cart'][$item] = 1; } else { echo "in here"; $_SESSION['cart'][$item]++; } /** just for testing print out all values in array **/ echo '<pre>', print_r($_SESSION['cart'], true), '</pre>'; /** Build string to display cart items **/ $disp_str = "<h2>Added to your cart: "; $disp_str .= $_POST['item']; $disp_str .= "</h2><hr><p>"; $disp_str .= "<table><tr><td colspan='3'><h2>Your Shopping Cart:</h2></td></tr>"; /** Loop to retrieve and build table values from cart **/ foreach ($_SESSION['cart'] as $key => $value) { $disp_str .= "<tr><td><img src='images/"; $disp_str .= $key; $disp_str .= ".jpg' alt='"; $disp_str .= $key; $disp_str .= "'></img></td>"; $disp_str .= "<td>$key qty = "; $disp_str .= "$value </td>"; $disp_str .= "<td><input type='submit' value='Remove One'></input></td>"; $disp_str .= "</table>"; } echo $disp_str; } /** ** End Of Library Functions **/ if (isset($_POST['item'])) addtocart(); ?> <form method='post'> <input type="text" name="item"><input type="submit" name="action" value="Submit"> </form> Quote Link to comment Share on other sites More sharing options...
cyrilsnodgrass Posted April 15, 2007 Author Share Posted April 15, 2007 hi folks, sorry for not being able to get this to work, but following the good advice given has not made any difference - the bleedin' cart only ever holds one item and when I try to add another item it seems to replace the previous one or maybe the session has been lost or something ?!?!?!?!! One thing I was wondering - I am using IE6. Are there any php environment type variables I should be manipulating ? Must admit I am getting to like php. Cheers guys, Cyril Snod Quote Link to comment Share on other sites More sharing options...
cyrilsnodgrass Posted April 15, 2007 Author Share Posted April 15, 2007 ok so you need session_start() on each page you want to use session variables on .....D'OH !!!! Quote Link to comment 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.