counting variables from form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

counting variables from form

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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. :)
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

base the name off the itemID in some fashion.. :?:
hongco
Forum Contributor
Posts: 186
Joined: Sun Feb 20, 2005 2:49 pm

Post 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.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

how about.....

Code: Select all

<input type=&quote;text&quote; name=&quote;items&#1111;itemID]&quote; value=&quote;12&quote; />
:D
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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...
Post Reply