-
Notifications
You must be signed in to change notification settings - Fork 3
/
timetable.php
93 lines (93 loc) · 2.88 KB
/
timetable.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
$table = "timetable";
include("lib/layout.php");
include("lib/ironserver.php");
authentication();
?>
<?php
if(isset($_POST["action"])){
$db = new sqlite3('../main.db');
if($_POST["action"] == "edit"){
$prepare = $db->prepare('UPDATE timetable SET time = :time WHERE id = :id');
$time=prepare_db_string($_POST["time"]);
$prepare->bindparam(':time', $time);
$prepare->bindParam(':id', $_POST["id"]);
$result = $prepare->execute();
if(!$result){
echo $db->lastErrorMsg();
exit();
}
}
$db->close();
$returnto = "timetable.php#post_".$_POST["id"];
header("location:".$returnto);
}
?>
<html>
<?php
doctype();
head();
?>
<body>
<div class='container'>
<?php
html_header($table);
navigation();
?>
<div class="navigation">
</div>
<div class="main">
<div class="content">
<?php
//connect to local database
$db = new sqlite3('../main.db');
//create table if it doesn't exist
$prepare = $db->prepare('CREATE TABLE IF NOT EXISTS timetable(id INTEGER PRIMARY KEY AUTOINCREMENT, date INTEGER NOT NULL, time TEXT DEFAULT "FREE")');
$execute = $prepare->execute();
//for each day following today, find it in the table, if not existing create it.
$countstmnt = $db->prepare('SELECT count(*) FROM timetable WHERE date = :date');
for ($x = 0; $x <=30; $x++){
//define date for today +x, giving correct dates for next 30 days
date_default_timezone_set('Europe/London');
$date = mktime(0, 0, 0, date("m"), date("d")+$x, date(Y));
$countstmnt->bindparam(':date', $date);
$countres = $countstmnt->execute();
while($countrow = $countres->fetchArray(SQLITE3_ASSOC)){
if($countrow["count(*)"] == 0){
$insert = $db->prepare('INSERT INTO timetable(date) VALUES(:date)');
$insert->bindparam(':date', $date);
$complete=$insert->execute();
$insert->reset();
} else {
$selectstmnt = $db->prepare('SELECT * FROM timetable WHERE date = :date');
$selectstmnt->bindparam(':date', $date);
$selectres = $selectstmnt->execute();
while($selectrow = $selectres->fetchArray(SQLITE3_ASSOC)){
echo '<div class="post" id="post_' . $selectrow["id"] . '">';
echo "<div class='controls'><button class='database' onclick=\"javascript:editTimetable('".$selectrow["id"]."')\">edit</div>";
if($x==0){
echo '<h1>today</h1>';
}else{
echo '<h1>'.strftime("%a-%d", $selectrow["date"]).'</h1><i>'.strftime("%b", $selectrow["date"]).'</i>';
}
echo '<p id="time_'.$selectrow["id"].'">'.$selectrow["time"].'</p></div>';
}
}
//reset statement within loop next iteration
$prepare->reset();
}
}
$db->close();
?>
</div>
<?php
sidenav();
?>
<div class="clearer"><span></span></div>
</div>
<div class="navigation">
</div>
<?php footer(); ?>
</div>
</body>
</html>