djones Posted September 18, 2008 Share Posted September 18, 2008 I need a little help getting the form values combined into an array. I have two fields: <input type="text" name="qty[]" id="qty_'.$i.'" class="qty_input" value="'.$qty.'" /> <input type="hidden" name="cart_id[]" id="cart_id_'.$i.'" value="'.$cart_id.'" /> And I need to combine them into an array so I make sure I update the correct quantity with the cart id: $array_qty = array($_POST['qty']); $array_cart_id = array($_POST['cart_id']); $qty_cart = array_combine($array_qty, $array_cart_id); foreach($qty_cart as $qty_u => $cart_id_u){ // DO my update SQL here... } print_r($qty_cart); It's not combining them. Any help? Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted September 18, 2008 Share Posted September 18, 2008 You don't need to put the array() around the POST values, since it will automatically become an array, Quote Link to comment Share on other sites More sharing options...
Barand Posted September 18, 2008 Share Posted September 18, 2008 And put the id as the key in the combined array $qty_cart = array_combine($array_cart_id, $array_qty); (The key must be unique so if qty is the key and there are several values of, say, 1, then you lose all but one of them.) Quote Link to comment Share on other sites More sharing options...
djones Posted September 18, 2008 Author Share Posted September 18, 2008 I adjusted Barand. Thanks ProjectFear: if I remove the array() from the POST I get this error: Warning: array_combine() expects parameter 1 to be array Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted September 18, 2008 Share Posted September 18, 2008 Are both fields being posted correctly? Parameter 1, that is now the $array_cart_id right? Do a print_r on it and see if it is returning an array. print_r($array_cart_id); It could be that your field isn't populating correctly. Quote Link to comment Share on other sites More sharing options...
djones Posted September 18, 2008 Author Share Posted September 18, 2008 Hmmm I did what you mentioned and nothing prints: $array_qty = $_POST['qty']; $array_cart_id = $_POST['cart_id']; //$qty_cart = array_combine($array_cart_id, $array_qty); //print_r($qty_cart); print_r($array_qty); Here are some actual field examples, so there is data there: <input type="text" name="qty[]" id="qty_2" class="qty_input" value="2" /> <input type="hidden" name="cart_id[]" id="cart_id_2" value="116" /> <input type="text" name="qty[]" id="qty_3" class="qty_input" value="1" /> <input type="hidden" name="cart_id[]" id="cart_id_3" value="117" /> Quote Link to comment Share on other sites More sharing options...
djones Posted September 18, 2008 Author Share Posted September 18, 2008 wow I'm an idiot. I did not have the <form> tag over all the cart items, only the submit button. Works now. Thanks guys... 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.