Jump to content

Insert while looping with an increment help .... this is probably so simple....


Sir-lancealot

Recommended Posts

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 counter
while (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 ...
 
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.
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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.