I'm stuck trying to create an app that references a database of what people prefer out of two random options

I am trying to create an app where people can select their preference out of two random options from a table I’ve created in Airtable.

I’ve created a design that has a “Vote Option A” and “Vote Option B” button where it pulls a random item out of the OptionA column and random item out of the Option B column in the table.

I have used the below blocks and have successfully managed to get the random items to come out of the table and appear in the “OptionALabel” and OptionBLabel" items. The blocks shown here also allow the user to click on one of the items and for the table to update. However, I can’t figure out how to manage the data and make it only update if a user votes for Cat vs Cakes for example, as those could be the items that are pulled from the table.

What I’m ultimately trying to achieve is for someone to vote for one random item vs another random item, but then the app to display what percentage of people voted for the specific Option A vs the specific Option B. So, if the app randomly pulls out Cat as option A and then Cakes and option B, for instance, and the user votes for Cat, the data would know how many people voted for Cat in preference to Cakes and will show the percentages that each option had vote for it i.e. the percentage that voted for Cat vs Cakes and vice versa.

Can anyone help figure out how best to structure the data and in turn the blocks? Thanks!

Your data structure isn’t going to work.* You’ve aligned certain values such as Cat and Football, Dog and Hockey, etc. but because they are going to be random, you need a way to keep track of each value separately rather than storing them on a single row like that.

Your data source should look like this:

        Option    Votes
1    Cat            1
2    Dog           0
3    Horse        0
50  Football      3
51  Hockey       0
52  Cakes         0

Each option has its own row with a column for tallied votes. When you want to offer a random animal, you choose a number from 1 to [the last row containing an animal] and use that as the row id in the Get Value block. When you want to offer a random sport, you choose a number from 50 to [the last row containing a sport] and use that as the row id in the Get Value block.

When a user clicks to vote, if they press the button for Option A, you update the Votes column in the row # corresponding to the random animal row number. If they press the button for Option B, you update the Votes column in the row # corresponding to the random sports number.

*I mean, I guess it could work. It just would bug me to have a data source set up like that where things appear to have a connection but actually don’t. But there is a way to keep the format you have. When you pick a random animal row, store that in a variable called OptionA row number. When you pick a random sports row, store that in a variable called OptionB row number. Then, when getting the cell values, use the OptionA column with the OptionA row number variable and the OptionB column with the OptionB row number variable. Similarly, when recording votes, depending on the button pressed either store the value in the VotesForA column or the VotesForB column

Thanks for the response. I really appreciate it. I know my structure couldn’t really work but couldn’t figure out how to it.

In your suggested change, would this show me how many people preferred cats over hockey, for instance, by knowing how people had voted before in that particular face-off? If i do it where it just tallies votes for cats, that will only show me how many people voted cat in total wouldn’t it, no matter what cat was against? Apologies if I’ve misunderstood your suggestion.

Yes, you’re correct. I guess I wasn’t clear on what you were trying to tally. I thought you wanted to compare sports (or animals) but I see you’re wanting to compare cats to hockey. It’s more complicated to keep track of votes for each option in each possible pairing (cats vs. football, cats vs. hockey, cats vs. cakes, etc.).

I would probably do this in Firebase because you can create the data structure you need. But that’s a steep learning curve if you haven’t used it before. In Firebase, you could create a key name whenever a new combination is created (cat vs. hockey) and store the winning option and vote count. Then, when the next combination is created, you search all existing combinations and if you find the same one, you update the vote count. If you don’t find it, you repeat the process of creating a key name for it, etc.

Someone else might have a suggestion for how to do this in Google Sheets but I don’t. I don’t think it’s really set up for that type of data.

Thanks. The way you describe doing it with Firebase sounds like it would definitely work. I’m pet sitter I wouldn’t be able to build that myself though. Any other suggestions or even if there is a Firebase guide that would help would be extremely welcome.

Plan to spend 5-10 hours figuring out how Firebase works and getting your database set up and synced to Thunkable.

If that sounds like something you’re up for then dive in! There is no single guide to using Firebase with Thunkable although like other things in Thunkable, if you Google firebase Thunkable you’ll find documentation, forum posts, and video tutorials. But you’re going to be piecing things together from different sources and doing a lot of trial and error.

1 Like

@rosstaylor1b96fv3 If I understand correctly you are trying to build something like this:

@ioannis The problem with that is that the row 1 values are always going to be cat vs. football. So every time the random value is 1, the pairing will be cat vs. football. Correct me if I’m wrong, @rosstaylor1b96fv3, but I think what you’re wanting is a random animal paired with a random sport. So for example, cat vs. football one time then cat vs. hockey the next, etc. As I said, if that’s the case then the data structure is not ideal. But you can still use it… you just have to get two random rows: one for the OptionA column and one for the OptionB column.

@tatiang I see,
I thought he is going to create all the possible combinations in advance manually

1 Like

