Page 1 of 1

error with passing though variables as array

Posted: Tue Sep 12, 2006 3:11 am
by rsmarsha
I have the following code:

Code: Select all

foreach($_POST['order'] as $orderID=>$orderValue)
{
	$amount = current($_POST['amount']);
	next($_POST['amount']);
	$buy_price = current($_POST['buy_price']);
	next($_POST['buy_price']);
	$supplier = current($_POST['supplier']);
	next($_POST['supplier']);
	//update req2
	db_query("UPDATE products SET required2=required2-'".$amount."' WHERE product_id='".$orderID."'", 'update_req');
	//insert into supplier orders
    db_query("INSERT INTO supplier_orders (supplier_id,product_id,amount,price,norm_order) VALUES ('".$supplier."','".$orderID."','".$amount."','".$buy_price."','1')", 'supplier_order');
	echo 'O='.$orderID.',S='.$supplier.',A='.$amount.',P='.$buy_price.'<br />';
}
It's passed from a variables such as :

Code: Select all

<td align="center"><input type="text" name="<?php echo 'amount['.$srow['product_id'].']'; ?>" id="amount" size="3"></td>
<td align="center">£<input type="text" name="<?php echo 'buy_price['.$srow['product_id'].']'; ?>" id="buy_price" value="<?php echo ''.$srow['buy_price'].''; ?>" size="3"></td>
<td align="center"><input type="checkbox" name="<?php echo 'order['.$srow['product_id'].']'; ?>" id="order"></td>
<td align="center"><input type="checkbox" name="<?php echo 'backorder['.$srow['product_id'].']'; ?>" id="backorder"></td>
<td align="center">
For some reason it only works as it should if the top order variable is ticked before posting. If the top one in the list isn't ticked it doesn't post any of the other information apart from the order variable. Any ideas?

Posted: Tue Sep 12, 2006 3:26 am
by rsmarsha
It seems to be when one tick is missing in the order list of boxes.

So if i tick the 1st and 2nd it works, if i tick the 1st and 3rd then only the 1st will work. Really need this to work asap, anyone any ideas ? :)

Posted: Tue Sep 12, 2006 3:33 am
by Jenk
Geshi really doesn't like embedded php :\

Instead of creating text boxes with names like "order[123]" just create a hidden field for the ID, then use single element names for 'order', 'backorder' etc.

Posted: Tue Sep 12, 2006 3:36 am
by rsmarsha
You mean loop through the hidden field with the foreach?

If i have a hidden field for the id, how does it know to which rows have been ticked when all $_POST['order'] would be the same?

Posted: Tue Sep 12, 2006 3:38 am
by volka
Wouldn't it be a lot easier if you name your input elements like

Code: Select all

wo[$product_id][amount]
wo[$product_id][buy_price]
wo[$product_id][order]
wo[$product_id][backorder]
?

Posted: Tue Sep 12, 2006 3:40 am
by rsmarsha
Sorry if i'm a bit thick here, but how would i use those variables?

I need to create a query using them. There is a table with one row having one instance of each variable. I need to loop through each row of posted variables and input them as a query.

In a rush to get this done, as i'll be leaving the project soon and it needs to be done today. :( Will be very grateful if you guys can help me find a solution to this. :)

Posted: Tue Sep 12, 2006 4:56 am
by rsmarsha
Tried various methods and can't get it to work. :(

Just need to know how to pass lots of variables though in sets.

So say i have:

order (tickbox), amount, buy price

and there can be say 10 rows of those all posted. I need to know how to get each row on it's own so i can build a query.