***How do you use data from database to suggest a product

I have a quiz on my website that collects data from the users. How can I use that data to suggest one of 4 products. The only variable that I need to have set is gender and number of items they want. So if they are male and want 5 or more items, it suggest the product “male advanced” vs female and 5 or less items, “female basic”. I collected the male/female data from a button and number of items data from a slider.

Hello Nathan,

i do not really understand your issue and what you want to do, but i can show you how to get data out of a database…

You can do it by using a query…

import wixData from 'wix-data';
2
3// ...
4
5wixData.query("myCollection")
6  .find()
7  .then( (results) => {
8    if(results.items.length > 0) {
9      let firstItem = results.items[0]; //see item below
10    } else {
11      // handle case where no matching items found
12    }
13  } )
14  .catch( (err) => {
15    let errorMsg = err;
16  } );

…or you can use a dataset…

$w.onReady( () => {
2  $w("#myDataset").onReady( () => {
3    $w("#myDataset").getItems(3, 2)
4      .then( (result) => {
5        let items = result.items;
6        let totalCount = result.totalCount;
7        let offset = result.offset;
8      } )
9      .catch( (err) => {
10        let errMsg = err.message;
11        let errCode = err.code;
12      } );
13
14  } );
15
16} );

…and if you are using a dynamic page, then …

$w.onReady( () => {
2  $w("#myDataset").onReady( () => {
3    let itemObj = $w("#myDataset").getCurrentItem();
4
5  } );
6
7} );

So that’s how I get the data out of the data set. Now I need it to read the data and do like an if then type thing where if the data it pulls is of the numbers 1,2,3,4, or 5 AND also is the word male it will display the product that I have setup through wix as Male basic or if it says 6,7,8,9, or 10 and says the word male, it will display the product that I have setup for Male advanced. (the same for women). I have the products set up and I have data going into the database in a field for numbers (1,2,3,4,5,6,7,8,9,10) and for male/female. I just need a way to code whether or not it shoes male basic (variables: male AND 1,2,3,4, or 5), male advanced (variables: male AND 6,7,8,9, or 10), female basic (variables: female AND 1,2,3,4, or 5), or female advanced (variables: female AND 6,7,8,9, or 10). The field key for the numbers in the dataset is #numbers and the key for the male/female is #gender

Simple example of an if-query…

if (mySkills would be === good) {i would do better coding}
else {i musst learn more to get good enough}
let gender = "male" // change to "female" do again


if (gender === "male") {console.log("male = true")}
else {console.log("female")}

Yes, I understand how to do that. What I can’t figure out is how to make the product show up as the else. I know you can use a repeater to change each part of the product buy page but how should I get the product to show up?

Before you can show up some data in a repeater, you must have first the RESULTS, which should be showed up, right?

Do you have already a result-data?

Yes, I do. But I need a way to use the answer data to choose which result to display in the repeater.

So the way I think it needs to go is something like:

if #gender = male, and #number = 1,2,3,4, or 5
then display repeater data from dataset male basic

if #gender = male, and #number = 6,7,8,9 or 10
then display repeater data from dataset male advanced

if #gender = female, and #number = 1,2,3,4, or 5
then display repeater data from dataset female basic

if #gender = female, and #number = 6,7,8,9, or 10
then display repeater data from dataset female advanced

Your if-query, should be like this…

function myFunction(){
let GENDER ="female" // change to "male" run the code again and see results in the CONSOLE.
let NUMBER = 5

if (GENDER === "male" && NUMBER < 6) {
console.log("Male-BASIC-activated")
}
else {"Male-ADVANCED-activated"}

if (GENDER === "female" && NUMBER < 6) {
console.log("Female-BASIC-activated")
}
else {"Female-ADVANCED-activated"}
}

@russian-dima Ok so I think I found an easier way. I made a page for each of the plans. For the command, I would just need to link them to the page. Thanks for all your help!