Jump to content

WHY INSERT fail when I put an IF inside a FOR LOOP ?


vincej

Recommended Posts

Hi - I have spent 2 days trying to figure this out. I must be doing something stupid ...

 

I am trying to update the DB with a collection of variables off of a various POST array's. I have indexed the variables so that I can loop through them with a FOR. 

 

However I am having all kinds of trouble: 

 

1 - WEIGHT is giving 5 values even though the counter should only go 4 times.

2 - The WEIGHT variable is going into the wrong field in the DB even though I specify in my Query a specific place.

3 - I get undefined offset errors even though I have initialised the variables with their respective offsets.

4 - Adding the conditional statement adds even more offset errors even though they are initialised

 

I am starting to wonder if my entire approach to updating my DB is flawed.

 

As a student of PHP I think I need someone with more experience to look at what I am doing and tell me if something jumps out at them.

 

There is a lot of statements echoing out values - and they all seem to check out.

 

Man Many thanks for your advice

 

    function finishedorder(){  
$prodname = $_POST['prodname'];
$prodid =$_POST['prodid'];
$quantity = $_POST['quantity'];
$pricelb = $_POST['pricelb']; 
$customerid = $_POST['customerid']; 
$price = $_POST['price']; 
$orderid = $_POST['orderid']; 
$weight	= $_POST['weight'];

	echo "Varuable WEight"; print_r($weight);	 echo"<br/>";
echo "line 196 Weight"; print_r ($_POST['weight']); echo"<br/>";
echo "line 192 PriceLB"; print_r ($_POST['pricelb']); echo "<br/>";
echo "line 193 Price"; print_r ($_POST['price']); echo "<br/>";
echo "line 194 ProdID"; print_r ($_POST['prodid']); echo "<br/>";
    echo "line 195 OrderID"; print_r ($_POST['orderid']); echo "<br/><br/>";


$numloops = count($_POST['prodid']); 
for ($i = 0; $i < $numloops ; $i++) {										//  !!! Start of the for loop !!! 

if (!isset($orderid[$i])) $orderid[$i] ='';
if (!isset($prodid[$i])) $prodid[$i] ='';
if (!isset($weight[$i])) $weight[$i] ='';		
if (!isset($pricelb[$i])) $pricelb[$i] ='';									// This only initialises the Price & Weight variable to avoid an offset error message.	
if (!isset($weight[$i])) $weight[$i] ='';	


$ordervalue = $pricelb[$i] * $weight[$i];

echo " L. 216 The number is " . $i . "<br />";
echo "line 221 ProdID".$prodid[$i];echo "<br/>";
echo "line 217 Pricelb".$pricelb[$i];echo "<br/>";
echo 'Weight L. 219  weight[i]'. $weight[$i];echo "<br/>";
echo "line 220 weight".$weight; echo "<br/>";
echo "line 221 Numloops".$numloops; echo "<br/><br/>";
echo ' L. 225 $ordervalue  ' .$ordervalue;echo "<br/>";

}

//if ($pricelb[$i] == 0 ){													// conditonal statement  
//	$ordervalue =  $price[$i];
//	}



$sql ="
UPDATE confirmedorder
SET  weight='$weight[$i]', ordervalue='$ordervalue', confirmedorder.picking = 'finished', confirmedorder.sale = 'open'
WHERE prodID = '$prodid[$i]'
AND OrderID = '$orderid[$i]' ;
";

$this->db->query($sql); 

	}																	// !! End For Loop. !! 

Link to comment
Share on other sites

Thanks for the feedback :

 

I have checked the ending if the for loop and it does end where it ends.  Dreamweaver checks it for you by balancing braces .

 

the Update statement is inside the For loop so it will know what the offset $i is

 

I'm not sure what is implied by 'inconceivable' ?

 

Link to comment
Share on other sites

