Forum Moderators: coopster

Message Too Old, No Replies

can I assign a number to the name value in a text field?

         

michlcamp

10:28 pm on Oct 12, 2005 (gmt 0)

10+ Year Member



I'm using a while loop to query products from a table to create an orderform. Works fine.
The products and descriptions "echo" on the page to create the orderform and a text field named "qty" for quantity ordered is written for each product in the table. This is where the customer puts in how many of each product they want to buy.

It writes like this:


while
blah blah blah..

echo "$product - Qty: <input type=\"text\" name=\"qty\" size=2><br>";

My question:
How do I assign ascending numbers to each "qty" field?
I'd like the output to be:

product 1 : <input type=\"text\" name=\"qty1\" size=2><br>
product 2 : <input type=\"text\" name=\"qty2\" size=2><br>
product 3 : <input type=\"text\" name=\"qty3\" size=2><br>

and so on..

the products part is already working, I'm just after how to number the "qty" field for each.

Thanks in advance.
mc

diddlydazz

10:33 pm on Oct 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



if i understand correctly:

create a variable before the while loop:

$qtyvalue = 0;

then increment the value with each iteration of the loop:

$qtyvalue ++;

then add the variable to the field name:

product X : <input type=\"text\" name=\"qty\".$qtyvalue.\"\" size=2><br>

there is probably a better way, there are plenty of PHP pros round here ;o)

HTH

dazz

vincevincevince

9:27 am on Oct 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I would make only this change:


echo "$product - Qty: <input type=\"text\" name=\"qty[".$productid."]\" size=2><br>";

You should ensure that $productid is the id number of the product, or catalog number.

In this way the results after posting the form will be in an array $_POST[qty], and to find the quantity for product 1234, you will just do $_POST[qty][1234], paving the way toward:


pg/mysql_query("UPDATE products SET qty = ".intval($_POST[qty][$id])." WHERE productid = $id");