Events i Kalender
Hej eksperterJeg er i gang med at lave en event kalender og den virker som sådan. jeg har dog problemer med at få mine events lagt ind i min kalender jeg kan slet ikke finde rundt i det.
Men her kommer min kode fra filen events.php
<?php
// Mysqli Connection
$con = mysqli_connect('localhost', 'jp_pro_dk', '******', 'jp_pro_dk');
if(mysqli_connect_errno()) {
die('Kunne ikke forbinde til databasen: ' . mysqli_connect_error());
}
$sql = "select * from calendar";
$result = $con->query($sql);
$output = '';
if($result->num_rows > 0) {
while($r = $result->fetch_array()) {
$caldate = $r['cal_date'];
$output .= '<span class="event"><a href="" data-toggle="model" data-target="#show_event">'.$r['cal_title'].'</a></span>';
}
} else {
$output .= '';
}
$data = array(
'events' => $output,
'caldate' => $caldate
);
json_encode($data);
// insert data to database tabel calendar
if(isset($_POST['add_event'])) {
$cal_date = $_POST['aar'].'-'.$_POST['maaned'].'-'.$_POST['dag'];
$cal_title = $_POST['cal_title'];
$cal_type = $_POST['cal_type'];
$cal_about = $_POST['cal_about'];
$con->query("insert into calendar(cal_date, cal_type, cal_title, cal_about) VALUES ('$cal_date', '$cal_type', '$cal_title', '$cal_about')");
header('location:calendar.php');
}
Når jeg laver en echo af min json_enconde($data) kan jeg se at den har hentet det jeg har bedt om. Nu vil jeg så have den pr automatik skal opdatere min kalender det vil jeg gøre via ajax. Det kikser totalt for mig. for det vil selt ikke som jeg vil.
her kommer min kode i event.js
$(document).ready(function() {
var d = new Date();
var year = d.getFullYear();
var month = d.getMonth()+1;
var day = d.getDay();
var dates = year + '-' + month + '-' +day;
function load_events(view) {
$.ajax({
url:'events.php',
type: 'post',
data: {view:view},
dataType: 'json',
success:function(data) {
$('#show_events').html(data.events);
if(data.caldate == dates) {
$('#show_events').innerHTML(data.events);
}
}
})
}
load_events();
});
alt dette skal så smedes sammen med min calendar.php og det skulle jeg gerne have gjort korrekt. men her kommer min calendar.php kode
<?php
// Set your timezone !!-
date_default_timezone_set('Europe/Copenhagen');
if (isset($_GET['ym'])) {
$ym = $_GET['ym'];
} else {
$ym = date('Y-m');
}
// Check format
$timestamp = strtotime($ym . '-01'); //First day of the month
if($timestamp === false) {
$ym = date('Y-m');
$timestamp = strtotime($ym . '-01');
}
// Today (Format:2018-12-28)
$today = date('Y-m-j');
// Title (Format:December, 2018)
$title = date('F, Y', $timestamp);
// Create prev & next month link
$prev = date('Y-m', strtotime('-1 month', $timestamp));
$next = date('Y-m', strtotime('+1 month', $timestamp));
//Number of days in month
$day_count = date('t', $timestamp);
// 1:mandag, 2:Tirsdag, 3:Onsdag ... 7: Søndag
$str = date('N', $timestamp);
// Array for calendar
$weeks = [];
$week = '';
// Add empty cells(s)
$week .= str_repeat('<td></td>', $str - 1);
for($day = 1; $day <= $day_count; $day++. $str++) {
$date = $ym . '-' . $day;
if($today == $date) {
$week .= '<td class="today" id="show_events">'; //. $event;
} else {
$week .= '<td id="show_events">'; //. $event;
}
$week .= '<a href="?ym='.$date.'" data-toggle="modal" data-target="#events">' .$day . '</a></td>';
// Sunday or last day of the month
if($str % 7 == 0 || $day == $day_count) {
// Last day of the month
if($day == $day_count && $str & 7 != 0) {
// Add empty cell(s)
$week .= str_repeat('<td></td>', 7 - $str % 7);
}
$weeks[] = '<tr>' . $week . '</tr>';
$week = '';
}
}
?>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Calendar</title>
<link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<script src="events.js"></script>
</head>
<style>
.container {
font-family: 'Montserrat', sans-serif;
margin: 60px auto;
}
.list-inline {
text-align: center;
margin-bottom: 30px;
}
.title {
font-weight: bold;
font-size: 26px;
}
th {
text-align: center;
}
td {
height: 100px;
}
a {
color: black;
text-decoration: none;
}
a:hover {
color: black;
}
th:nth-of-type(6), td:nth-of-type(6) {
color: white;
background-color: #357ec7;
}
th:nth-of-type(7), td:nth-of-type(7) {
color: white;
background-color: red;
}
.today {
background-color: orange;
color: black;
}
</style>
<body>
<div class="container">
<ul class="list-inline">
<li class="list-inline-item"><span class="title"><?php echo $title ?></span></li>
</ul>
<div class="btn-group float-right mb-3">
<a href="?ym=<?php echo $prev; ?>" class="btn btn-primary">Forrige</a>
<a href="calendar.php" class="btn btn-success">Idag</a>
<a href="?ym=<?php echo $next; ?>" class="btn btn-danger">Næste</a>
</div>
<table class="table table-bordered">
<thead>
<tr>
<th>Mandag</th>
<th>Tirsdag</th>
<th>Onsdag</th>
<th>Torsdag</th>
<th>Fredag</th>
<th>Lørdag</th>
<th>Søndag</th>
</tr>
</thead>
<tbody>
<?php
foreach ($weeks as $week) {
echo $week;
}
?>
</tbody>
</table>
</div>
<div class="modal" id="events">
<div class="modal-dialog">
<div class="modal-content">
<form action="events.php" method="post" enctype="multipart/form-data">
<div class="modal-header">
<h4>Tilføj event</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<div class="modal-body">
<div class="form-group">
<label for="date">Dato</label>
<select name="dag">
<?php
$daynow = date('d');
for ($i = 1; $i <= $day_count; $i++) {
?>
<option <?php if($daynow == $i) echo 'selected'; ?>><?php echo $i; ?></option>;
<?php
}
?>
</select>
<select name="maaned">
<?php
$month = date('m', $timestamp);
?>
<option value="01" <?php if($month == '01') echo 'selected'; ?>>Januar</option>
<option value="02" <?php if($month == '02') echo 'selected'; ?>>Februar</option>
<option value="03" <?php if($month == '03') echo 'selected'; ?>>Marts</option>
<option value="04" <?php if($month == '04') echo 'selected'; ?>>April</option>
<option value="05" <?php if($month == '05') echo 'selected'; ?>>Maj</option>
<option value="06" <?php if($month == '06') echo 'selected'; ?>>Juni</option>
<option value="07" <?php if($month == '07') echo 'selected'; ?>>Juli</option>
<option value="08" <?php if($month == '08') echo 'selected'; ?>>August</option>
<option value="09" <?php if($month == '09') echo 'selected'; ?>>September</option>
<option value="10" <?php if($month == '10') echo 'selected'; ?>>Oktober</option>
<option value="11" <?php if($month == '11') echo 'selected'; ?>>November</option>
<option value="12" <?php if($month == '12') echo 'selected'; ?>>December</option>
</select>
<select name="aar">
<?php
$years = '1970';
$now = date("Y");
$nows = $now + 1;
for($i = $years; $i <= $nows; $i++) {
?>
<option value="<?php echo $i; ?>" <?php if($now == $i) echo 'selected'; ?>><?php echo $i; ?></option>
<?php
}
?>
</select>
</div>
<div class="form-group">
<label for="title">Begivenhed</label>
<input type="text" name="cal_title" id="title" class="form-control">
</div>
<div class="form-group">
<label for="title">Type</label>
<select name="cal_type" class="form-control">
<option value="birthday">Fødselsdag</option>
<option value="christmas">Jul</option>
<option value="newyear">Nytår</option>
<option value="meeting">Møde</option>
<option value="holiday">Ferie</option>
<option value="other">Andet</option>
</select>
</div>
<div class="form-group">
<label for="title">Beskrivelse</label>
<textarea name="cal_about" class="form-control"></textarea>
</div>
</div>
<div class="modal-footer">
<input type="submit" name="add_event" value="Opret Event" class="btn btn-success">
<button type="reset" class="btn btn-warning">Ryd Felter</button>
<button type="button" class="btn btn-danger" data-dismiss="modal">Luk</button>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
Jeg håber at der er nogen som kan hjælpe mig med at få det gjort så det virker. for jeg kan slet i kringle den selv lige nu. Har brugt flere dage på det men må gangske enkelt smide håndklædet i ringen og bede om hjælp til det.
På forhånd tak for hjælpen eksperter og et rigtig godt nytår.