davemoody Posted August 11, 2011 Share Posted August 11, 2011 G'day, I'm trying to get this shopping cart script to work. the relevent code is: <?php echo "<pre>" ; print_r ( $_SESSION ); echo "</pre>" ; if(isset($_POST['submit'])) { $itemname = $_POST['h1']; echo $itemname; //echo $_SESSION['itemname'][$itemname]; unset($_SESSION['itemqty'][$itemname]); unset($_SESSION['itemprice'][$itemname]); unset($_SESSION['itemname'][$itemname]); } echo "<p />"; echo "<table>"; echo "<tr><th class='a'>Name</th><th class='a'>Quantity</th><th class='a'>Price</th><th class='a'></th></tr>"; foreach($_SESSION['itemname'] as $key=>$value) { echo '<tr><td>'.$_SESSION['itemname'][$key].'</td> <td><input type="text" name="t1" value='.$_SESSION['itemqty'][$key].'></td> <td>$'.$_SESSION['itemprice'][$key].'</td> <td><form id="f1" method="post" name="f1"><input type="submit" name="submit" value="Delete Item"> <input type="hidden" name="h1" value='.$key.'></td></tr>'; } echo "</table>"; ?> It correctly displays a table, but herein lies the problem. When the last item is added to the table, it's key value becomes the key value for all the buttons. So, if I choose to delete for example the first item in the list, it's actually the last item that gets deleted. I've been staring at this code for about 10 hours and can't get anywhere. Does anyone have any clue what I could do to get it working? If it helps, here's the cart script on each of the product pages: The header: <?php session_start(); if (isset($_POST['submit'])) { if(!isset($_SESSION['item'])) $_SESSION['item'] = ''; if($_SESSION['item'] == $_POST['h1']) { $_SESSION['price'] += $_POST['h2']; } else { $_SESSION['item'] = $_POST['h1']; $_SESSION['price'] = $_POST['h2']; } $_SESSION['itemname'][$_SESSION['item']] = $_SESSION['item']; $_SESSION['itemqty'][$_SESSION['item']] = ($_SESSION['itemprice'][$_SESSION['item']] / $_POST['h2']) + 1; $_SESSION['itemprice'][$_SESSION['item']] = $_SESSION['price']; } ?> The body: <?php if(!$_SESSION['username'] == '') { echo '<form action="products_hillsong_godhe.php" method="post"> <input type="hidden" name="h1" value="New Day" /> <input type="hidden" name="h2" value="19.99" /> <input type="submit" name="submit" value="Add To Shopping Cart" /> </form>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/244499-shopping-cart-troubles/ Share on other sites More sharing options...
WebStyles Posted August 11, 2011 Share Posted August 11, 2011 you don't seem to be closing the <form> element. add </form> to the end of this: <form id="f1" method="post" name="f1"><input type="submit" name="submit" value="Delete Item"> <input type="hidden" name="h1" value='.$key.'></form> Quote Link to comment https://forums.phpfreaks.com/topic/244499-shopping-cart-troubles/#findComment-1255872 Share on other sites More sharing options...
davemoody Posted August 11, 2011 Author Share Posted August 11, 2011 Yeh, validation didn't like me adding that. either way, there or not it doesn't fix the problem. Dave. Quote Link to comment https://forums.phpfreaks.com/topic/244499-shopping-cart-troubles/#findComment-1256142 Share on other sites More sharing options...
WebStyles Posted August 11, 2011 Share Posted August 11, 2011 can you post the results of this: echo "<pre>" ; print_r ( $_SESSION ); echo "</pre>" ; So I can see the actual array please. thanks Quote Link to comment https://forums.phpfreaks.com/topic/244499-shopping-cart-troubles/#findComment-1256154 Share on other sites More sharing options...
davemoody Posted August 11, 2011 Author Share Posted August 11, 2011 Thanks for checking it out for me. the contents of the array produced by that test code is as follows: Array ( [username] => test [item] => A Beautiful Exchange [price] => 24.99 [itemname] => Array ( [Transformation] => Transformation [A Beautiful Exchange] => A Beautiful Exchange ) [itemqty] => Array ( [Transformation] => 1 [A Beautiful Exchange] => 1 ) [itemprice] => Array ( [Transformation] => 9.99 [A Beautiful Exchange] => 24.99 ) ) Quote Link to comment https://forums.phpfreaks.com/topic/244499-shopping-cart-troubles/#findComment-1256157 Share on other sites More sharing options...
WebStyles Posted August 12, 2011 Share Posted August 12, 2011 man, I just did a little test here, and your problem is what I said the first time: you need to close the form. I used this to test: <?php if($_SERVER['REQUEST_METHOD']=='POST'){ echo '<pre>'; print_r($_POST); } for($i=1;$i<10;$i++){ echo '<form id="f1" method="post" name="f1"><input type="submit" name="submit" value="Delete Item"> <input type="hidden" name="h1" value='.$i.'></form>'; } ?> without </form> at the end, I always get the number 9, with </form> everything works. Quote Link to comment https://forums.phpfreaks.com/topic/244499-shopping-cart-troubles/#findComment-1256162 Share on other sites More sharing options...
davemoody Posted August 12, 2011 Author Share Posted August 12, 2011 Ok, asssuming this is correct as you say, you're missing the foreach loop which constructs the table from the original data, you've replaced it with a normal for loop. I'm a beginner at this, how do I incorporate your fix so that it still builds that table of data? Quote Link to comment https://forums.phpfreaks.com/topic/244499-shopping-cart-troubles/#findComment-1256165 Share on other sites More sharing options...
WebStyles Posted August 12, 2011 Share Posted August 12, 2011 no, you DO NOT use my code, it was just a loop I created to verify how it would deal with not having the closing tag on your form. try what I said in my first post: add </form> after your hidden field (inside the foreach loop) and test again. Quote Link to comment https://forums.phpfreaks.com/topic/244499-shopping-cart-troubles/#findComment-1256166 Share on other sites More sharing options...
davemoody Posted August 12, 2011 Author Share Posted August 12, 2011 Well, I am embarassed BIG TIME Thank you so much, problem solved Dave. A big recommendation to you as a helper! Quote Link to comment https://forums.phpfreaks.com/topic/244499-shopping-cart-troubles/#findComment-1256169 Share on other sites More sharing options...
WebStyles Posted August 12, 2011 Share Posted August 12, 2011 glad it's sorted out. Good luck Quote Link to comment https://forums.phpfreaks.com/topic/244499-shopping-cart-troubles/#findComment-1256170 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.