HELP: arrays and datasets

ANY HELP IS FANTASTIC (I LOVE YOU IF YOU ANSWER) (PROJECT IS DUE TOMORROW)
I have some code so that the visitor clicks on a button and an element is taken off an array i have. this is what i want the code to do. but once you refresh the page, the array goes back to the original.
how can i make so that when you click on the button the array is permanently affected?
should i be using something else?
like a dataset?
if so, how should i use that dataset?
how do i take elements off datasets permanently?
PLEASE
THANKYOU

Hi Chris,

Can you explain what you mean by " the visitor clicks on a button and an element is taken off an array i have ."?

In general yes, databases are the way to save data and keep it in store.
You can use datasets in order to save/retrieve/update data to the certain database it is connected to.

Here are some articles that will help you better understand those methods:

About Database Collections
About Connecting Page Elements to Database Content

Please also explain what you mean by " how do i take elements off datasets permanently "
and what are you trying to achieve so we’ll be able to figure out the best solution for your case.

Doron.

thank you for answering

My family does a present exchange every year where we put all the names in a little basket and then each person pulls a name out and that’s who they give their present to, except they can’t be from their family.
So I made an array with all the names, and then they click on a button and a name comes up and then i ask them (and i trust them) if that person is from their family. if that person is, than they do it again, if they’re not, then i delete that person from the array.
So it works fine except that when you reload the page the array goes back to what it was.
is there a way that the array is permanently affected? is there an alternative?
thanks again
here’s my code (i speak spanish: familia=family; si=yes; no=no; the names are names; textcris is the textbox where the name appears)

