I have a javascript calendar and I want to make it so when a event is added to a mysql table it can be seen on the calendar. I know you can't put SELECT and other mysql stuff in a script so how can I do this?
HTML:
<div id="main" class="container">
<span class="jumbotron">
<h1 class="text-center">
<a id="left" href="#">
<i class="fas fa-chevron-left"></i>
</a>
<span id="month"></span>
<span id="year"></span>
<a id="right" href="#">
<i class="fas fa-chevron-right"></i>
</a>
</h1>
</span>
<span class="row">
<span class="col-sm-10 col-sm-offset-1"></span>
</span>
<table class="table"></table>
</div>
JAVASCRIPT for calendar:
$(document).ready(function() {
var currentDate = new Date();
function generateCalendar(d) {
function monthDays(month, year) {
var result = [];
var days = new Date(year, month, 0).getDate();
for (var i = 1; i <= days; i++) {
result.push(i);
}
return result;
}
Date.prototype.monthDays = function() {
var d = new Date(this.getFullYear(), this.getMonth() + 1, 0);
return d.getDate();
};
var details = {
// totalDays: monthDays(d.getMonth(), d.getFullYear()),
totalDays: d.monthDays(),
weekDays: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
months: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
};
var start = new Date(d.getFullYear(), d.getMonth()).getDay();
var cal = [];
var day = 1;
for (var i = 0; i <= 6; i++) {
cal.push(['<tr>']);
for (var j = 0; j < 7; j++) {
if (i === 0) {
cal[i].push('<td>' + details.weekDays[j] + '</td>');
} else if (day > details.totalDays) {
cal[i].push('<td> </td>');
} else {
if (i === 1 && j < start) {
cal[i].push('<td> </td>');
} else {
cal[i].push('<td class="day">' + day++ + '</td>');
}
}
}
cal[i].push('</tr>');
}
cal = cal.reduce(function(a, b) {
return a.concat(b);
}, []).join('');
$('table').append(cal);
$('#month').text(details.months[d.getMonth()]);
$('#year').text(d.getFullYear());
$('td.day').mouseover(function() {
$(this).addClass('hover');
}).mouseout(function() {
$(this).removeClass('hover');
});
}
$('#left').click(function(e) {
$('table').text('');
if (currentDate.getMonth() === 0) {
currentDate = new Date(currentDate.getFullYear() - 1, 11);
generateCalendar(currentDate);
} else {
currentDate = new Date(currentDate.getFullYear(), currentDate.getMonth() - 1)
generateCalendar(currentDate);
}
e.preventDefault();
});
$('#right').click(function(e) {
$('table').html('<tr></tr>');
if (currentDate.getMonth() === 11) {
currentDate = new Date(currentDate.getFullYear() + 1, 0);
generateCalendar(currentDate);
} else {
currentDate = new Date(currentDate.getFullYear(), currentDate.getMonth() + 1)
generateCalendar(currentDate);
}
e.preventDefault();
});
generateCalendar(currentDate);
});
Not sure if needed, but I provided all the code. For the sake of my question lets say I have database table called "current_events" and I want to show all events on the calendar in a different color.