Page 1 of 1

Retrieving data from many form objects in a loop

Posted: Tue Mar 22, 2005 11:29 pm
by robster
I have an array that contains the product ID of various products. IE (1,2,34,23,7,12,15,67,234,4)

I parse the database using each array item as a unique ID and retrieve the product info taken from each ID in the array.

When the product info is displayed, it is done so inside a form. The price for each product is displayed in a Text Field box, so if a customer gets a discount, the price may be edited. There could be man Text Field boxes, depending on how many products have been chosen.

I want to then be able to take the product_id and new adjusted prices from each product (if any were adjusted) and use them later for totals, stock control etc, but the problem I'm facing is, the Text Fields are drawn in a loop, and I am unsure of how to retrieve the data. See this code for the problem:

Code: Select all

foreach($services as $val) 
		{
  			
			$sql = "SELECT * FROM services WHERE id = $val";
			$content = mysql_query($sql);
			$Xcontent = mysql_fetch_array($content);	
		
				$service_id = $Xcontent["id"];
				$service_name = $Xcontent["name"];
				$service_description = $Xcontent["description"];
				$service_price = $Xcontent["price"];

				if ($val == $service_id)
				{
					echo "
					<tr>
					<td class=\"clientview\"><div align=\"center\">$service_name</div></td>
					<td class=\"clientview\"><div align=\"center\">$$service_price</div></td>
					<td class=\"clientview\"><div align=\"center\"><input name=\"adjusted_service_price\" type=\"text\" value=\"$service_price\" size=\"3\" maxlength=\"4\"></div></td>
					";

				}
		}
The problem and my question is about the name of the Text Edit box, it is 'adjusted_service_price'. The problem with that is, each of my (say) 10 products will have the same Text Edit box name, hence I will only be able to get the value of the very last one (I presume) and I need to get the values from all of them.

This could be overcome by supplying each Text Edit box with a unique name, ie, make the name value adjusted_service_price

I really hope that makes sense and I really hope someone has some ideas. It's been working this brain of mine over for two days now whilst I work on other things to keep moving but the time has come, it has to be sorted. Any help would be GREATLY appreciated.

Posted: Tue Mar 22, 2005 11:39 pm
by feyd

Posted: Wed Mar 23, 2005 6:42 am
by robster
mmm, sounds like he's having the same problem. Like I said, it's easy to get a unique name for each Text Edit box, and even have a unique value assigned to each unique name, the problem is then passing that data to another form, having that other form know how many items are being passed, and what names to look for when doing the variable $_Get thing. Sometimes it may pass only 1 item, other times as many as 10 or 20, and always with a different name.

It's really a tough one, I just can't think of a way to do it, mind you, I am really not too advanced at this.

Thanks for that link BTW, but it seems that thread is also in a quandry. Hopefully working one out will help the other :)

Posted: Wed Mar 23, 2005 8:09 am
by feyd
no.. it's not tough.. you name them "foo[item number]" they will come into php as $_POST['foo'] the array..

Posted: Thu Mar 24, 2005 12:17 am
by robster
That's pretty much what I'm after except I need to have both the ID and the Price variable in the array.

Is there a way to have the ID and Price for many items in an array? A multidimentional array perhaps? If so, could I be directed to some info on how to achieve it?

The array would roughly look like:

ID Price
1 - 10
5 - 34
2 - 36
12 - 1
etc - etc

Thanks again :)

Posted: Thu Mar 24, 2005 12:26 am
by feyd
the prices for the items are passed as the value of the array element.