Retrieving multiple variables from Airtable for the same random list entry

Hi all,

We are currently designing an app to display random photos from a database together with their location on a map.

We have managed to set up the Airtable/mediaDB database and to generate random photo display. However, the location on the map shown (latitude and longitude) is not linked to the randomly selected photo. The same location is retrieved each time.

It seems that the latitude and longitude in the Thunkable block is always set to 1, while the photo is updated randomly with each click. We somehow need to access the latitude and longitude that is attached to the photo. So three records for the same Airtable.

Below the current blocks.

Thanks for any help you may be able to provide!
Tom

1 Like

You’re choosing a random list item from the Photos list but then getting the first list item of the Lat and Long lists:

Did you create the blocks in your screenshot? I only ask because it’s your first time posting and it sounds like you might not understand how all of these blocks work. I can explain more if you need it but if you designed the blocks and understand them but just missed the two "1"s then read on…

To fix this, instead of getting a random list item, first set a variable’s value to a random integer from 1 to the length of the Photos list. Then, use that variable value as the list item number for the Photos list, the Lat list and the Long list.

3 Likes

Just want to add another comment:

Use this suggestion from @tatiang outside the for loop. Currently, you are selecting a random photo each time you loop and get the condition evaluated to true which is not something you want.

You should first loop through all the record to build the three lists with values matching your condition. One the loop is over then do @tatiang recommendation to view the photo with related coordinates.

2 Likes

Good catch! Yes, set the variable value and set the Picture, Latitude and Longitude after the loop. Thanks, @muneer.

2 Likes

@muneer @tatiang Thank you so much for being so helpful!

Yes, I am pretty new to thunkable, while I think I understand the previous blocks, probably there are things I actually don’t yet fully…

So I tried to follow what you suggested, but it still doesn’t seem to work unfortunately. Maybe I am not using the right variable (ListOfPhotos) as key setting it to a random integer? Is it correct to build three separate lists for photos, longitude and latitude, or should I build one single list? Below the changes I have made. What am I still doing wrong?

Thanks again!

1 Like

You are using the same variable used for the List Of Photos as a variable to store the random number. You need to create a different variable for this purpose and use it subsequently to select the row.

You are using the List Block where you should not. These variables are already initialized as lists therefore remove the block of the list with the gear icon.

image

2 Likes

Thanks so much, Muneer. I have made the fixes. Certainly looks more logical and simpler that way!

Do I need to link the list of keys to the Airtable as well and then loop through it as for the other variables? The Airtable includes a primary column with automatically generated integers. Or do I simply set that variable to a list of numbers from 1 to x in the blocks part?

It still doesn’t work unfortunately. It returns an error when I test it.

1 Like

You’ve done the same thing again @t.radinger4hmo - only this time you’re using the ListofKeys variable.

You need two vars to do this successfully, one to hold your random number and then the list of keys or photos then that you can measure the length of

1 Like

Your next step should be to add a block that says initialize app variable random number. Leave the right side empty (you don’t have to give it a starting value).

Then, after the loop, where you are getting a random integer, use a block that says set app variable random number to and connect it to the random integer block. Finally, use app random number for the get # value in each of the in list blocks.

1 Like

Thank you @domhnallohanlon, @tatiang, and muneer. I have made it work now! The functionality works perfectly and retrieves a photo together with its location on the map. Really wonderful to have this problem solved now!

2 Likes

That’s great! And thanks for posting a screenshot of the solution so other people can benefit from it.

2 Likes

Sorry, I just tried to recreate the solution for a similar problem, displaying only a random photo with its description. The user should click on the button to generate a new random photo and label.

It all works fine, except that the photo gets stuck on the random number generated when the screen opens. Clicking on the button does not generate a new photo and label.

When I simply change to for example “random integer from 1 to 30” instead of “app variable ListAllRows1”, it works. So somehow there is a problem with the list. Do I need to create a new one?

Thanks again!

If you assign the length of list app variable listAllRows1 to a label’s text when clicking Button1, what is the value you see? Is it 30? Is it something else?

Do you know how to create a function in Thunkable? If so, move these blocks from the when Slides Opens section to a function and then call the function where you have those blocks in the when Slides Opens section and the when Button1 Click section (that is, replace those blocks with the function call) and see if that makes a difference:

1 Like

I checked in the database, and the random number always seems to be 1 - the first image and label are displayed.

I tried to create the function separately and to call it in each block, but it doesn’t seem to make a difference unfortunately.

If I change it as follows, it works…

I missed this before but you need to remove the list block from this set of blocks:

This is a common mistake. app variable ListOfPhotos1 is already a list. You don’t need to add the [gear]list block to it. The problem is that you are getting the length of a list with ONE item. That item is app variable ListOfPhotos1. So you’re basically telling Thunkable that you want the length of a list of a list. Anyway, remove the gear/list block and it should work fine.

1 Like

Awesome, @tatiang!

I actually just tried removing the list block, and yes it works! Brilliant.

Thanks again for all your help, and with the function the blocks look much cleaner!

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.