tags:

views:

219

answers:

1

I am creating a order table. my problem I am having is with my form field for each row/record with in the table.

<input type="text" size="4" name="buy_item['2']" value="0">

I am defining each identifier by a similar syntax

buy_item[ item number ] 

my problem is when the entire form is sent to through the post request how do I know exactly which items were purchased?

It is possible their could be up to 100 different items, so the post variables can always be changing. what i know how to do is hard code each item into the buy script but i feel that it is much to in efficient and would really lack the ability to add items or remove them.

$_POST['buy_item[2]']; 

would be the equivalent to the example at the top. but what if someone bought

buy_item['99'] 

instead of buy_item['2']

If anyone can lead me towards the right direction it would be greatly appreciated.

A: 
foreach($_POST['buy_item'] as $item_id=>$amount){
  if($amount > 0){
    echo $item_id." was bought ".$amount." times";
  }
}
Rufinus
$item_id would give you array index so it would say item 0 was bought at blah blah, where as it should represent name of the item :)
Sarfraz
nope, he defines it as name="buy_item['2']" 2 is the item id. it makes no sense if it would be otherwise, he needs two values, the item id and the amount bought.
Rufinus
defining it like this buy_item['2'] won't work for multiple fields, he needs to wrap them in an array by just using buy_item[]
Sarfraz
sorry sarfraz but buy_item[2] is a array. but with defined index key. used it a thousand times. (of course only works in post method forms)proof: http://www.cwd.at/so.php
Rufinus
yes i agree that it is array but questioner does not know ahead of time how to handle **unknown** number of fields, he just showed example but he is not using names like that. :)
Sarfraz
[quote]I am defining each identifier by a similar syntaxbuy_item[ item number ] [/quote] suggest otherwise... but ok, let the questioner decide :-)
Rufinus
he also said in the line "suggest otherwise", he was doing it wrong way. and you are right let the questioner decide :)
Sarfraz
This is perfect, it allows me to have any amount of items selected, while separating each Key and value with each other.
Jeremy