@tatiang You are correct. I am trying to keep a record of every possible combination of OptionA vs OptionB and also in a way that more items that can be added. As an example flow for the app, this is how I see it working:

  1. App presents random OptionA and random OptionB, let’s say it pulls out Buses vs Libraries
  2. User chooses OptionA
  3. App looks up the data source and finds if there is a record of that particular OptionA vs OptionB (Buses vs Libraries)
  4. If there is a history of Buses vs Libraries, it adds the user’s vote and shows the percentage results of what people preferred
  5. If there is no history of Buses vs Libraries, it adds a record for that automatically into the data source for future events

@ioannis I thought about having a table that has all the different permutations, but the plan is to have hundreds of options so it wouldn’t really be possible.

It feels like I may need two data sources - one for the items to be chosen at random and one for the OptionA vs Option B data maybe?

Another complication is that it would be great if the app could recognise “Buses vs Libraries” as being the same condition as “Libraries vs Buses”, in case the same items are pulled at random but with Buses as OptionB rather than OptionA. So, if 77% preferred Buses, it would show OptionA as being preferred by 77% where Buses is OptionA, but OptionB as 77% where Buses was pulled out as optionB.

I hope this makes sense. I really do appreciate the guidance.

Hello @rosstaylor1b96fv3
I suggest you have two tables.
One with all the options and the other one that will have the combinations and the votes.
So the first function will be to get two options random from the first table and then check if this combination exists on the second table so that you can add the vote, if not, you will create this combination and then add the vote.
Could you please try it and let us know if you have any questions going forward?

1 Like

Thanks @ioannis , yes, this is exactly what I would like to do. I know how to do the first part i.e. getting a random item from table 1, but I don’t know how to create and manage entries into the second table i.e. creating the combinations and then subsequently have the app check for the existing combinations.

Any help anyone can give on the second part would be great. Thanks again.

Hello @rosstaylor1b96fv3
Please check the screenshots below to help you understand how it works

It is one for each button, they look similar but they have small changes.

Please try it and let me know if you have further questions. I will be happy to help.

Thank you @ioannis . I have tried to do what you proposed but I’m not sure if I am setting things up in a compatible way. I have created two Airtable tables - one that contains the list of OptionA items and the list of Option B items (table is called “ThisOrThat”):

I have also created the combinations table that you suggested (called Combinations1)that contains the following:

My design is very simple with just two buttons that reference the ThisOrThat table:

I have created the blocks following your instructions but, again, I’m not sure how to set up the data to make sure it is all compatible. I’ve noticed the part in your block design that says “RandomOptionA” for example - does this mean I do not need the ThisOrThat table? I’ll post my blocks in the next message.

I’m ultimately trying to display a percentage preference on the screen once the user has chosen their option, based on all previous votes between those two items.

Thanks again for all your help.

My blocks:

@rosstaylor1b96fv3 I am suggesting using a variable to save each option and then update the label

On each button, you will need also to add a variable, a math block to calculate the percentage,
and an alert to show it.
Then, please update the variable for RandomOptionA and RandomOptionB

Could you please try it?

Hi. I’ve made some changes to the way the percentage is handled, by updating my airtable source to the following:

I have added two fields that handle the percentage calculation based on the updated VotesForA and VotesForB fields. The idea is that the text of the labels I have introduced would display the text from the field PercentA and PercentB. Here is my design:

In terms of the functionality, i have changed my blocks also (I’ll upload the blocks in the following message).

Mostly, the functionality and logic seems to be working fine in terms of the votes being updated and new entries into the table being created where the combination does not already exist. However, sometimes the item that is inserted into the Combinations1 table as OptionA or OptionB is not the value that has been presented in the app. This seems to be pretty random. For example, the preview might show OptionA as Football and OptionB as “Cakes” but when I select one, the items inserted into the Combinations1 table might be something like Football and Clouds instead. I really can’t figure out why it is OK sometimes and then sometimes it just enters a value that doesn’t match.

The label behaviour is working a bit erratically too, with the label text values not updating on the first click. It usually updates the second time I click a button and then won’t update again even if the value in the PercentA and PercentB fields update successfully.

It would also be great if I could find a way to make the app force the user to press the “Another” button once they have voted for an option. I don’t want them to be able to vote multiple times on the same combination.

Also (just one more thing!), is there a way that I can expand the way it grabs random items from the ThisOrThat table to cover all of the rows in the table, not just “random integers from row 1 to row X”. The table could end up growing in terms of rows and it would be good to always have it cover all the rows that contain a value.

Thanks so much for any help! I’ll show my blocks in the next message.

Here are my blocks. I don’t think this is too far away but I’m not an expert and there are probably some things in here that are causing the erratic behaviours described in the previous reply.

Update - I’ve managed to update the code so that it will grab a random item from all rows of the table, not just the specified random integer rows.

I still just can’t work out the weird behaviour regarding the text display from my labels though.

Which is the weird behavior? Could you please share more info?