TheJoey Posted September 9, 2009 Share Posted September 9, 2009 i currently working with this very simple but im having trouble trying to implement qty amount into it. Also another trouble im having it once i add a item i cant add another one. Hope you guys can help. Thanks in advance <?php error_reporting(E_ALL); ini_set('display_errors', '1'); session_start(); $_SESSION['shopping_cart'] = array(); // And then to add items... $_SESSION['shopping_cart'][] = "Ball"; $_SESSION['shopping_cart'][] = "Shoe"; // And to echo them back out... foreach($_SESSION['shopping_cart'] as $value); ?> <html> <body> <table> <tr> <td><a href='<?PHP echo $_SERVER['REQUEST_URI']; ?>?action=add_to_cart&item=Ball'>Add Ball To Cart</a></td> </br> </tr> <tr> <td><a href='<?PHP echo $_SERVER['REQUEST_URI']; ?>?action=add_to_cart&item=Shoe'>Add Shoe To Cart</a></td> </tr> <tr> <td><select name="qty"><option value="0">--</option><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option></select></td></tr> </tr> </body> </html> <?php if($_GET['action'] == "add_to_cart") { $_SESSION['shopping_cart'][] = $_GET['item']; } ?> <?php foreach($_SESSION['shopping_cart'] as $key => $value) { echo '<br> </br>'; echo $value . "<a href='" . $_SERVER['REQUEST_URI'] . "&action=remove_item&itemkey=" . $key ."'>Remove</a>"; } if($_GET['action'] == "remove_item") { unset($_SESSION['shopping_cart'][$_GET['itemkey']]); } $_SESSION['shopping_cart'] = array(); ?> Quote Link to comment Share on other sites More sharing options...
ignace Posted September 9, 2009 Share Posted September 9, 2009 if (!isset($_SESSION['shopping_cart'])) { $_SESSION['shopping_cart'] = array(); } Quote Link to comment Share on other sites More sharing options...
TheJoey Posted September 9, 2009 Author Share Posted September 9, 2009 i placed that bottom of my page and it didnt effect it. Quote Link to comment Share on other sites More sharing options...
l0ve2hat3 Posted September 9, 2009 Share Posted September 9, 2009 <?php error_reporting(E_ALL); ini_set('display_errors', '1'); session_start(); $_SESSION['shopping_cart'] = array(); // And then to add items... just look at this for a second... everytime you load this page you are clearing $_SESSION[shopping_cart] by setting it equal to a blank array. you need to replace $_SESSION['shopping_cart'] = array(); with $_SESSION['shopping_cart'] = (!isset($_SESSION['shopping_cart']))?array():$_SESSION['shopping_cart']; Quote Link to comment Share on other sites More sharing options...
TheJoey Posted September 9, 2009 Author Share Posted September 9, 2009 Could you please explain what that is doing. im kinda new to sessions. Quote Link to comment Share on other sites More sharing options...
l0ve2hat3 Posted September 9, 2009 Share Posted September 9, 2009 VARIABLE=(CONDITION)?IF TRUE:IF FALSE; same as if (!isset($_SESSION['shopping_cart'])) { $_SESSION['shopping_cart'] = array(); } so if session variable is already set (you added something to the cart) then dont set it if not then set it as a array... Quote Link to comment Share on other sites More sharing options...
TheJoey Posted September 9, 2009 Author Share Posted September 9, 2009 Well the main problem is that once i add a item. i can remove it but after that its not possible for me to add another item to be removed. its very messy so far. Quote Link to comment Share on other sites More sharing options...
l0ve2hat3 Posted September 9, 2009 Share Posted September 9, 2009 i was bored... <?php error_reporting(E_ALL); ini_set('display_errors', '1'); session_start(); $_SESSION['shopping_cart']=(!isset($_SESSION['shopping_cart']))?array():$_SESSION['shopping_cart']; switch($_REQUEST['action']){ case'add_to_cart'; $_SESSION['shopping_cart'][$_POST['item']]['qty']=$_POST['qty']; break; case 'remove_item': unset($_SESSION['shopping_cart'][$_REQUEST['item']]); break; } ?> <html> <body> <form method="POST"> <input type=hidden name="action" value="add_to_cart" /> <input type=hidden name="item" value="Ball" /> Ball: <input type="text" size=2 name="qty" /> <input type="submit" value="Add To Cart" name="submit" /> </form> <form method="POST"> <input type=hidden name="action" value="add_to_cart" /> <input type=hidden name="item" value="Shoe" /> Shoe: <input type="text" size=2 name="qty" /> <input type="submit" value="Add To Cart" name="submit" /> </form> <b>CART</b> <br /> <?php foreach($_SESSION['shopping_cart'] as $item => $item_array) { ?> <form method="POST"> <input type=hidden name="action" value="add_to_cart" /> <input type=hidden name="item" value="<?=$item;?>" /> <?=$item;?>: <input type="text" size=2 name="qty" value="<?=$item_array['qty'];?>" /> <input type="submit" value="Update" name="submit" /> <input type="button" onclick="window.location='http://<?php echo $_SERVER['SERVER_NAME'].$_SERVER['SCRIPT_NAME']; ?>?action=remove_item&item=<?=$item;?>';" value="Remove" name="remove" /> </form> <br /> <?php } ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
TheJoey Posted September 10, 2009 Author Share Posted September 10, 2009 thanks dude. Still some tweeks needed to be done but provides a good platform for me thanks again. Quote Link to comment Share on other sites More sharing options...
TheJoey Posted September 10, 2009 Author Share Posted September 10, 2009 The quanity add doesnt seem to work to well. i havent changed the code. Quote Link to comment Share on other sites More sharing options...
TheJoey Posted September 10, 2009 Author Share Posted September 10, 2009 The quanity add doesnt seem to work to well. i havent changed the code. Quote Link to comment Share on other sites More sharing options...
l0ve2hat3 Posted September 10, 2009 Share Posted September 10, 2009 The quanity add doesnt seem to work to well. i havent changed the code. Please explain... you can add some error checking to the php.... look at number_format and is_nan Quote Link to comment Share on other sites More sharing options...
TheJoey Posted September 11, 2009 Author Share Posted September 11, 2009 there isnt a error but inside the text box where the quanity number goes it displays <?=item?> insted Quote Link to comment Share on other sites More sharing options...
l0ve2hat3 Posted September 11, 2009 Share Posted September 11, 2009 oh short tags... <?php error_reporting(E_ALL); ini_set('display_errors', '1'); session_start(); $_SESSION['shopping_cart']=(!isset($_SESSION['shopping_cart']))?array():$_SESSION['shopping_cart']; switch($_REQUEST['action']){ case'add_to_cart'; $_SESSION['shopping_cart'][$_POST['item']]['qty']=$_POST['qty']; break; case 'remove_item': unset($_SESSION['shopping_cart'][$_REQUEST['item']]); break; } ?> <html> <body> <form method="POST"> <input type=hidden name="action" value="add_to_cart" /> <input type=hidden name="item" value="Ball" /> Ball: <input type="text" size=2 name="qty" /> <input type="submit" value="Add To Cart" name="submit" /> </form> <form method="POST"> <input type=hidden name="action" value="add_to_cart" /> <input type=hidden name="item" value="Shoe" /> Shoe: <input type="text" size=2 name="qty" /> <input type="submit" value="Add To Cart" name="submit" /> </form> <b>CART</b> <br /> <?php foreach($_SESSION['shopping_cart'] as $item => $item_array) { ?> <form method="POST"> <input type=hidden name="action" value="add_to_cart" /> <input type=hidden name="item" value="<?php echo $item;?>" /> <?=$item;?>: <input type="text" size=2 name="qty" value="<?php echo $item_array['qty'];?>" /> <input type="submit" value="Update" name="submit" /> <input type="button" onclick="window.location='http://<?php echo $_SERVER['SERVER_NAME'].$_SERVER['SCRIPT_NAME']; ?>?action=remove_item&item=<?php echo $item;?>';" value="Remove" name="remove" /> </form> <br /> <?php } ?> </body> </html> 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.