I have a calendar on my site that uses javascript/jquery to place events in the calendar. All the events are stored in a MySQL database with the title and Unix timestamp.
My problem is that this method (shown in the code below) only allows one event per day, and creating two rows with the same date, only uses the latter of the two. There are several instances where I need two or more events in one day.
My code:
var CalendarEvent = function (time, title) {
this.time = time;
this.title = title;
}
var allEvents = {
<?php require_once 'ndx/mysql.php';
$events = mysqli_query($con, "SELECT * FROM events");
while($lse = mysqli_fetch_array($events)):
$date = date('Y-m-d', $lse['timestamp']);
$notime = $lse['notime'];
if($notime != 'true'){
$time = date('g:i a', $lse['timestamp']);
}else{
$time = '';
}
$event = addslashes($lse['title']);
?>
'<?php echo $date; ?>': [
new CalendarEvent('<?php echo $time; ?>', '<?php echo $event; ?>')
],
<?php endwhile; ?>
Related
As part of a version of FullCalendar with user accounts, I would like to check if the current session matches the value of one of the fields of one or more events created via fullcalendar.
My events are generated as follows:
SQL query:
$sql = "SELECT id, title, description, organisateur, orgaavailable, donator, start, end, color FROM events ";
$req = $bdd->prepare($sql);
$req->execute();
$events = $req->fetchAll();
My events are pre-generated via the following javascript function (which allows me to automatically replace the content of the fields of a form by the content of what has been retrieved from my database)
eventRender: function(event, element, view) {
element.bind('click', function() {
$('#ModalEdit #id').val(event.id);
$('#ModalEdit #title').val(event.title);
$('#ModalEdit #description').val(event.description);
$('#ModalEdit #organisateur').val(event.organisateur);
$('#ModalEdit #orgaavailable').val(event.orgaavailable);
$('#ModalEdit #donator').val(event.donator);
$('#ModalEdit #color').val(event.color);
$('#ModalEdit').modal('show');
});
},
Finally, my event is parsed in JSON via a PHP loop
events: [
<?php foreach($events as $event):
$start = explode(" ", $event['start']);
$end = explode(" ", $event['end']);
if($start[1] == '00:00:00'){
$start = $start[0];
}else{
$start = $event['start'];
}
if($end[1] == '00:00:00'){
$end = $end[0];
}else{
$end = $event['end'];
}
$title = addslashes($event['title']);
$description = addslashes($event['description']);
$organisateur = addslashes($event['organisateur']);
$orgaavailable = addslashes($event['orgaavailable']);
$donator = addslashes($event['donator']);
if ($userlogged == $organisateur)
{
$vartest = 1;
}else{
$vartest = 0;
}
?>
{
id: '<?php echo $event['id']; ?>',
title: '<?php echo $title; ?>',
test : '<?php echo $vartest; ?>',
description: "<?php echo $description; ?>",
organisateur: '<?php echo $organisateur; ?>',
orgaavailable: '<?php echo $orgaavailable; ?>',
donator: '<?php echo $donator; ?>',
start: '<?php echo $start; ?>',
end: '<?php echo $end; ?>',
color: '<?php echo $event['color']; ?>',
},
<?php endforeach; ?>
]
After that, my form is displayed in a modal and the fields are pre-filled with the information retrieved from my database (the form being too big, I don't know if I can put its code here)
The problem is that I can't see how I could compare the value of my session ($_SESSION['user_session']) with the value of what is parse in the "organisateur" value: by the variable $organisateur to know if my connected user is the user indicated in $organisateur.
For information, I performed a test in my code that fills in the value 1 in the variable $vartest if $userlogged == $organizer ($userlogged being a variable retrieving the content of $_SESSION['user_session']) or the value 0 if the condition is not met.
The variable $vartest is then parsed next to the value "test :"
This condition works perfectly, 1 is displayed in the parse of the event belonging to the connected user and conversely, a 0 is displayed if it is not the case.
The parse is well formed when I look at the source code generated by my browser
Thanks in advance !
I've created a search page that sends results to a table with the ability to click on a specific record which then opens another page in the desired format.
I'd like to do is be able to open different formatted pages based on the data returned in the search query but I'm having a bit of trouble pulling it all together.
Here's the PHP used to request and retrieve the data from the database, as well as populate it in a table where each record can be selected and used to populate a planner page with all the proper formatting:
$search = $_POST['search'].'%';
$ment = $_POST['ment'];
$stmt = $link->prepare("SELECT lname, fname, rank, reserve, ment1, pkey FROM planner WHERE lname LIKE ? AND ment1 LIKE ? ORDER BY lname, fname");
$stmt->bind_param('ss', $search, $ment);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
echo "<table><tr><th>Last Name</th><th>First Name</th><th>Rank</th><th>Mentor Group</th><th></th></tr>";
while($row = $result->fetch_assoc()) {
$rsv = $row['reserve'];
$pkey = $row['pkey'];
echo "<tr><td>".$row['lname']."</td><td>".$row['fname']."</td><td>".$row['rank']."</td><td>".$row['ment1']."</td><td><button onClick=getPlanner('".$pkey."');>Get Planner</button></td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
Now the fun part. I want to open different pages based on the information contained in the record. I've got it working for the pkey variable by itself with a single javascript function. However, if I want to open a differently formatted page using the same function using if, else statements, the table only populates with the link page based on the last record compared. Here is my attempt to get the JavaScript with the if, else statements working but it only uses the format of the last record that's compared.
var pkey = <?php echo json_encode($pkey); ?>;
var rsv = <?php echo $rsv ?>;
//var check = document.write(rsv);
function getPlanner(pkey) {
if(rsv != 0){
var plan = window.open("../php/plannerR.php?pln=" + pkey);
} else {
var plan = window.open("../php/planner.php?pln=" + pkey);
}
}
How do I get the 'Get Planner' button to open the correctly formatted planner page based on the users specific information?
To make things easier I'd suggest the following:
Do the logic already in php when generating the html-table (and the link).
while($row = $result->fetch_assoc()) {
$rsv = $row['reserve'];
$pkey = $row['pkey'];
if($rsv) { // thats basicly the same as !=0
$target='../php/plannerR.php'
} else {
$target='../php/planner.php'
}
echo "<tr><td>".$row['lname']."</td><td>".$row['fname']."</td>";
echo "<td>".$row['rank']."</td><td>".$row['ment1']."</td>";
echo "<td><a class='button styleIt' href='".$target."?pkey=".$pkey."&rsv=".$rsv."'>Get Planner</a></td></tr>";
}
If you wanna stick to your js solution (which is more hassle unless you really need it) you can of course go with the solution from my comments that you already successfully implemented (and posted as answer so others can see the implementetion).
Thanks to Jeff I played around a bit with bringing both variables into the function and got it to work. Final code below.
$search = $_POST['search'].'%';
$ment = $_POST['ment'];
$stmt = $link->prepare("SELECT lname, fname, rank, reserve, ment1, pkey FROM planner WHERE lname LIKE ? AND ment1 LIKE ? ORDER BY lname, fname");
$stmt->bind_param('ss', $search, $ment);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
echo "<table><tr><th>Last Name</th><th>First Name</th><th>Rank</th><th>Mentor Group</th><th></th></tr>";
while($row = $result->fetch_assoc()) {
$rsv = $row['reserve'];
$pkey = $row['pkey'];
echo "<tr><td>".$row['lname']."</td><td>".$row['fname']."</td><td>".$row['rank']."</td><td>".$row['ment1']."</td><td><button onClick=getPlanner('".$pkey."','".$rsv."');>Get Planner</button></td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
var pkey = <?php echo json_encode($pkey); ?>;
var rsv = <?php echo $rsv ?>;
//var check = document.write(rsv);
function getPlanner(pkey, rsv) {
if(rsv != 0){
var plan = window.open("../php/plannerR.php?pln=" + pkey);
}
else{
var plan = window.open("../php/planner.php?pln=" + pkey);
}
}
var phpCode = '<?php
$sql = "SELECT Name,Surname,id_room FROM timatable.professors WHERE p.id_professor = '".mysqli_real_escape_string($_POST['hiddenProfId'])."'";
$resutl = mysqli_query($db,$sql);
if ($result == 1 ) {
$row = mysqli_fetch_array($result);
$professorName = $row['Name'];
$professorSurname = $row['Surname'];
} else echo "Error";
?>';
alert(phpCode);
this is my code. how to make it work ????
Try this.
First initialize, variables to null.
$professorName = "";
$professorSurname = "";
This is because, if php code enters else part, you will not get any error in javascript part.
<?php
$sql = "SELECT Name,Surname,id_room FROM timatable.professors WHERE p.id_professor = '".mysqli_real_escape_string($_POST['hiddenProfId'])."'";
$resutl = mysqli_query($db,$sql);
if ($result == 1 ) {
$row = mysqli_fetch_array($result);
$professorName = $row['Name'];
$professorSurname = $row['Surname'];
} else echo "Error";
?>
<script>
var professorName = "<?php echo $professorName ?>";
var professorSurname = "<?php echo $professorSurname ?>";
alert(professorName);
alert(professorSurname);
</script>
PHP is a server-side language. So it is processed on a server. Therefore you cannot have a PHP code in javascript.
If you want to have javascript managed some editing in database, you can use AJAX to do it without reloading the page.
I am trying to disable dates in a jQuery UI Date Picker, it works when I hard code the dates in to the variable in the JS file as follows:
var bookedDays = ["2015-3-7","2015-3-8","2015-3-15"];
In my PHP file I have:
<?php
$testing = "SELECT fromdate, todate FROM messages WHERE listing_id = '".$_GET['listingid']."'";
$resulttesting = mysql_query($testing) or die(mysql_error() . "<br>" . $testing);
while ($rowtesting = mysql_fetch_assoc($resulttesting))
{
$from = $rowtesting['fromdate'];
$to = $rowtesting['todate'];
}
$start_time = strtotime($from);
$end_time = strtotime($to);
$date_list = array($from);
$current_time = $start_time;
while($current_time < $end_time) {
//Add one day
$current_time += 86400;
$date_list[] = date('Y-m-d',$current_time);
}
//Finally add end date to list, array contains all dates in order
$date_list[] = $to;
$date_list_res = '["' . implode('","', $date_list) . '"]';
print_r ($date_list_res);
?>
<script type="text/javascript">
var bookedDays = <?php echo json_encode($date_list_res); ?>;
</script>
When I run a console.log in the JS file for the variable bookedDays I get ["2015-03-05","2015-03-06","2015-03-07","2015-03-08","2015-03-08"] output which is read from the database which is correct but these dates are not disabling in the date picker. Does anybody know where I'm going wrong?
Instead of
<?php echo json_encode($date_list_res); ?>;
Type just
<?php echo $date_list_res; ?>;
Everything should be dandy.
I am trying to embed a data picker calendar in my php site. The code, which came off the shelf from DatePicker, allows to select some pre-highlighted dates, which is good as I have a list in a db table with the dates I need.
However, I have had no success in finding a way to include php within the script, or the other way round.
The original calendar script is as follows:
<script type='text/javascript'>
$(function() {
$("#txtDate").datepicker({dateFormat: 'yy-mm-dd'});
});
$(document).ready(function() {
var SelectedDates = {};
SelectedDates[new Date('02/24/2014')] = new Date('02/24/2014');
SelectedDates[new Date('03/10/2014')] = new Date('03/10/2014');
$('#txtDate').datepicker({
beforeShowDay: function(date) {
var Highlight = SelectedDates[date];
if (Highlight) {
return [true, "Highlighted", Highlight];
}
else {
return [true, '', ''];
}
}
});
});
</script>
I would like to be able to select a long list of dates to go in
SelectedDates[new Date('03/10/2014')] = new Date('03/10/2014');
so my original idea was to do as follows:
$(document).ready(function() {
var SelectedDates = {};
<?php
$query = "SELECT eventDate FROM database.calendar WHERE tag='R' AND competition='1'";
$result = mysql_query($query);
while ($row = mysql_fetch_row($result)) {
$eventdate = $row[0];
SelectedDates[new Date('$eventdate')] = new Date('$eventdate');
}
?>
Sadly, this doesn't work (and neither do any of the various attempts to re-add tags within the PHP.
Any idea? Thank you so much for your help!
You have to close your PHP tags to output JavaScript (or use echo as mentioned in a comment):
$(document).ready(function() {
var SelectedDates = {};
<?php
$query = "SELECT eventDate FROM database.calendar WHERE tag='R' AND competition='1'";
$result = mysql_query($query);
while ($row = mysql_fetch_row($result)) {
$eventdate = $row[0];
?>
SelectedDates[new Date('<?php echo $eventdate; ?>')] = new Date('<?php echo $eventdate; ?>');
<?php
}
?>
});
Although I should mention that what you are trying to do is not really ideal. Do the PHP while loop separately, get that into an array, and json_encode that.