Getting my head around arrays

So, I’ve been having a play about with arrays all day and I’m struggling to get my head around them.

I have a select which is populated from a db with a value. I’m then running this value as a server connect query to pull a specific record and then taking the data from this record to add to an array. For example value of select is say 10, which pulls out product, id, price etc and then clicking a button adds these as an array.

This seems to be working correctly and I’m getting the output of widget1,10,2.99

Worked through the tutorial at Working with Arrays just to make sure it was working as expected, and I’m getting a button for each item in the array e.g widget1,10,2.99 and widget2,11,3.99

So, my aim is to populate a table with this info. I’ve set up the table to reference array1.items. When add and item to the array it generates a new row. I have set one cell as $value and as expected each row is outputting the expected result of widget1,10,2.99 etc.

So, maybe I’m overthinking this a bit, but what should I be looking for to output each item into a specific cell e.g, cell 1 - product, cell 2 - id, cell 3- price. Do I need to relate this to some sort of index? Or is there anyway of purely taking the product id from the array, and querying this against the db to populate the table with all the required information on each row?

Help is appreciated!

Thanks

I am not sure that I understand the problem. You do not need an Array to populate a table from a database data source, all you need is a repeat region.

Here I have used Session Storage to populate the Array wCart Tutorial part 5. Add Session and Arrays to the Template. This should be similar when using a database data source.

Hi @ben. Thanks for the reply, and thank you very much for the tutorial. This was my starting point as I’m building a basket so went through the tutorial line by line, but I feel like I’m still missing something.

I have a select which populates from a DB. I’m looking at being able to add items from this select to a table with a quantity, but I’m struggling to get my head around this. Should I be using an array or just session storage to store these values?

Sorry, I’m not sure if that was clear as I don’t always know the technical terminology. In basic terms, I have a select which chooses a category e.g. widget types, which in turn queries a dB of widgets e.g widget 1, widget 2 in another select. Inna select each widget has a value say 1,2,3. So, I’m trying to build a cart where I can add a widget with the name, price, ref and the quantity selected.

Maybe I’m going about this the wrong way but I’m trying to add these values to an array and then populate the table with this array. Should I be doing this another way? Should querying the dB based on each widgets value, or populating the table using an array?

Sorry of this is a little confusing. I notice you mention session variables, if I use these can I not just store one value at a time? Its the basics of an array I’m needing to know,.and ultimately how to manipulate this data.

Thanks

As I understand it, you filter the database to display only the filtered items. This should be done without using Session and or Arrays.

When you display a grid of filtered items, you will have an add to cart button for each of those items. That’s where you can apply the Add to Cart functionality.

If you are having a problem displaying the filtered items, please have a look at https://docs.wappler.io/t/displaying-dynamic-data-on-your-pages/2871

Hi @ben thanks again for taking the time to reply. Sorry, I think I’ve over complicated the question. Forget about filtering the DB, I don’t have any issue with this. My issue is showing array values of say product,price,id as individual cells in a table.

Thanks

Craig

I won’t speak for Ben here butt I think his question is (mine too) why are you using an array to populate a table and not just the simple query?

1 Like

@brad - I was getting a bit confused between populating a table with basket data from @ben excellent wCart guides. In other words, just generally confused about the whole array and session storage setup.

Thanks again @ben, I’ve re-read through your guides again today and had a bit of a play about and got everything working as expected. Many thanks!

2 Likes