Page 1 of 1

counting variables from form

Posted: Wed Mar 16, 2005 7:07 pm
by shiznatix
Im making a form and in the form i have a mysql query that makes text fields with a specific name for each. when you hit submit i need it to update the database with the new quantity (what was in the textbox).

my problem is i need a way to count the amount of textboxes that were submited with the form. I also need to be able to send a hidden field with each textbox so i can have the itemID and the quantity for that itemID.

Code: Select all

for ($i=0; $i</*amount of textboxes submited whith the form*/; $i++){
    $qry = mysql_query("UPDATE cart SET qty="/*named textbox*/" WHERE itemID="/*itemID going with the named textbox*/"");
}
i know this is a bit confusing, sorry i really cant word this any better because im real confused as to how to do this.

Posted: Wed Mar 16, 2005 7:28 pm
by feyd
you could iterate through the posted data using isset() until you hit a false.. or, you could name them such that they create an array for php to hand right to you. :)

Posted: Wed Mar 16, 2005 7:38 pm
by shiznatix
ya im trying the $z=0 then the textarea name is '.$z++.' but i need to pass with that name the itemID. how would i do that?

Posted: Wed Mar 16, 2005 7:49 pm
by feyd
base the name off the itemID in some fashion.. :?:

Posted: Wed Mar 16, 2005 7:54 pm
by hongco
one thing i've noticed from your 3 lines of codes is that it has to do as many querries as your posted data, and that is not a good idea. You can update your table in 1 single query.

Posted: Wed Mar 16, 2005 7:59 pm
by shiznatix
feyd wrote:base the name off the itemID in some fashion.. :?:
the itemID is preset and i wanna pass it through a hidden field but i need to link the quantity field and the hidden field together even though there will be many hidden and quantity fields in the 1 form. heres some code maybe this will make it clearer?

Code: Select all

<form action="view_cart.php?action=update_qty" method="post">
$itm_qty = mysql_query("SELECT * FROM cart WHERE cartID='$cartID' AND itemID='$itemID'");
		while ($row_qty = mysql_fetch_assoc($itm_qty)){
		echo '<td> 
		
		<input type="text" name="'.$z++.'" value="'.$row_qty['qty'].'" size="2">
//i need a way to have the $itemID in a hidden field that i can link to the $z++ thing
		
		</td>
		';
}
echo '
<input type="submit" name="new_qty_s" value="Change Quantity">
</form>';
keep in mind that this is inside of another loop and each time the other loop goes through $itemID changes

Posted: Wed Mar 16, 2005 8:12 pm
by feyd
It would have been nice to know that in the beginning.. :P ... use a name that will create an array in php automatically... as I suggested earlier.

Posted: Wed Mar 16, 2005 8:35 pm
by shiznatix
lol sorry but! a name that will automatically create a array or somtin i can make one out of like a number autoincrementing like i have $z made? that still dosnt help me link the $z to whatever i define the hidden field holding the itemID for that $z? please elaborate

Posted: Wed Mar 16, 2005 8:49 pm
by feyd
how about.....

Code: Select all

<input type=&quote;text&quote; name=&quote;items&#1111;itemID]&quote; value=&quote;12&quote; />
:D

Posted: Wed Mar 16, 2005 10:17 pm
by shiznatix
ahhhh i know fyed i said i had it and i thought i did but i didnt. heres what i have but it isnt working obviously

Code: Select all

&lt;input type='text' name='items&#1111;$itemID]' value='&quote;.$row_qty&#1111;'qty'].&quote;' /&gt;

Code: Select all

foreach($items as $key => $value){
		$qry = mysql_query("UPDATE cart SET qty='$value' WHERE cartID='$cartID' AND itemID='$key'");
		}
where $itemID and $row_qty['qty'] are diffrent every time. how can i pass the name as a array? sigh...