Good morning all,
I’m looking to display data from a database on a homepage based on the actual date of the viewers location looking at it.
Basically it’s a historical fact that will display a “on this day in…” text each day.
Can this be done with script?
Apologies if this has been asked before, found a few entries here but nothing seemed to match.
Thanks in anticipation folks!
Hello Steve,
Yes you can, but only if your database has a valid date field, you can do it by running a query to get the items that match the current date, but this approach will have a long code, to make things easier, create two new fields in your database, one for the months (January, February, etc…), and one for the days (11, 29, 5, etc…), then query the database with the current month and day, I’ll show you how to get them.
First of all, get the current month and day:
import wixData from 'wix-data';
$w.onReady(async function () {
await getFact();
})
async function getFact() {
// get month and day
let today = new Date();
let month = today.toLocaleDateString('en-us', {month: 'long'})
let day = today.toLocaleDateString('en-us', {day: 'numeric'})
// get the matching fact
await wixData.query('facts')
.eq('month', month)
.eq('day', day)
.find()
.then((result) => {
let firstFact = result.items[0];
// Now show the fact in a text element
$w('#factTextElement').text = firstFact.title;
})
}
Hope that helped!
Ahmad
Hi Ahmad! Many thanks for your quick reply - very much appreciated! I think i’m getting a bit lost though, i have set up this edited code but it’s still not displaying on my main page in the names text box?
import wixData from 'OnThisDay';
$w.onReady(async function () {
await getFact();
})
async function getFact() {
// get month and day
let today = new Date();
let month = today.toLocaleDateString('en-us', {month: 'long'})
let day = today.toLocaleDateString('en-us', {day: 'numeric'})
// get the matching fact
await wixData.query('text')
.eq('month', month)
.eq('day', day)
.find()
.then((result) => {
let firstFact = result.items[0];
// Now show the fact in a text element
$w('#onthisday').text = firstFact.title;
})
}
I am probably failing to do something simple but my mind is fried at the moment! There are two columns in the dataset called ‘Month’ and ‘Day’ one is text and the other number. Is this correct? Thank you for your patience and help. Cheers, Steve
Do you get an undefined error? If so, please make sure that the field that holds the fact text (value) has its key named " title ", otherwise change this line to the field key of your database.
$w('#onthisday').text = firstFact.title;
Change title to the field key of the field that holds the fact.
Hi Ahmad,
Sorry, still having trouble with this - many thanks for your help! I have changed the code to show ‘text’ as this is the field it is in. Please see attached screenshots…
Thanks,
Steve
import wixData from 'onthisday';
$w.onReady(async function () {
await getFact();
})
async function getFact() {
// get month and day
let today = new Date();
let month = today.toLocaleDateString('en-us', {month: 'long'})
let day = today.toLocaleDateString('en-us', {day: 'numeric'})
// get the matching fact
await wixData.query('facts')
.eq('month', month)
.eq('day', day)
.find()
.then((result) => {
let firstFact = result.items[0];
// Now show the fact in a text element
$w('#onthisday').text = firstFact.text;
})
}
Hi Steve,
On your database, click on " Manage Fields " and head over to the " Text " field and click on the three dots next to it and click " Properties ", a window will open with a field that contain the ID of the field, screenshot that window and let me see what is the key.
Yes, the code should be:
$w('#onthisday').text = firstFact.text;
Sorry to be a pain - That’s what i have but there’s still no entry in the life site in that text box…?
Does there need to be a dynamic element on thAt page too…?
AH! SORRY!
Yes, it appears to be working now! Closed the editor and started again!
MANY THANKS for your time and help!
Regards,
Steve
The sabdbox database is different from the live site database.
You need to sync your live database with your sandbox .
You’re welcome, glad that I helped
Hi Ahmad,
Sorry, it appears to not be working (broken) again - im not sure what we have done.
Appreciate your help again please - ive included screenshots to hopefully help…
Is there meant to be a dataset linked to this?
Kind Regards,
Steve
Hi
Just change the day field type to “Text” instead of number, or just force the day value in the query to be a number like this:
await wixData.query('facts')
.eq('month', month)
.eq('day', Number(day))
.find()
So here are two options, go for the one you like.
Would you be able to take a look at the site for me please mate if i add you as contributor?
Can’t seem to get it to work - i’ve tried both option - i’ll gladly contribute to a tea fund!
Steve
Hey Steve
Sure, send me a message (links in my profile).
I’ve sent you a message on Facebook mate. Cheers.