top of page
Anchor 1
Last Updated on :
Sunday, 30 January 2022
Randomized Content from database in WIX Website

Randomized Content
Create a database with content to filter
If you don't have one already, create a database collection to save your information that will be auto randomized. (Learn how to create database)
In our example, we created a database collection called 'quote' with the following fields:
Announcement (Text field)
Author (Text field)
We made sure to adjust the database settings / permissions to 'Anyone' can view content.
Follow the Video tutorial:
Add the code to your page
import wixData from 'wix-data';
let randomActivate = true;
random;
$w.onReady(async function () {
 $w("#randomDataset").onReady(async () => {
 if(randomActivate);
  console.log("Activate random dataset.");
 let database = $w('#randomDataset').getTotalCount(); //This line gets the total count
 let mixon = Math.floor(Math.random() * database); //This line begins randomizing code
  $w("#randomDataset").setCurrentItemIndex(mixon)
   .then(() => {
 //You can use this section to do something else after the code finishes randomizing
    console.log("Randomizing is complete.");
 let file = $w("#randomDataset").getCurrentItem();
 let label = file.author; //This line gets the current author
    $w("#randomQuoteLabel").label = label; //This line labels our button
   });
 });
});
function random(items) {
 var settings = items.length, randomize, index;
 while (0 !== settings) {
    index = Math.floor(Math.random() * settings);
    settings -= 1;
    randomize = items[settings];
    items[settings] = items[index];
    items[settings] = randomize;
  }
 return items;
}bottom of page
