PDA

View Full Version : little help needed with loops


earthsiege
07-27-2004, 04:21 PM
hey guys (and girls),
Ive been trying to make a simple update script for the shopping basket of my page where if the certain product id is already in the order table the quantity number is updated but if its not then the new product and product id are inserted into the table.
but the trouble i seem to be having is im using too loops for my variable varibles and when the product ids dont match thenew order is inserted not once but n times where n is the amount of orders in my table.
$numberA is the number of products in my products table and the quantiti.$i and $proid come from the page before.
heres the script:
if ($update == 1) {
$query1 = "SELECT * FROM `order` WHERE com_id='$comid' and step_number='5'";
$result1 = MYSQL_QUERY($query1);
$num = MYSQL_NUM_ROWS($result1);
for ($B = 0; $B < $num ; $B++){
${"orderid".$B} = MYSQL_RESULT($result1,$B,"order_id");
${"productid".$B} = MYSQL_RESULT($result1,$B,"product_id");
}
For ($C =0; $C < $num ; $C++) {
$ordid = "${'orderid'.$C}";
$productid = "${'productid'.$C}";
for ($B = 0; $B < $numberA ; $B++){
$qty= ${'quantity'.$B};
$proid= ${'proid'.$B};
print("$numberA from page b4 qty is $qty and proid is $proid and for order orderid is $ordid and productid is $productid <p> ");
if ($proid == $productid){
print ("yes<p>");
$query2 = "UPDATE `order` SET quantity='$qty' where order_id='$ordid'";
$result2 = MYSQL_QUERY($query2);
}
else {
print("no<br>");
if ($qty > 0) {
$query3 = "INSERT INTO `order` ( `order_id` , `com_id` , `product_id` , `quantity` , `step_number` )
VALUES ('', '$comid', '$proid', '$qty', '5')";
} $result3 = MYSQL_QUERY($query3);
}
}
}
//$rc=Header("location:summary.php?comid=$comid");
}
can anyone shed a light on my problem.
:confused:
p.s i think that i have to make the two loops into one but because of the different number of varible variables that need to be compared i dont know how to do this.

earthsiege
07-27-2004, 04:42 PM
never mind guys
i sorted it out by changing the productid field to unique so that i dont get multiple entries.
thanks anyway though.