Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何最好地使用数据库中的事件填充日历_Javascript_Jquery - Fatal编程技术网

Javascript 如何最好地使用数据库中的事件填充日历

Javascript 如何最好地使用数据库中的事件填充日历,javascript,jquery,Javascript,Jquery,我正在从头开始创建日历,但不知道如何使用数据库中的生日事件填充它。 我不知道如何将事件数据放入创建日历日的循环中,或者即使这是正确的顺序,我也不知道该怎么做 $.post('fetch-event.php', function(result){ let days = ""; currentMonth = date.getMonth() +1; // days of the current month and finds today's dat

我正在从头开始创建日历,但不知道如何使用数据库中的生日事件填充它。 我不知道如何将事件数据放入创建日历日的循环中,或者即使这是正确的顺序,我也不知道该怎么做

$.post('fetch-event.php', function(result){  
    let days = "";
    currentMonth = date.getMonth() +1;  

    // days of the current month and finds today's date
    for(let i = 1; i<=lastDay; i++){
        if(i === new Date().getDate() && date.getMonth() === new Date().getMonth()){
            days += `<div class="today" id="${i}/${currentMonth}/${year}">${i}</div>`;
        } else{
            days += `<div id="${i}/${currentMonth}/${year}">${i}</div>`;
        }
    }
    monthDays.innerHTML = days;
});

理想情况下,如果div的id日期与数据库中的日期匹配,我想用数据库中的人名而不是天数来填充div,但这有点超出了我的技术知识。

您可以使用
findIndex
如果找到匹配项,这将返回数组的索引值,然后使用此
索引
值从JSON数组中提取名称,即:
结果[index].name
然后将其添加到您的div中

演示代码

let days=”“;
日期=新日期();
currentMonth=date.getMonth()+1;
年份=日期。getFullYear()
让monthDays=document.getElementById(“monthDays”)
var结果=[{
“id”:“1”,
“姓名”:“艾略特”,
“日期”:“2021年1月31日”
},
{
“id”:“2”,
“姓名”:“菲尔”,
“日期”:“2021年1月17日”
},
{
“id”:“3”,
“姓名”:“托比”,
“日期”:“2021年1月25日”
},
{
“id”:“4”,
“名称”:“Abc”,
“日期”:“2021年10月1日”
}
]
//当前月份的天,并查找今天的日期

对于(让i=1;我可以让您发布日历的全部代码(html/js,mount div)
[{"id":"1","name":"Elliot","date":"31/12/2020"},
 {"id":"2","name":"Phil","date":"17/1/2020"},
 {"id":"3","name":"Toby","date":"25/1/2021"}]