I am trying to upload events from a mySQLi database to a jQuery datepicker. For example, if my team had a game on June 28, I want the user to be able to click on the datepicker and to have a popup show up saying who the team is playing on that date.
Here is my javascript for the datepicker:
<link href="CSS/jquery-ui.css" rel="stylesheet" />
<script src="Scripts/jquery.js"></script>
<script src="Scripts/jquery-ui.js"></script>
<!-- Javascript -->
<script>
var events = new Array();
<?php echo "events = ".$js_array. ";\n";?>
/*var events = [
{ Title: "Five K for charity", Date: new Date("06/30/2015") },
{ Title: "Dinner", Date: new Date("07/01/2015") },
{ Title: "Meeting with manager", Date: new Date("07/02/2015") }
];*/
$(function() {
$( "#datepicker" ).datepicker({
beforeShowDay: function(date) {
var result = [true, '', null];
var matching = $.grep(events, function(event) {
return event.Date.valueOf() === date.valueOf();
});
if (matching.length) {
result = [true, 'highlight', null];
}
return result;
},
onSelect: function(dateText) {
var date,
selectedDate = new Date(dateText),
i = 0,
event = null;
while (i < events.length && !event) {
date = events[i].Date;
if (selectedDate.valueOf() === date.valueOf()) {
event = events[i];
}
i++;
}
if (event) {
alert(event.Title);
}
}
});
And the accompanying php:
<?php
session_start();
if(isset($_SESSION["userid"])) {
mysqli_select_db($con, "fundraising") or die("could not find db");
$queryGame = "SELECT Date, Event FROM schedule_test WHERE EventID
BETWEEN 1 AND 20" or die("the query don’t work!");
$gamer=$con->query($queryGame);
if(!$gamer){
$count=0;
} else {
$count = mysqli_num_rows($gamer);
echo $count;
}
if ($count==0){
echo("something went wrong");
} else {
$gamesArr = array();
$i=0;
while($row = mysqli_fetch_array($gamer)) {
$arr[$i]['Title'] = $row['Date'];
$arr[$i]['Date'] = $row['Event'];
++$i;
}
$js_array = json_encode($gamesArr);
}
} else {
header('Location: indexv2.php');
}
?>
As you can see, there is an events array in the javascript that is commented out. When events are directly placed in the code, it works just fine and the alert comes up, but I want an administrator to be able to upload a schedule to the database and then have the calendar automatically load with that schedule.
I tried the json_encode() to convert the php array to javascript, but either it is not functioning or there is a bad race condition occuring such that the events array is undefined when the script runs.
Does anyone have a good solution to this? Or a better way to do it? Really appreciate your help.
Aucun commentaire:
Enregistrer un commentaire