sum_guy Posted June 22, 2009 Share Posted June 22, 2009 Hi I am designing a shopping cart. I have stored the cart information within a multiple session array. To display the cart I have used a foreach function. Within this function I have used a remove button to remove individual items. How would I use the $_POST to match the individual item. Please advise. Below is the code I have so far: <?php session_start(); /** * @author Grant Kinsman * @copyright 2009 */ $b = count($_SESSION['cart']); if ($_POST[$i]) { $_SESSION['cart'][$i] = ""; $_SESSION['prod'] = $_SESSION['prod']-1; } ?> <html> <head> <title>Mums Cards</title> <link rel="stylesheet" href="style.css" style="text/css" /> </head> <body> <div id="titleBar"> <p class="shopCar"><b>Cart</b><br> Items - <?php echo $_SESSION['prod']; ?><br> Total - £<?php echo $_SESSION['total']; ?> <a href="checkout.php" style="padding-left=0px">Checkout</a></p> </div> <div id="menu"> <a href="index.php">Home</a> <a href="products.php">Products</a> </div> <div id="main"> <div id="checkOut"> <form method="post" action="<?php $_SERVER['PHP_SELF']; ?>"> <?php foreach ($_SESSION['cart'] as $name => $id) { echo "<div class='check'>"; echo "<img src='" .$id['image']. "' class='check2'>"; echo "Order No: " .$id['orderNo']. "<br>"; echo "Quantity: " .$id['quantity']. "<br>"; echo "Unit Price: £" .$id['price']. "<br>"; echo "Info: " .$id['info']. "<br>"; echo "<input type='submit' value='remove' name='" .$i++. "'>"; echo "</div><br>"; } ?></form></div> </form> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/163221-solved-php-_post-array/ Share on other sites More sharing options...
MadTechie Posted June 22, 2009 Share Posted June 22, 2009 I assume you mean something like this <?php session_start(); /** * @author Grant Kinsman * @copyright 2009 */ $b = count($_SESSION['cart']); if ($_POST['remove']) { if(isset($_SESSION['cart'][$_POST['Item']])) { unset($_SESSION['cart'][$_POST['Item']]); $_SESSION['prod']--; } } ?> <html> <head> <title>Mums Cards</title> <link rel="stylesheet" href="style.css" style="text/css" /> </head> <body> <div id="titleBar"> <p class="shopCar"><b>Cart</b><br> Items - <?php echo $_SESSION['prod']; ?><br> Total - £<?php echo $_SESSION['total']; ?> <a href="checkout.php" style="padding-left=0px">Checkout</a></p> </div> <div id="menu"> <a href="index.php">Home</a> <a href="products.php">Products</a> </div> <div id="main"> <div id="checkOut"> <form method="post" action="<?php $_SERVER['PHP_SELF']; ?>"> <?php foreach ($_SESSION['cart'] as $name => $id) { echo "<div class='check'>"; echo "<img src='" .$id['image']. "' class='check2'>"; echo "Order No: " .$id['orderNo']. "<br>"; echo "Quantity: " .$id['quantity']. "<br>"; echo "Unit Price: £" .$id['price']. "<br>"; echo "Info: " .$id['info']. "<br>"; echo "<input type='hidden' value='$name' name='Item'>"; echo "<input type='submit' value='remove' name='remove'>"; echo "</div><br>"; } ?></form></div> </form> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/163221-solved-php-_post-array/#findComment-861195 Share on other sites More sharing options...
sum_guy Posted June 22, 2009 Author Share Posted June 22, 2009 That works thanks ever so much. Been banging my head on a brick wall for days lol. Quote Link to comment https://forums.phpfreaks.com/topic/163221-solved-php-_post-array/#findComment-861201 Share on other sites More sharing options...
MadTechie Posted June 22, 2009 Share Posted June 22, 2009 Your welcome, can you click topic solved bottom left Quote Link to comment https://forums.phpfreaks.com/topic/163221-solved-php-_post-array/#findComment-861204 Share on other sites More sharing options...
sum_guy Posted June 22, 2009 Author Share Posted June 22, 2009 It works but it is only removing the last item in the array not the individual items sorry. Quote Link to comment https://forums.phpfreaks.com/topic/163221-solved-php-_post-array/#findComment-861221 Share on other sites More sharing options...
MadTechie Posted June 22, 2009 Share Posted June 22, 2009 your need to put the form in the loop <?php session_start(); /** * @author Grant Kinsman * @copyright 2009 */ $b = count($_SESSION['cart']); if ($_POST['remove']) { if(isset($_SESSION['cart'][$_POST['Item']])) { unset($_SESSION['cart'][$_POST['Item']]); $_SESSION['prod']--; } } ?> <html> <head> <title>Mums Cards</title> <link rel="stylesheet" href="style.css" style="text/css" /> </head> <body> <div id="titleBar"> <p class="shopCar"><b>Cart</b><br> Items - <?php echo $_SESSION['prod']; ?><br> Total - £<?php echo $_SESSION['total']; ?> <a href="checkout.php" style="padding-left=0px">Checkout</a></p> </div> <div id="menu"> <a href="index.php">Home</a> <a href="products.php">Products</a> </div> <div id="main"> <div id="checkOut"> <?php foreach ($_SESSION['cart'] as $name => $id) { echo "<form method=\"post\" action=\"{$_SERVER['PHP_SELF']}\">"; echo "<div class='check'>"; echo "<img src='" .$id['image']. "' class='check2'>"; echo "Order No: " .$id['orderNo']. "<br>"; echo "Quantity: " .$id['quantity']. "<br>"; echo "Unit Price: £" .$id['price']. "<br>"; echo "Info: " .$id['info']. "<br>"; echo "<input type='hidden' value='$name' name='Item'>"; echo "<input type='submit' value='remove' name='remove'>"; echo "</div><br>"; echo "</form>"; } ?></div> </form> </div> </body> </html> or use a checkbox selection ie <?php session_start(); /** * @author Grant Kinsman * @copyright 2009 */ $b = count($_SESSION['cart']); if ($_POST['remove']) { foreach($_POST['Items'] as $Item) { if(isset($_SESSION['cart'][$Item])) { unset($_SESSION['cart'][$Item]); $_SESSION['prod']--; } } } ?> <html> <head> <title>Mums Cards</title> <link rel="stylesheet" href="style.css" style="text/css" /> </head> <body> <div id="titleBar"> <p class="shopCar"><b>Cart</b><br> Items - <?php echo $_SESSION['prod']; ?><br> Total - £<?php echo $_SESSION['total']; ?> <a href="checkout.php" style="padding-left=0px">Checkout</a></p> </div> <div id="menu"> <a href="index.php">Home</a> <a href="products.php">Products</a> </div> <div id="main"> <div id="checkOut"> <form method="post" action="<?php $_SERVER['PHP_SELF']; ?>"> <?php foreach ($_SESSION['cart'] as $name => $id) { echo "<div class='check'>"; echo "<img src='" .$id['image']. "' class='check2'>"; echo "Order No: " .$id['orderNo']. "<br>"; echo "Quantity: " .$id['quantity']. "<br>"; echo "Unit Price: £" .$id['price']. "<br>"; echo "Info: " .$id['info']. "<br>"; echo "<input type='checkbox' value='$name' name='Item[]'>"; echo "</div><br>"; } ?> <input type='submit' value='remove' name='remove'> </form></div> </form> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/163221-solved-php-_post-array/#findComment-861246 Share on other sites More sharing options...
sum_guy Posted June 22, 2009 Author Share Posted June 22, 2009 Thanks thats perfect Quote Link to comment https://forums.phpfreaks.com/topic/163221-solved-php-_post-array/#findComment-861253 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.