Sir-lancealot Posted January 26, 2007 Share Posted January 26, 2007 Real quick one (I hope) I am trying to loop through a post with line items of a purchase order. Rather than inserting 10 lines which are on the form, I am checking for nothing in desc, stopping there , works fine.[color=blue]$ct = 1; // start the counterwhile (strcmp($_POST["desc$ct"], '')) {[/color]But I can't loop with that counter $ct as part of the name, example;[color=blue] $id = "INSERT INTO purchase_order_detail ( `qty`, `ucost`, `desc`)"; $id .= " VALUES ([color=red]$_POST[qty$ct][/color],[color=red]$_POST[ucost$ct][/color],[color=red]$_POST[desc$ct] [/color])";[/color] where the red text is obviously the problem.I get an expected ']' when trying, tried concat's like '$_POST[qty".$ct."]', with the same result.Any help is more than appreciated. Buddy said to look at eval() but it's rather overwhelming and some1 might have a quicker solution.Thanks ... Link to comment https://forums.phpfreaks.com/topic/35830-insert-while-looping-with-an-increment-help-this-is-probably-so-simple/ Share on other sites More sharing options...
c4onastick Posted January 26, 2007 Share Posted January 26, 2007 Have you tried posting to an array?[code]<html><form> <input name='desc[]' value='whatever_goes_here'> <input name='desc[]' value='whatever_goes_here2'> <input name='desc[]' value='whatever_goes_here3'> ...</form></html>[/code]Then you can access $_POST['desc'] as an array and just do a foreach. It works well for a bunch of checkboxes. Link to comment https://forums.phpfreaks.com/topic/35830-insert-while-looping-with-an-increment-help-this-is-probably-so-simple/#findComment-169875 Share on other sites More sharing options...
kenrbnsn Posted January 26, 2007 Share Posted January 26, 2007 The best solution would be to use arrays as the form names. Where now you probably have something like:[code]<input type="text" name="qty<?php echo $ct; ?>]">[/code]You can change it to[code]<input type="text" name="qty[<?php echo $ct; ?>]">[/code]Then in the processing script, you can do:[code]<?php$id = "INSERT INTO purchase_order_detail ( `qty`, `ucost`, `desc`) values ({$_POST['qty'][$ct]},{$_POST['qty'][$ct]},'{$_POST['qty'][$ct]}')";?>[/code]Ken Link to comment https://forums.phpfreaks.com/topic/35830-insert-while-looping-with-an-increment-help-this-is-probably-so-simple/#findComment-169878 Share on other sites More sharing options...
Sir-lancealot Posted January 26, 2007 Author Share Posted January 26, 2007 *sigh*, I knew it was something I was just overlooking, thanks for both replies so quickly.Regards,Lance Link to comment https://forums.phpfreaks.com/topic/35830-insert-while-looping-with-an-increment-help-this-is-probably-so-simple/#findComment-169914 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.