'Random Page' button connected to database

Hi!

So I’m trying to create a ‘random page’ or ‘shuffle’ button for my site, extremely similar to Wikipedia’s “random article” link. I’ve made a database of URLs of the pages that I want to be selected from, together with an extra column denoting each with a unique number. However, I’m not sure how I can A) connect a button to this data and B) use a random number generator to select a page from my site out of the database at random via its assigned number.

I’m assuming this is a very easy thing to do, but the Wix ‘connect data’ feature is flipping confusing. I’d appreciate any help - thanks! :slight_smile:

Get your records into an array like you do when you get results.items. Then add this function to your code.

function shuffle(a) {
    var j, x, i;
    for (i = a.length - 1; i > 0; i--) {
        j = Math.floor(Math.random() * (i + 1));
        x = a[i];
        a[i] = a[j];
        a[j] = x;
    }
    return a;
}
// Then
let records = shuffle(results.items);
// The items in records will now be shuffled