Assuming that the $_POST['prodid'] count is really WHAT you want to be using for loop ending value, moving the end loop tag so it surrounds the insert should fix the problem.  Note: what you had commented as the closing loop was the end of the function loop.

  <?php
  function finishedorder(){  
$prodname = $_POST['prodname'];
$prodid =$_POST['prodid'];
$quantity = $_POST['quantity'];
$pricelb = $_POST['pricelb']; 
$customerid = $_POST['customerid']; 
$price = $_POST['price']; 
$orderid = $_POST['orderid']; 
$weight	= $_POST['weight'];

	echo "Varuable WEight"; print_r($weight);	 echo"<br/>";
echo "line 196 Weight"; print_r ($_POST['weight']); echo"<br/>";
echo "line 192 PriceLB"; print_r ($_POST['pricelb']); echo "<br/>";
echo "line 193 Price"; print_r ($_POST['price']); echo "<br/>";
echo "line 194 ProdID"; print_r ($_POST['prodid']); echo "<br/>";
    echo "line 195 OrderID"; print_r ($_POST['orderid']); echo "<br/><br/>";


$numloops = count($_POST['prodid']); 
for ($i = 0; $i < $numloops ; $i++) {										//  !!! Start of the for loop !!! 

if (!isset($orderid[$i])) $orderid[$i] ='';
if (!isset($prodid[$i])) $prodid[$i] ='';
if (!isset($weight[$i])) $weight[$i] ='';		
if (!isset($pricelb[$i])) $pricelb[$i] ='';									// This only initialises the Price & Weight variable to avoid an offset error message.	
if (!isset($weight[$i])) $weight[$i] ='';	


$ordervalue = $pricelb[$i] * $weight[$i];

echo " L. 216 The number is " . $i . "<br />";
echo "line 221 ProdID".$prodid[$i];echo "<br/>";
echo "line 217 Pricelb".$pricelb[$i];echo "<br/>";
echo 'Weight L. 219  weight[i]'. $weight[$i];echo "<br/>";
echo "line 220 weight".$weight; echo "<br/>";
echo "line 221 Numloops".$numloops; echo "<br/><br/>";
echo ' L. 225 $ordervalue  ' .$ordervalue;echo "<br/>";



//if ($pricelb[$i] == 0 ){													// conditonal statement  
//	$ordervalue =  $price[$i];
//	}



$sql ="
UPDATE confirmedorder
SET  weight='$weight[$i]', ordervalue='$ordervalue', confirmedorder.picking = 'finished', confirmedorder.sale = 'open'
WHERE prodID = '$prodid[$i]'
AND OrderID = '$orderid[$i]' ;
";

$this->db->query($sql); 

	}																	// !! End For Loop. !! 
  }
?>

Link to comment
Share on other sites

If you tabulate your code properly, you will see your issue. I don't care what Dreamweaver says.

 

<?php

function finishedorder() {

$prodname = $_POST['prodname'];
$prodid = $_POST['prodid'];
$quantity = $_POST['quantity'];
$pricelb = $_POST['pricelb'];
$customerid = $_POST['customerid'];
$price = $_POST['price'];
$orderid = $_POST['orderid'];
$weight = $_POST['weight'];

echo "Varuable WEight";
print_r($weight);
echo"<br/>";

echo "line 196 Weight";
print_r($_POST['weight']);
echo"<br/>";

echo "line 192 PriceLB";
print_r($_POST['pricelb']);
echo "<br/>";

echo "line 193 Price";
print_r($_POST['price']);
echo "<br/>";

echo "line 194 ProdID";
print_r($_POST['prodid']);
echo "<br/>";

echo "line 195 OrderID";
print_r($_POST['orderid']);
echo "<br/><br/>";


$numloops = count($_POST['prodid']);
for ($i = 0; $i < $numloops; $i++) {
	if (!isset($orderid[$i]))
		$orderid[$i] = '';
	if (!isset($prodid[$i]))
		$prodid[$i] = '';
	if (!isset($weight[$i]))
		$weight[$i] = '';
	if (!isset($pricelb[$i]))
		$pricelb[$i] = '';
	if (!isset($weight[$i]))
		$weight[$i] = '';


	$ordervalue = $pricelb[$i] * $weight[$i];

	echo " L. 216 The number is " . $i . "<br />";
	echo "line 221 ProdID" . $prodid[$i];
	echo "<br/>";
	echo "line 217 Pricelb" . $pricelb[$i];
	echo "<br/>";
	echo 'Weight L. 219  weight[i]' . $weight[$i];
	echo "<br/>";
	echo "line 220 weight" . $weight;
	echo "<br/>";
	echo "line 221 Numloops" . $numloops;
	echo "<br/><br/>";
	echo ' L. 225 $ordervalue  ' . $ordervalue;
	echo "<br/>";
} // WOA THE FOR ENDS!

//if ($pricelb[$i] == 0 ){
	// $ordervalue =  $price[$i];
//}



$sql = "
UPDATE confirmedorder
SET  weight='$weight[$i]', ordervalue='$ordervalue', confirmedorder.picking = 'finished', confirmedorder.sale = 'open'
WHERE prodID = '$prodid[$i]'
AND OrderID = '$orderid[$i]' ;
";

$this->db->query($sql);
}

