Multi-Select Inputs - How to Write an Array to the Database

So you may have an input that can have more than one value.

You can use the article from Wappler Docs to see how to create a nice interface for selecting the multiple values, which is linked to below.

You then end up with an array of values. But how do you write that array to your database, maybe along with other single dimensional data?

Let’s assume you have:

Following these simple (but not so obvious!) steps…

In your form:

  1. Create a hidden input in your form that will write all the data to your database.
    Set name = my_array_list
    Set Dynamic Attributes -> Value to be your array, so my_array.items
    This creates a text list of your array values separated by commas, so value1,value2,value3

In your Server Action:

  1. Create a $_POST variable called my_array_list.

  2. Insert a database Multi Insert action.
    This creates a repeat loop, inside of which is a single database insert.
    You want the loop to be repeated for each item in your array… which at the moment are in a comma separated text list in your $_POST.my_array_list variable.
    To do this you need to “split” your my_array_list into separate items for the repeat loop to work with.

  3. Set the repeat “Expression” to {{$_POST.my_array_list.split(",")}}
    Rather than typing all this in, you can use the graphical interface to select the my_array_list post variable then choose the Text- Split String function

  4. You can now reference your specific array value in the Database Insert action with $value.

Good luck!

Antony.

Community Page
Last updated: