Hi All,
I want to do 24 questions DISC personality test but I do not know how to filter value in multiple database fields and sum all value example D: ? I:? S:? C:?. I’d like to assign a value to D I S C . After that I don’t what I need to use statement or query. However I’m feeling a bit lost, I’m sure the answer is simple but sometimes the documentation can feel a bit vague on a case by case basis, so please help me coding on dynamic page.
maybe use multiple .eq () functions to find exactly the database line with the result
wixData.query('DatabaseName')
.eq('databaseField',comparisonValue)
.eq('databaseField',comparisonValue)
.eq('databaseField',comparisonValue)
.find()
.then((results)=>{
//here you can add what comes back from the database
})
Hi, but I did not get the correct value for my total score. How to filter and count the all same value in the database but different fields
import wixData from 'wix-data';
$w.onReady(function () {
wixData.query("DISC")
.eq("no1Q","D")
.eq("no2Q","D")
.eq("no3Q","D")
.eq("no4Q","D")
.eq("no5Q","D")
.eq("no6Q","D")
.eq("no7Q","D")
.eq("no8Q","D")
.eq("no9Q","D")
.eq("no10Q","D")
.eq("no11Q","D")
.eq("no12Q","D")
.eq("no13Q","D")
.eq("no14Q","D")
.eq("no15Q","D")
.eq("no16Q","D")
.eq("no17Q","D")
.eq("no18Q","D")
.eq("no19Q","D")
.eq("no20Q","D")
.eq("no21Q","D")
.eq("no22Q","D")
.eq("no23Q","D")
.eq("no24Q","D")
.count()
.then((results)=>{
$w('#sumD').text = results.toString()
})
});
Ah so if I understand correctly you need to understand how many times the user clicks D and the same for all the other letters, right?
Well to do this you would do much earlier to use the numeric fields in the database, so that each time one selects D it adds 1 to the item in the database and in the end you will not need to count the characters, but you will have the number.
As you have structured now the database is more complex, since the filters work by rows not by columns, so you looking for the user should retrieve all the letters that the user has chosen, put them inside an array with a loop and then count the elements inside the array with .lenght ()
@bigimatt14
Yaa, you right. But like you said about this “Well to do this you would do much earlier to use the numeric fields in the database, so that each time one selects D it adds 1 to the item in the database and in the end you will not need to count the characters, but you will have the number.” How can i do that? Can you give the example?
Ok, what you have to do is remove the columns no1q … and add 3 numeric ones, let’s say you want to call them responseD, responseI, responseS, responseC.
now let’s pretend that the user answers S to the first question, the code will be this:
wixData.query('DISC')
.eq('name', varWithTheName) //better to use the id
.limit(1)
.find()
.then((returns) => {
let item = returns.items[0];
item.responseS = item.responseS + 1;
wixData.update('DISC', item)
this is used to update the data already existing in the collection, so when the user answers row is created you must assign a default value of 0 to all answers.
So maybe you can make an ‘enter name’ field and a start test button that creates the row when pressed.
Also I recommend that you put this code in the backend of the site so as not to overload the client
or you create numeric variables that are updated at each answer and the data is saved at the last answer