?>

 

As a student of PHP, you should uninstall Dreamweaver.

Link to comment
Share on other sites

Bugger me - I take it all back. You were right, I was indeed missing a brace at the end !!!! Thank you !

 

However,  it's almost working but not quite.  One of the WEIGHT variables is not in the correct field even though I specify a PRODID AND ORDERID. look at Prodid 101. It should say WEIGHT:7 instead the 7 is in the next row. To illustrate the problem here is a print of the table:

 

ID OrderID CustomerID ProdID Prodname Price Quantity Pricelb Weight OrderValue Picking Sale

151 JAC9506 5                 17           Ribs          60.00 1         5.00 6.00 30.00         finished open

152 JAC9506 5                 28           25 Piece Bag 15.00 1         0.00 0.00 15.00         finished open

153 JAC9506 5                 101           breasts         10.25 1         2.00 0.00 0.00         finished open

154 JAC6193 5                 27           Sirloin Steak 24.00 1         0.00 7.00 24.00         finished open

 

Link to comment
Share on other sites

What do you see when adding echo sql.  Does product 101 have weight of 7?

$sql ="
UPDATE confirmedorder
SET  weight='$weight[$i]', ordervalue='$ordervalue', confirmedorder.picking = 'finished', confirmedorder.sale = 'open'
WHERE prodID = '$prodid[$i]'
AND OrderID = '$orderid[$i]' ;
";
echo "$sql<br />";

 

Link to comment
Share on other sites

Ok - I ran the test, and the results are consistent with the MYSQL  print:

 

 

UPDATE confirmedorder SET  weight='0.00', ordervalue='0', confirmedorder.picking = 'finished', confirmedorder.sale = 'open' WHERE prodID = '101' AND OrderID = 'JAC9506' ; 

 

Thanks  for sticking with me on this !

 

btw - how do you get colored code on this site .. mine comes out in bw.

 

 

Link to comment
Share on other sites

Are all your form tags arrays? name='prodname[]' etc.  Seems like a simple foreach loop should handle this fine.  Btw. code with <?php ?> will have color.

 

ALSO is product id ever duplicated in the form?  Just wondering why you have count($_POST['prodid']). OK, I assume this is an array... Never mind.

Link to comment
Share on other sites

Sorry if I'm not seeing the big picture and how this code fits into what you have going, but it would seem to me that a simple foreach loop would grab the the "post array key" and using this you identify each item and make this update.  Again sorry if this is way off.

<?php
function finishedorder(){
foreach ($_POST['prodid'] as $arrkey => $prodid){
	$pricelb = $_POST['pricelb'][$arrkey];
	$price = $_POST['price'][$arrkey]; 
	$orderid = $_POST['orderid'][$arrkey]; 
	$weight	= $_POST['weight'][$arrkey];

	$ordervalue = $pricelb * $weight;

	$sql ="	UPDATE confirmedorder SET weight='$weight', ordervalue='$ordervalue', picking = 'finished', sale = 'open' WHERE prodID = '$prodid' AND OrderID = '$orderid'";
$this->db->query($sql);
}
}
?>

