[SOLVED] Can I search for 1 random item?

Hi, I have a bunch of animated Gifs that I want to use in a loading screen (popup). The images are saved in the database in a thing called “Loading Animation”.

When the loading screen is called, I want it to randomly search for 1 image and display it. The easy way would be to have an image element with a dynamic source like Search for "Loading Animations":random item's image. But this requires to search for all images before picking only one. Not performance wise.

I could load all the images in a list and then randomly pick one, but since the popup is called when “Page is loaded”, doing a full search before that isn’t ideal.

Is there a way to make a request that returns just 1 record from the database (randomly of course)?

2 Likes

Hi Julienallard1,

I was able to do something similar using a Page is loaded event, and then setting a custom state. For the value I did a search for a thing, then set :random item’s Name (see the image below for details).

I hope that gets you where you need to be!

-Jon

You could maybe use random sorting on your search and just grab the first item.
image

I haven’t actually tried this so I don’t know if it’s better performance wise than selecting a random item.

Thanks guys for replying. Although…

@jon.laplante, your suggestion is exactly the one I described and trying to avoid since it first does a search for several records BEFORE picking one randomly.

@eli Samething here: The search returns several records in a random order before I can pick one…

Well, bugger… I thought that might work. My only other thought offhand would be to create a field on your Loading Animations thing called Use this one: yes/no and run a recursive API workflow randomly setting one to Yes and all the others to No. Then you could simply search for loading animations with constraints Use this one is ‘yes’.

I’m sure one of the coding geniuses around here have ideas. Good luck!

I think the search Search for Loading Animation’s: Item #25 will only load that item.

I just did a test by first loading that one item on page load, and then loading the rest of the list into a repeating group by a button press (the list has around 500 items). The RG took several seconds to load, making me think that the entire list was not loaded when the single item was fetched on page load.

So if you can randomize that number, it may get you the result without using too much capacity. I set up a List of Numbers with the Toolbox plugin, and picked a random number from that list (which is done client-side), and used that for the item # in the first search:

image

Would that work? I may be wrong about this method not loading the full list, I admit my method of checking it isn’t scientifically viable…

1 Like

If that won’t do, another solution I sometimes use when I need something to load really fast, is to save the info in one long text field, rather than in separate data types, and separate them with an unusual character such as |.

So, whenever you save your list of GIFS, you also create a text field that saves all the image URL’s in a long string, :join with |, and use the Textlist plugin from Utilities (bdk) to convert it to a list of URL’s from which you can pick a random entry.

In theory, if this list remains static, you could even copy/paste the entire string into the default value of a custom state. In that case you wouldn’t perform any search at all.

That is brilliant.

I did slightly different than you but followed the core idea. I made a string of URLs separated by a | character and saved it in a App Text (they load very fast) and used a regex expression to convert it into a list of urls and picked a random item. NO SEARCH!
41

Thank you very much!

1 Like

Awesome! Glad it worked, and clever to use App text.

Care to share your regex expression? I’m rusty at that, would love to see how you set it up.

Generate a list of text from a string using “|” as a separator: ([^|]*|)[^|]

*I wish I’d come up with this all by myself… Found on stack overflow and changed the separator character :yum:

3 Likes

Brilliant, thanks for sharing :slight_smile: