0

The problem comes when I am echoing more than one order on a page and submitting it. This is my form:

<input name="checkOrder['.($row['ord_no']).'][ord_no]" value="'.($row['ord_no']).'" type="checkbox" class="checkOrder" id="checkOrder">
      <input type="hidden" name="checkOrder['.($row['ord_no']).'][item_seq_no]" value="'.$line_number.'" />
      <input type="hidden" name="checkOrder['.($row['ord_no']).'][item_no]" value="'.$item_number.'" /> 
      <input type="hidden" name="checkOrder['.($row['ord_no']).'][item_desc_1]" value="'.$item_description.'" /></input>

Right now my ouput is this:

Array ( [ord_no] => 1000007 [item_seq_no] => 1 [item_no] => 001 [item_desc_1] => item1 ) 
    Array ( [ord_no] => 1000008 [item_seq_no] => 1 [item_no] => 002 [item_desc_1] => item2 ) 
    Array ( [ord_no] => 1000000 [item_seq_no] => 1 [item_no] => 003 [item_desc_1] => item3 ) 
    Array ( [ord_no] => 1000001 [item_seq_no] => 1 [item_no] => 001 [item_desc_1] => item1 )

I would prefer this:

[checkOrder] => Array
(
        [ord_no] => 1000007
        (
            [item_seq_no] => 1 [item_no] => 001 [item_desc_1] => item1
        )
        [ord_no] => 1000008
        (
            [item_seq_no] => 1 [item_no] => 002 [item_desc_1] => item2
        )
        [ord_no] => 1000000
        (
            [item_seq_no] => 1 [item_no] => 003 [item_desc_1] => item3
        )
        [ord_no] => 1000001
        (
        [item_seq_no] => 1 [item_no] => 001 [item_desc_1] => item1
        )
)

So how do I change my form to create one big array rather than multiple or am I not thinking about this correctly. Right now I am inserting all of these that are checked into a table inside a foreach loop. Then sending one email outside my foreach. It is only inserting the first array order # into the email. When I actually need everyone that was checked. I believe with this other array I have in mind it will fix my problem. If you want more info please let me know.

Ryan
  • 303
  • 5
  • 16
  • Did you check for the function array_merge() ? – David Ansermot Jun 03 '16 at 15:03
  • No I am fairly new at this.I will look into that – Ryan Jun 03 '16 at 15:06
  • 1
    `Right now my ouput is this` isnt right!! Do you use foreach($checkOrder as $v) and print that?. Just try `var_dump($_POST);` and you get what u want. – JustOnUnderMillions Jun 03 '16 at 15:08
  • You can't, you can't assign two values to a key. In this instance you're trying to make the key `[ord_no]` contain two values, the order number and the array. You would need to have `[ord_no]` inside the array as well, or have the value of `[ord_no]` as the key. – Styphon Jun 03 '16 at 15:08
  • I see what you are saying. Is there a way to put [ord_no] inside the array, but keep them all tied together? I'm okay with my output right now on the first one besides the fact that they are all separate. – Ryan Jun 03 '16 at 15:14
  • I believe you may be looking for an array of arrays. – devlin carnate Jun 03 '16 at 15:16
  • @JustOnUnderMillions right now it looks like foreach ($_POST['checkOrder'] as $multi_order) { Are you saying to do foreach (var_dump($_POST['checkOrder'] as $multi_order)) { – Ryan Jun 03 '16 at 15:16
  • use ` foreach ($_POST['checkOrder'] as $ord_no => $multi_order)` and print that $ord_no. Thats the way it works. – JustOnUnderMillions Jun 03 '16 at 15:17
  • @devlincarnate That sounds right. any idea how to format my form to make that happen? – Ryan Jun 03 '16 at 15:17
  • @JustOnUnderMillions I don't know if you saw my last details but I need to save just the order numbers outside the foreach. Inside the foreach I am doing an insert statement that inserts ord_no, item_seq_no,item_no and item_desc_1.How do I do that? – Ryan Jun 03 '16 at 15:20
  • You Problem is the use of `type="checkbox"` for the `checkOrder['.($row['ord_no']).'][ord_no]` change it to `checkOrder['.($row['ord_no_checked']).'][ord_no]` and add an hidden - input for [ord_no] that moves the ord_no into the array – JustOnUnderMillions Jun 03 '16 at 15:20
  • Have you tried this? http://stackoverflow.com/questions/6072383/how-to-post-data-as-an-indexed-array-of-arrays-without-specifying-indexes – devlin carnate Jun 03 '16 at 15:22
  • @JustOnUnderMillions That sounds good, will try and checkback – Ryan Jun 03 '16 at 15:22

1 Answers1

0

Change your form to:

  <input name="checkOrder['.($row['ord_no']).'][checked]" value="" type="checkbox" class="checkOrder" id="checkOrder">
  <input type="hidden" name="checkOrder['.($row['ord_no']).'][ord_no]" value="'.($row['ord_no']).'" />
  <input type="hidden" name="checkOrder['.($row['ord_no']).'][item_seq_no]" value="'.$line_number.'" />
  <input type="hidden" name="checkOrder['.($row['ord_no']).'][item_no]" value="'.$item_number.'" /> 
  <input type="hidden" name="checkOrder['.($row['ord_no']).'][item_desc_1]" value="'.$item_description.'" /></input>

if you now var_dump($_POST); you got:

[checkOrder] => Array
(
        [1000007] => Array
        (
          [ord_no] => 1000007, [item_seq_no] => 1, [item_no] => 001, [item_desc_1] => item1, [checked]='on'
        )
        [1000008] => Array
        (
          [ord_no] => 1000008, [item_seq_no] => 1, [item_no] => 002, [item_desc_1] => item2, [checked]='on'
        )
        [1000000] => Array
        (
          [ord_no] => 100000, [item_seq_no] => 1, [item_no] => 003,[item_desc_1] => item3, [checked]='on'
        )
        [1000001] => Array
        (
          [ord_no] => 1000001, [item_seq_no] => 1, [item_no] => 001, [item_desc_1] => item1, [checked]='on'
        )
)

[checked]='on' is only there if you checked that in the Form.

Hope that helps.

I need to save just the order numbers outside the foreach

Then use array_keys($_POST['checkOrder'])and you got the ids.

But it thing you should use instead of

<input name="checkOrder['.($row['ord_no']).'][checked]" value="" type="checkbox" class="checkOrder" id="checkOrder">

this

<input name="checkedOrders[]" value="'.($row['ord_no'].'" type="checkbox" class="checkOrder" id="checkOrder">

And then in $_POST['checkedOrders'] are all the ids that where checked in the form.

The rest will be still the same.

JustOnUnderMillions
  • 3,741
  • 9
  • 12