Dablue Posted May 15, 2007 Share Posted May 15, 2007 Hi Folks, I'm working on a shopping cart and right now I'm looking at an update feature where the good shoppers of the world can update their quantities of various rows... I can't just do a straight $_POST[qty] because there are X number of rows in the order_rows table. I had a few thoughts on how to record the product id's which are associated to the qty input... I think the easiest would be to post all the qtys for the order_rows in an array... Then explode the array and then update the qtys based on there position in the order_rows table... But can it be done? I've not seen a WHERE clause which is based on row position, but I've got a hunch it can be done... There's no easy way of recording and tying the product id's to the qtys. Hope that makes sense... so if $array[0] = 1 and $array[1] = 2, I want to update the 1st occurrence in the cart table (associated to the shoppers session id) to 1 and the 2nd occurrence to 2. The only way I can see it working would be to make sure the order_rows table are displayed (ORDER BY) by their product_id's and then when I'm updating, do the update based on that display... Any thoughts please? TIA Quote Link to comment https://forums.phpfreaks.com/topic/51489-solved-how-do-i-update-rows-dependent-on-their-occurrence-in-the-table/ Share on other sites More sharing options...
Wildbug Posted May 15, 2007 Share Posted May 15, 2007 I'm not sure I really understand what you're having trouble with. If you have a table of pending orders, order_rows (customer_id,item,quantity,price), for example, can't you just update it via: UPDATE order_rows SET quantity=xx WHERE customer_id=xxxx AND item=xxxx ? Oh, or are you having trouble associating the quantity with the item number? Well, you could use a multi-dimensional array in the same way arrays are used to capture multiple FORM elements. <input type=text name="quantity[item001]" value="1"> <input type=text name="quantity[item002]" value="1"> <input type=text name="quantity[item003]" value="10"> Then in PHP: foreach ($_REQUEST['quantity'] as $key => $value) { mysql_query(sprintf('UPDATE order_rows SET quantity=%s WHERE customer_id=%s AND item=%s', $value, $_REQUEST['customer_id'], $key )); } Quote Link to comment https://forums.phpfreaks.com/topic/51489-solved-how-do-i-update-rows-dependent-on-their-occurrence-in-the-table/#findComment-253588 Share on other sites More sharing options...
Dablue Posted May 15, 2007 Author Share Posted May 15, 2007 Many thanks Wildbug - your 2nd solution: <input type=text name="quantity[order_row_id]" value="1"> was what I was looking for... Quote Link to comment https://forums.phpfreaks.com/topic/51489-solved-how-do-i-update-rows-dependent-on-their-occurrence-in-the-table/#findComment-253741 Share on other sites More sharing options...
Wildbug Posted May 15, 2007 Share Posted May 15, 2007 Cool, glad I could help! Quote Link to comment https://forums.phpfreaks.com/topic/51489-solved-how-do-i-update-rows-dependent-on-their-occurrence-in-the-table/#findComment-253818 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.