Jump to content

Setting multidimensional arrays


Jalz

Recommended Posts

Hi all,

 

I've set up a form with inputs which I am going to pass on in arrays as I have up to 10 repetitions. Below I've got a snippet of code from two input fields. When I send the data across, the FirstName input text field I have seems to be fairly easy to process back into my database as the ouput I get is fairly simple. I'm having trouble with the Membership field which involves checkboxes. Each checkbox that gets submitted to my request page is already in an array, but I can't seem to work out which value belongs to which member. I was thinking of grouping the values together, so i'll have an array called Membership and in each key I will have another array of values passed on from my form. Hope that makes sense. Its so I can later work out Steve has membership only to apple.

 

I've out put under the code the output I am getting.

 

Thanks to all that respond

 

<li><input type="text" id="FirstName_<?php echo $portal ?>" name="FirstName[]" /></li>
<li><?php
    foreach(fmsValueListItems($OO_Secure,'Web_Tickets','Dietary Requirements',"") as $list_item_key=>$list_item) {
      if($list_item == "") {
        echo "<input id=\"Membership{$list_item_key}\" name=\"Membership{$list_item_key}[]\" type=\"checkbox\" value=\"{$list_item}\" checked=\"checked\">{$list_item}\n";
      } else {
        echo "<input id=\"Membership{$list_item_key}\" name=\"Membership{$list_item_key}[]\" type=\"checkbox\" value=\"{$list_item}\">{$list_item}\n";
      }
    }
    ?>
</li>

 

output using the [] in the name field

 

Array

(

    [FirstName] => Array

        (

            [0] => Steve

            [1] => Bill

            [2] => Edward

      )

 

    [Membership2] => Array

        (

            [0] => Microsoft

        )

 

    [Membership3] => Array

        (

            [0] => Apple

        )

    [Membership4] => Array

        (

            [0] => Oracle

        )

    [Membership5] => Array

        (

            [0] => Google

        )

)

Link to comment
Share on other sites

If you want

 

Membership[1]
Membership[2]

 

POSTed you should take out the ($list_item_key} of your echo under the name so that:

 

<li><input type="text" id="FirstName_<?php echo $portal ?>" name="FirstName[]" /></li>
<li><?php
    foreach(fmsValueListItems($OO_Secure,'Web_Tickets','Dietary Requirements',"") as $list_item_key=>$list_item) {
      if($list_item == "") {
        echo "<input id=\"Membership{$list_item_key}\" name=\"Membership[]\" type=\"checkbox\" value=\"{$list_item}\" checked=\"checked\">{$list_item}\n";
      } else {
        echo "<input id=\"Membership{$list_item_key}\" name=\"Membership[]\" type=\"checkbox\" value=\"{$list_item}\">{$list_item}\n";
      }
    }
    ?>
</li>

 

I think this works too:

 

<li><input type="text" id="FirstName_<?php echo $portal ?>" name="FirstName[]" /></li>
<li><?php
    foreach(fmsValueListItems($OO_Secure,'Web_Tickets','Dietary Requirements',"") as $list_item_key=>$list_item) {
      if($list_item == "") {
        echo "<input id=\"Membership{$list_item_key}\" name=\"Membership[{$list_item_key}]\" type=\"checkbox\" value=\"{$list_item}\" checked=\"checked\">{$list_item}\n";
      } else {
        echo "<input id=\"Membership{$list_item_key}\" name=\"Membership[{$list_item_key}]\" type=\"checkbox\" value=\"{$list_item}\">{$list_item}\n";
      }
    }
    ?>
</li>

Link to comment
Share on other sites

Hi Andrew,

 

Unfortunately, both snippets didnt make a difference (well they did, I've now got an array called Membership but the values from row one and two a coming in mixed). I'll try and explain what I have on my form, I have a input field called FirstName which is where I type in the firstname of a person. I then have some checkboxes beside this field, which contain upto 9 options. To get the 9 options to display, I've used an array like you have suggested below to display the checkboxes 

name=\"Membership[]\"

 

The problem is, lets say I have two rows of data being displayed , i,m using | character as the checkbox separator:                         

FirstName

Membership

Steve Jobs

Apple|CEO|iPad|iphone

Bill Gates

Microsoft|Office|Kin

When I submit my form, the values for Bill Gates get sent in the same array as the values for Steve Jobs. I would like to create an array called Membership and within that have another array for the values for Steve jobs and anolther array for the values of the second row Bill Gates.

 

If I can do this, I know when I am writing a record back to the database (I have the correct values each member has chosen in the checkboxes).

 

Hope that makes sense.

 

Thanks for your help

 

Jalz

Link to comment
Share on other sites

So your  original snippet was part of a loop?  Do you track any iteratives in it (such that x=0 for the first instance, and x increments).

 

If not put an $x++ before your closing curly-brace and an $x=0; before the loop.  Then, replace name=\"Membership[] with:

 

name=\"Membership{$x}[]\"

Link to comment
Share on other sites

Thanks andrewgauger,

 

That works perfectly.

 

On another issue, once I have added the data back to my database, I want to remove the value from the FirstName and  Membership array as I have a mixture of old records and new records I am posting. I've searched the web, and I rekon I should be using the unset($FirstName[$key]) command. I've tried this and it doesn't want to remove the value, is there another remove command I should be using to remove values from the the array.

 

Thanks for helping me get teh arrays in there in the first place, the search on google continues.

 

Jalz

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.