Skip to content
Advertisement

How to connect db ID to a db Value in javascript?

So I got a function that executes every X minutes/hours, when it executes it will select a random database array value, when it does, the selected will get + 1. Selected stands for the amount of times the row has been selected.

JavaScript

This is my code from which I catch all of my database values

JavaScript

Lets say if we have Selected: 5, 4, 12 and 17. 17 is the highest selected count, to avoid it gaining more selected points, I ensure the highest numbers is removed from the array. Once the highest number is removed, I shuffle the numbers and it will select a random one.

JavaScript

Lets say from the shuffle I get 5. I now have to add + 1 to this, so 5 must become 6.

In function hi() I have an If statement, everyday at 8 am this code has to be executed. So as I said, I have to update the selected value.

Here comes the question: Usually I would do UPDATE developers SET selected = 6 WHERE id = 2. but since I have multiple rows this wont work. Today It might be Timbie, tommorow It can be Tim. How do I link the selected values with their IDs?

JavaScript

Advertisement

Answer

In the function removeMax(), the data is reduced to become just a list of the selected values. You would need to keep each item, not only the number of times it is selected.

Original code:

JavaScript

Original result:

JavaScript

After that, you are removing the largest value. Notice how all the id’s are gone?

If you instead do:

JavaScript

Now that you have an array of objects, you can rearrange them as before, and afterwards access the # of selected with item.selected, and each id with item.id.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement