Forum Academy Marketplace Showcase Pricing Features

Associative Arrays?

Is there any way to create an associative array in Bubble?

Here is the use case.

I have a product, and I want to define the attributes which can be defined in each SKU (color, size, etc.).

When a SKU is created, I want to show a list of the attributes associated with the product, and allow the user to fill in the values for each attributed. (i.e. Color: Blue, Size: Medium,…)

It would make for a better UI, because right now, the only solution I can think of is 2 inputs “key” and “value” on the SKU, but that leaves room for errors when a user forgets what attributes should be created on a SKU.

Hi there,

The way I would probably approach this is to have a ‘Thing’ that is ‘Product SKU’ which has a field - Product (Type Product) a SKU Attribute (Text) and attribute values (list of values - text). You may also want to have some other meta related data per attribute to describe it (such as ‘cm’ for size)

Thing:
Product SKU

Fields:
Product - Type Product
SKU Attribute - Type Text
SKU Values - Type Text - Multiple Values

Table should look something like this

Product | SKU Attributes | Values
Shirt | Colour | Red , Blue, Green, Orange
Shirt | Size | Small, Medium, Large

With these values associated to a product, you can search for SKU attributes and create a repeating group that returns the SKU Attribute per cell as a text object (so the user does not need to select it) and drop down value (populated from the values list).
The user can then select a value from the drop down of available values.

On submit, take those values and write them to what ever table you need to.

One thing I would probably also do is create a form that allows me to populate the SKU Attributes table and in the workflow I would also write each SKU Attribute instances into a list on the product so you don’t actually have to set search criteria, you can simply return the list of SKU Attributes in the repeating group.

Hope that makes sense.

1 Like

Thanks for your response.

Here is how I solved this…

I create a new

Thing:
Attribute

Fields:
Key - text
Product - Product
SKU - SKU
Value - text

I have the form, which you suggested on the product. The user enter’s the text for the Key. When the text is entered I modify/create a new attributed assigned to that product. I also add it to a list of attributes on the product.

When a SKU is created, I map a repeating group with the Product’s attributes. Inside of the repeating group I map the Key’s text, next to an input. The input’s initial content is a “Search for: Attributes:first item” with constraints matching the key & SKU of the current cell and parent group’s SKU (saved in a custom state). When the input is changed I modify/create an attribute whose key, value, and SKU match the current cell.

Hopefully that makes sense. Probably useless for most, but I figured I would post my solution nonetheless.

Thanks for the help!

1 Like

@csblack @lyndon.apthorpe

Wondering if you could help me out with something similar.

I have a thing; Products with attributes (name, description, price, image)
I want to first associate a product with user, then display product attributes to user (not necessarily in an array though).

How do I go about this?

Thanks!

There are a number of ways you can do this but it depends mostly on the user interactions flows and the context of the association. I’m going to assume the use case is something like a user views a product catalog and when the user clicks a product to view it, you want to associate at that point.
The simplest and most efficient way to do this is to have a field ‘Products Viewed’ of type Product on the User table (ticked to store multiple values).
When the user clicks a product, in the workflow, have a step that updates the current user and adds that product to the Products Viewed list on that user.
This is what creates your association and now you can use that list in repeating groups for that user without having to run a search to retrieve the results. This is the most efficient way to create and retrieve these associations.

Hope that helps