How to create array from database query?

Hi there,

I was wondering if anyone new the Wappler way of creating an array from a database query please? I have a query that I want to repeat through and as I make some decisions I then want to add those filtered results into a nested array. I think I can see how I would do that on the front end using however I cannot see anything similar on the backend. I was thinking essentially of an array push function in the server action. Thanks for any help.

You can apply the flatten formatter to the query expression, so that it creates an array of the selected values.

So the result is:

"test": [5,6,7]

Thanks @Teodor, I am not quite sure how I would then add that to an existing array. Sorry I didnt really explain myself well before.

I have a query, that brings back multiple results of product_name and amount, groupID.

I then set a repeat to cycle through each of those results.

I then have a condition check if its true I want to add that product into the array.

I then basically want to have a copy of that array with all of those products in there. Is that possible?

Are you sure you wanna filter the array using a repeat?
You can set conditions for the array using the Conditions tab in Database Query Builder.

Hi @nickneustroev thanks for your reply. I am already using the filter on the DB query before hand. However I am filtering the data further afterwards and I don’t want to perform a second query until I have gathered all the data.

Hi there @datguru… if I understand you correctly, then I would read the initial array into a data store which is held locally, and manipulate the data store elements after that.

I wrote a piece on how to do that a year or so ago… I can’t paste the link here as I’m on my phone, but it is called:

Using a Flow to Fill a Data Store from the Database

Best wishes,

Thanks @Antony apologies I should of mentioned that this is server side. Correct me if I’m wrong but I think the data store is app connect / front end?

Maybe just use the where filter for your repeat instead of condition. Then add a setvalue inside the repeat with the data you need. And after that apply the flatten formatter to the repeat, outputting the setvalue in an array.

Screenshot 2022-03-29 at 10.40.35

We run a query, repeat the query and filter it using the where formatter. Inside the repeat we take the filtered records IDs and then we output it in another setvalue as an array.

1 Like

Fantastic thanks @Teodor exactly what I was looking for your a star!

Sorry @Teodor one extra question. How do I make a multi dimension associative array in wappler? (please let me know if you would rather this be a new question it feels like it maybe should be!)

For example:

filtered_results[0][‘id’] = 1
filtered_results[0][‘amount’] = 2000
filtered_results[1]['id] = 2

Edit:Aha I got it. I can just output extra set values within the repeat. Thanks for all the help with this @Teodor

1 Like