Drummin Posted May 11, 2012 Share Posted May 11, 2012 Wondering what else you have going on that might change things. As far as I can tell the array keys should be matching. Do you get mis-match when not using the sort()? Also why does you WeightArray show six keys ([5] being the last) when the others all have four? Quote Link to comment https://forums.phpfreaks.com/topic/262376-why-insert-fail-when-i-put-an-if-inside-a-for-loop/page/2/#findComment-1344836 Share on other sites More sharing options...
Drummin Posted May 11, 2012 Share Posted May 11, 2012 Probably would be a good idea to see your form to have a better understanding of what's being passed. Quote Link to comment https://forums.phpfreaks.com/topic/262376-why-insert-fail-when-i-put-an-if-inside-a-for-loop/page/2/#findComment-1344842 Share on other sites More sharing options...
vincej Posted May 11, 2012 Author Share Posted May 11, 2012 Ok - I took off the sort and it brought me back to yesterday where the first row completes perfectly. The non subsequent rows, the Weight value was on the wrong row as well as Order Value not being calculated. Here is the Form - You might wonder why I put a value of 0.00 within the 'hidden' Weight variable... that was to ensure that I would not get an empty string being posted and showing up as a empty on the arrays - resulting in everything getting out of synch. The form is produced with CodeIgniter which is just a MVC framework. it is a mystery to me why there are in some cases 5 values when there are 4 prodID's. Again, Many Thanks ! <?php // INPUT SCREEN FOR FREEZER STAFF TO APPLY WEIGHTS AND PICK PRODUCTS// echo "<br/><H2>Picking List for: ". $customerfullname->firstname." ". $customerfullname->lastname." </H2>"; // echo "<h3>Customer Number: ".$pickinglist['0']['CustomerID']."</h3><br/><br/>"; ?> <table width="100%" border="1" cellpadding="5"> <tr> <th scope="col">Prod. ID</th> <th scope="col">Quantity</th> <th scope="col">Name</th> <th scope="col">Price lb</th> <th scope="col">Weight</th> <th scope="col"> Aprox. Expected Total</th> </tr> <?php //print_r($pickinglist); echo form_open('admin/pos/finishedorder'); foreach ( $pickinglist as $value) { ?> <tr> <td><?php echo $value['ProdID'] ?></td> <td><?php echo $value['Quantity']?></td> <td><?php echo $value['Prodname']?></td> <td><?php if ($value['Pricelb'] =='0.00'){ echo "N/A";} else echo $value['Pricelb']; ?></td> <td width="20" ><?php if ($value['Pricelb'] !== '0.00') { $data = array( 'name' => 'weight[]', 'id' => 'weight', 'maxlength' => '6', 'size' => '10', ); echo form_input($data); } else echo "N/A"; ?></td> <td width="25" align="center"><?php $OrderValue = $value['Price'] * $value['Quantity']; echo(number_format($OrderValue,2));?></td> </tr> <?php echo form_hidden('weight[]', '0.00'); echo form_hidden('prodid[]', $value['ProdID']); echo form_hidden('prodname[]', $value['Prodname']); echo form_hidden('orderid[]', $value['OrderID']); echo form_hidden('pricelb[]', $value['Pricelb']); echo form_hidden('customerid', $value['CustomerID']); echo form_hidden('quantity[]',$value['Quantity']) ; echo form_hidden('price[]',$value['Price']); } ?> </table> <br /> <?php echo "<span class=submit_button>".form_submit('submit','Update Order and Send For Payment >>')."</span>"; echo form_close(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/262376-why-insert-fail-when-i-put-an-if-inside-a-for-loop/page/2/#findComment-1344845 Share on other sites More sharing options...
vincej Posted May 11, 2012 Author Share Posted May 11, 2012 Here is a png of the html form so that it might be a bit easier to visualise. Quote Link to comment https://forums.phpfreaks.com/topic/262376-why-insert-fail-when-i-put-an-if-inside-a-for-loop/page/2/#findComment-1344849 Share on other sites More sharing options...
Drummin Posted May 11, 2012 Share Posted May 11, 2012 Never seen coding like this before. See something new all the time. What happens if you comment out line 41 where it adds the input for $data. echo form_input($data); I would think there should be a better option for line 49 where you have weight 0.00 and adding the extra input with same name is messing with the array keys. Actually I got that backwards. Man I could see this if it was a regular form. Comment out the hidden weight line. What you need is ONE weight input field regardless of conditions like Pricelb etc. Still a little hard to see. Sorry Quote Link to comment https://forums.phpfreaks.com/topic/262376-why-insert-fail-when-i-put-an-if-inside-a-for-loop/page/2/#findComment-1344853 Share on other sites More sharing options...
Drummin Posted May 11, 2012 Share Posted May 11, 2012 What about adding the hidden weight to the N/A line. <?php if ($value['Pricelb'] !== '0.00') { $data = array( 'name' => 'weight[]', 'id' => 'weight', 'maxlength' => '6', 'size' => '10', ); echo form_input($data); }else{ echo form_hidden('weight[]', '0.00') "N/A"; } ?> Not sure if this logic also needs to apply to the Pricelb section as well. <?php if ($value['Pricelb'] =='0.00'){ echo form_hidden('pricelb[]', $value['Pricelb']) "N/A";} else echo $value['Pricelb']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/262376-why-insert-fail-when-i-put-an-if-inside-a-for-loop/page/2/#findComment-1344858 Share on other sites More sharing options...
vincej Posted May 11, 2012 Author Share Posted May 11, 2012 thanks Drummin - I appreciate that it might be a little hard to follow in Codeigniter so here is the source code uploaded as txt. Also I need that input_form() as it generates the text input field. I don't take your generosity for granted ! Thanks . 18344_.txt Quote Link to comment https://forums.phpfreaks.com/topic/262376-why-insert-fail-when-i-put-an-if-inside-a-for-loop/page/2/#findComment-1344870 Share on other sites More sharing options...
Drummin Posted May 11, 2012 Share Posted May 11, 2012 I believe fixing those extra duplicate input fields will fix the problem. There is also nesting issues with the start form tag and the hidden fields being outside <td> tags. This copy might be close. Not having codelighter makes it harder to see so this is untested. <?php // INPUT SCREEN FOR FREEZER STAFF TO APPLY WEIGHTS AND PICK PRODUCTS// echo "<br/><H2>Picking List for: ". $customerfullname->firstname." ". $customerfullname->lastname." </H2>"; // echo "<h3>Customer Number: ".$pickinglist['0']['CustomerID']."</h3><br/><br/>"; echo form_open('admin/pos/finishedorder'); ?> <table width="100%" border="1" cellpadding="5"> <tr> <th scope="col">Prod. ID</th> <th scope="col">Quantity</th> <th scope="col">Name</th> <th scope="col">Price lb</th> <th scope="col">Weight</th> <th scope="col"> Aprox. Expected Total</th> </tr> <?php //print_r($pickinglist); foreach ( $pickinglist as $value) { ?> <tr> <td><?php echo $value['ProdID'] ?></td> <td><?php echo $value['Quantity']?></td> <td><?php echo $value['Prodname']?></td> <td> <?php if ($value['Pricelb'] =='0.00'){ echo form_hidden('pricelb[]', $value['Pricelb']) "N/A";} else echo form_hidden('pricelb[]', $value['Pricelb']) $value['Pricelb']; ?></td> <td width="20" > <?php if ($value['Pricelb'] !== '0.00') { $data = array( 'name' => 'weight[]', 'id' => 'weight', 'maxlength' => '6', 'size' => '10', ); echo form_input($data); }else{ echo form_hidden('weight[]', '0.00') "N/A"; } ?></td> <td width="25" align="center"><?php $OrderValue = $value['Price'] * $value['Quantity']; echo(number_format($OrderValue,2));?> <?php //echo form_hidden('weight[]', '0.00'); echo form_hidden('prodid[]', $value['ProdID']); echo form_hidden('prodname[]', $value['Prodname']); echo form_hidden('orderid[]', $value['OrderID']); //echo form_hidden('pricelb[]', $value['Pricelb']); echo form_hidden('customerid', $value['CustomerID']); echo form_hidden('quantity[]',$value['Quantity']) ; echo form_hidden('price[]',$value['Price']); } ?> </td> </tr> </table> <br /> <?php echo "<span class=submit_button>".form_submit('submit','Update Order and Send For Payment >>')."</span>"; echo form_close(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/262376-why-insert-fail-when-i-put-an-if-inside-a-for-loop/page/2/#findComment-1344880 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.