Link to comment
Share on other sites

Hi Drummin - thanks for this idea, also thank you very much for the generosity of your time and spirit. I am always amazed how some people get a kick out of patronizing posters whilst people like you are so generous. Where are you ? I'm in Canada.

 

 

I would love to change the approach. The reason why I chose a for loop is because my POST arrays come in looking like this:

 

 

Array ProdID:

 
Array
( [0] => 17 [1] => 28 [2] => 101 [3] => 27 )

 

 

a For loop allows me to increment the index like this: $prodid[$i].  If there is a way of incrementing the index, I'm not sure how to do that .. any ideas ?

 

 

Many thanks !

Link to comment
Share on other sites

You probably could sort() the list first then do the foreach loop.

<?php
function finishedorder(){
sort($_POST['prodid']);
foreach ($_POST['prodid'] as $arrkey => $prodid){
	$pricelb = $_POST['pricelb'][$arrkey];
	$price = $_POST['price'][$arrkey]; 
	$orderid = $_POST['orderid'][$arrkey]; 
	$weight	= $_POST['weight'][$arrkey];

	$ordervalue = $pricelb * $weight;

	$sql ="	UPDATE confirmedorder SET weight='$weight', ordervalue='$ordervalue', picking = 'finished', sale = 'open' WHERE prodID = '$prodid' AND OrderID = '$orderid'";
$this->db->query($sql);
}
}
?>

Link to comment
Share on other sites

Ok - then presumably $arrkey takes care of keeping all the different POST arrays synchronized ?  so for example

 

 

Prodid array([2] => 101) is correctly synchronized with Pricelb array([2] => 101) etc etc etc

 

correct ?

 

 

 

 

 

 

 

 

 

Link to comment
Share on other sites

heah Drummin - Thanks again for your code, however I'm not making it work properly.

 

 

The SQL Query works on the first pass - then it appears to fail without any PHP errors

 

 

One thing I have noticed is that for some reason the $arrkey and $prodid value is getting messed up when I compare it to the POST ARRAY.

 

 

Take for example the second UPDATE. $prodid 27 has an $arrkey of 2 yet when you look into the POST it has an array key of [3]

 

 

This might be causing the SQL query to fail ? NB - I have attached a PNG off the table so you can see how it is failing.

 

 

MANY THANKS !

 

 



line 196 WeightArray ( [0] => 6 [1] => 0.00 [2] => 0.00 [3] => 7 [4] => 0.00 [5] => 0.00 ) 
line 192 PriceLBArray ( [0] => 5.00 [1] => 0.00 [2] => 2.00 [3] => 0.00 ) 
line 193 PriceArray ( [0] => 60.00 [1] => 15.00 [2] => 10.25 [3] => 24.00 ) 
line 194 ProdIDArray ( [0] => 17 [1] => 28 [2] => 101 [3] => 27 ) 
line 195 OrderIDArray ( [0] => JAC9506 [1] => JAC9506 [2] => JAC9506 [3] => JAC6193 ) 

UPDATE confirmedorder SET weight='6', ordervalue='30', picking = 'finished', sale = 'open' WHERE prodID = '17' AND OrderID = 'JAC9506'
arrkey0
prodid 17
Pricelb5.00

UPDATE confirmedorder SET weight='0.00', ordervalue='0', picking = 'finished', sale = 'open' WHERE prodID = '27' AND OrderID = 'JAC9506'
arrkey1
prodid 27
Pricelb0.00

UPDATE confirmedorder SET weight='0.00', ordervalue='0', picking = 'finished', sale = 'open' WHERE prodID = '28' AND OrderID = 'JAC9506'
arrkey2
prodid 28
Pricelb2.00

UPDATE confirmedorder SET weight='7', ordervalue='0', picking = 'finished', sale = 'open' WHERE prodID = '101' AND OrderID = 'JAC6193'
arrkey3
prodid 101
Pricelb0.00


 

 

 

 

C:\Users\Vince\Desktop\Capture

post-80388-134824035106_thumb.png

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.