var familia =
[“¡BABY!”,“¡RICK!”,“¡CARLOS!”,“¡MIRNA!”,“¡NORI!”,“¡ARTURO IPIENS!”,“¡MIGUE!”,“¡BEATRIZ!”,“¡TOTOY!”,“¡ANY!”,“¡GUILLERMO!”,“¡MARIA JULIA!”,“¡ALEJO!”,“¡PEPE!”,“¡BRIGIT!”,“¡HANNAH!”,“¡DIEGO!”,“¡GABY!”,“¡CARLANGAS!”,“¡MAGGIE!”,“¡CARLITITOS!”,“¡JOSE LUIS!”,“¡NANCY!”,“¡REGINA!”,“¡ALE!”,“¡PABLO!”,“¡JULIETA!”]
var rand = Math.floor(Math.random()*familia.length);
$w.onReady( function () { }

export function button1_click(event) {
$w(’ #textcris ‘).text = familia[rand];
$w(’ #asignar ‘).hide();
$w(’ #si ‘).show();
$w(’ #no ‘).show();
$w(’ #text1 ‘).show();
$w(’ #textcris ').show(); }

export function no_click(event) {
familia.splice(rand, 1);
$w(’ #si ‘).hide();
$w(’ #no ‘).hide();
$w(’ #text1 ‘).hide();
//$w(’ #textcris ‘).hide();
$w(’ #ok ').show(); }

export function si_click(event) {
rand = Math.floor(Math.random()*familia.length);
$w(’ #asignar ‘).show();
$w(’ #textcris ‘).hide();
$w(’ #si ‘).hide();
$w(’ #no ‘).hide();
$w(’ #text1 ').hide(); }

Hi Chris!

Really fun idea!
I’d suggest you to put all the names in a collection (each name is an item).
Then, use a query from your page to retrieve all the items as an array.
Use your function to draw a name out of the array (show it to everyone) and then - delete it from the collection using the remove() function.
Then draw again!

It should be something like that:

import wixData from 'wix-data';

let familia;
let rand;

$w.onReady(function () {

wixData.query("familia")
.find()
   .then( (results) => {
      familia = results.items;
   } );

rand = Math.round(Math.random()*familia.length);
 }
 
 export function drawButton_click(event) {
 //Apply your logic.
 //Draw item and show your message.
 }
 
 export function use_click(event) {
    wixData.remove("familia", familia[rand].id)
     .then( () => {
       wixData.query("familia")
       .find() 
       .then( (results) => {      
          familia = results.items; 
          rand = Math.round(Math.random()*familia.length);
          //+all your messages
          } );
  } )
 }
 
 export function dontUse_click(event) {
    rand = Math.round(Math.random()*familia.length)
    //+all your messages
 }

Let me know if it works for you and don’t forget to adjust all the field names and field keys.

Doron.

Wow! thank you! I’ll get back to you soon

Hi again and thank you so much!

I adapted the code you proposed an this is what it gave me:

import wixData from ‘wix-data’;

var familia = ;
let rand;

$w.onReady( function () {
wixData.query(“familia”)
.find()
.then((results) => {
familia = results.items;
});

rand = Math.floor(Math.random() * familia.length);

});

export function asignar_click(event) {
$w(‘#cris’).show();
$w(‘#cris’).text = familia[rand];
$w(‘#asignar’).hide();
$w(‘#si’).show();
$w(‘#no’).show();
$w(‘#esta’).show();
$w(‘#cris’).show();
}

export function no_click(event) {
wixData.remove(“familia”, familia[rand].id)
.then(() => {
wixData.query(“familia”)
.find()
.then((results) => {
familia = results.items;
rand = Math.round(Math.random() * familia.length);
});
})
$w(‘#ok’).show();
$w(‘#cris’).hide();
$w(‘#no’).hide();
$w(‘#si’).hide();
$w(‘#esta’).hide();
}

export function si_click(event) {
$w(‘#asignar’).show();
$w(‘#cris’).hide();
$w(‘#esta’).hide();
$w(‘#no’).hide();
$w(‘#si’).hide();
}

Up to now the problem I’m having is that it always gives the last item of the array for some reason.
Have I made some mistake?
Could you please point it out
Otherwise I can’t see any problems, but then again, the problem I was having before was that it wasn’t deleting.
I hope this works!
Thank you so much for your help!
Also, answer whenever, I really don’t need to finish immediatly after all (the sooner the better though)
Diego
(cris is the name I put with the email and everything because that’s what you call the person who’s getting a present in the exchange, like Kris Kringle in the U.S. i think)

O I figured out what that problem was: I had the text box connected to the dataset.
So I disconnected it but now it only says huge title (which is what the textbox says if you don’t edit it)
I can’t figure out what the problem is
Do you think that i’ve done the .text option for the textbox incorrectly?
Or maybe the array is not getting any information and so it doesn’t show anything?
Anything else I’m not finding?

Here’s my code again(I saw on another post that I could use the storage function somehow, I didn’t really understand how to use it, but I added it after I tried it without it):

import wixData from ‘wix-data’;
import {storage,local} from ‘wix-storage’;
var array = ;
let rand;

$w.onReady( function () {
wixData.query(“familia”)
.find()
.then((results) => {
array = results.items;
});

rand = Math.floor(Math.random() * array.length); 

local.setItem(1, rand);
});

export function asignar_click(event) {
$w(‘#cris’).show();
$w(‘#cris’).text = array[local.getItem(1)];
$w(‘#asignar’).hide();
$w(‘#si’).show();
$w(‘#no’).show();
$w(‘#esta’).show();
$w(‘#cris’).show();
}

export function no_click(event) {
wixData.remove(“familia”, array[local.getItem(1)].id)
.then(() => {
wixData.query(“familia”)
.find()
.then((results) => {
array = results.items;
rand = Math.round(Math.random() * array.length);
});
})
$w(‘#ok’).show();
$w(‘#cris’).hide();
$w(‘#no’).hide();
$w(‘#si’).hide();
$w(‘#esta’).hide();
}

export function si_click(event) {
$w(‘#asignar’).show();
$w(‘#cris’).hide();
$w(‘#esta’).hide();
$w(‘#no’).hide();
$w(‘#si’).hide();
rand = Math.floor(Math.random() * array.length);
local.setItem(1, rand);
}

Thank you

I think the second is more likely, btw

Abyone else want to answer?