Jump to content

[SOLVED] How do I update rows dependent on their occurrence in the table?


Dablue

Recommended Posts

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  8)

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
));
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.