· 6 years ago · Aug 01, 2019, 06:34 AM
1<!DOCTYPE html>
2<html>
3<head>
4 <meta charset="utf-8" />
5 <meta http-equiv="X-UA-Compatible" content="IE=edge">
6 <title>ARMOUR SEAT POSITION</title>
7 <meta name="viewport" content="width=device-width, initial-scale=0.5">
8 <link rel="stylesheet" type="text/css" media="screen" href="main.css" />
9 <script src="main.js"></script>
10</head>
11<body style="min-width:700px;">
12 <style>
13 table {
14 border-collapse: collapse;
15 margin: auto;
16 }
17
18 table, th, td {
19 border: 1px solid black;
20 padding: 10px;
21 text-align: center;
22 font-size: 1.2em;
23 }
24
25 h1,h2 { text-align: center; margin: 20px; padding: 0;}
26 h1 {font-size: 32px;}
27 h2 {font-size: 20px;font-weight: normal; margin-bottom: 5px;}
28 </style>
29
30 <?php
31 $local = true;
32 if($local)
33 {
34 $db_host = 'localhost';
35 $db_username = 'root';
36 $db_password = '';
37 $db_name = 'randoman';
38 }
39 else
40 {
41 $db_host = '';//db url goes here
42 $db_username = '';//db username goes here
43 $db_password = '';//db password goes here
44 $db_name = '';//
45 }
46
47
48 date_default_timezone_set('Asia/Jakarta');
49
50 function generateNewSeatQuery($offset)
51 {
52 $names = array(
53 "Abed",
54 "Jojo",
55 "Aldi ",
56 "Agung",
57 "Alexius",
58 "Alvin",
59 "Andrew",
60 "Angel",
61 "Billie",
62 "Calvin",
63 "Candra",
64 "Chandra",
65 "Christian",
66 "Cindy",
67 "David",
68 "Deka",
69 "Edgar",
70 "Edrio",
71 "Febi",
72 "Gabrielle",
73 "Ananta",
74 "Harry",
75 "Hwang",
76 "Jovan",
77 "Kelvin",
78 "Kevin",
79 "Marcel",
80 "Natasha",
81 "Nathan",
82 "Nico",
83 "Dhani",
84 "Renaldy",
85 "Rendi",
86 "Rosa",
87 "Nisa",
88 "Sharen",
89 "Steven",
90 "Timothy",
91 "Verina",
92 "Vincent",
93 "Vincentius",
94 "Wiwi",
95 "William"
96 );
97 $names = array(
98 1,
99 1,
100 1,
101 1,
102 1,
103 1,
104 1,
105 0,
106 1,
107 1,
108 1,
109 1,
110 1,
111 0,
112 1,
113 1,
114 1,
115 1,
116 0,
117 0,
118 1,
119 1,
120 1,
121 1,
122 1,
123 1,
124 1,
125 0,
126 1,
127 1,
128 1,
129 1,
130 1,
131 0,
132 0,
133 0,
134 1,
135 1,
136 0,
137 1,
138 1,
139 0,
140 1
141 );
142 $date_before_select = date_create();
143 if($offset) $query = 'INSERT INTO seathistory VALUES(NULL,"' . date_format(date_sub($date_before_select, date_interval_create_from_date_string($offset." days")), "Y-m-d").'",';
144 else $query = 'INSERT INTO seathistory VALUES(NULL,"' . date("Y-m-d").'",';
145 while(count($names)>0) {
146 $idx = random_int(0,count($names)-1);
147 $query = $query . '"' . $names[$idx] .'"';
148 array_splice($names, $idx, 1);
149 if(count($names)!=0) $query = $query . ',';
150 else $query = $query . ')';
151 }
152 return $query;
153 }
154
155 function newTableQuery()
156 {
157 $query =
158 'CREATE TABLE IF NOT EXISTS seathistory (
159 id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
160 `TimeStamp` DATE, ';
161
162 for ($i=1; $i <= 43; $i++) {
163 $query .= 'member' . $i . ' VARCHAR(100)';
164 if($i<43) $query .= ','; else $query .= ')';
165 }
166 return $query;
167 }
168
169 function newTableGenderQuery()
170 {
171 $query =
172 'CREATE TABLE IF NOT EXISTS genderhistory (
173 id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
174 `TimeStamp` DATE, ';
175
176 for ($i=1; $i <= 43; $i++) {
177 $query .= 'member' . $i . ' INT';
178 if($i<43) $query .= ','; else $query .= ')';
179 }
180 return $query;
181 }
182
183 //MAIN
184 $conn = mysqli_connect($db_host, $db_username, $db_password, $db_name);
185
186 if (mysqli_connect_errno()) {
187 printf("Connect failed: %s\n", mysqli_connect_error());
188 exit();
189 }
190
191 //generate table check
192 mysqli_query($conn, newTableQuery()) or die(mysqli_error($conn));
193 mysqli_query($conn, newTableGenderQuery()) or die(mysqli_error($conn));
194 if(!empty($_GET["date"])){
195 $date_get = htmlspecialchars($_GET["date"]);
196 $date_before_select = date_create_from_format("d-m-Y", $date_get);
197 $date_after_select = date_create_from_format("d-m-Y", $date_get);
198 if(!$date_before_select)
199 {
200 echo '<i>Invalid date</i>';
201 exit();
202 }
203 }
204 else
205 {
206 $date_before_select = date_create();
207 $date_after_select = date_create();
208 }
209 $date_before = date_format(date_sub($date_before_select, date_interval_create_from_date_string("5 days")), "Y-m-d");
210 $date_after = date_format(date_add($date_after_select, date_interval_create_from_date_string("1 days")), "Y-m-d");
211 $query = "SELECT * FROM seathistory WHERE `TimeStamp` BETWEEN '" . $date_before . "' AND '" . $date_after . "'";
212
213 echo '<div style="text-align:center;"><img src="http://image.ibb.co/gmNQ07/armour256.png" width="150" height="150"></div>';
214 echo '<h1>ARMOUR SEAT POSITION</h1>';
215 if ($result = mysqli_query($conn, $query)) {
216 /* fetch associative array */
217 if(mysqli_num_rows($result) == 0)
218 {
219 if(empty($_GET["date"]))
220 {
221 if(date("w")>=0 && date("w")<=5)
222 {
223 mysqli_query($conn, generateNewSeatQuery(date("w"))) or die(mysqli_error($conn));
224 $query = "SELECT * FROM seathistory WHERE `TimeStamp` BETWEEN '" . $date_before . "' AND '" . $date_after . "'";
225 $result = mysqli_query($conn, $query);
226
227 }
228 else
229 {
230 echo "<h2>-Seat will be randomized at the next Sunday-</h2>";
231 }
232 }
233 else
234 {
235 echo "<h2><i>No data for date <b>".date_format(date_create_from_format("d-m-Y", $date_get), "l, F jS, Y")."</b></i></h2>";
236 }
237 }
238 if(mysqli_num_rows($result) != 0)
239 {
240 $row = mysqli_fetch_row($result);
241 $date_obj_first = date_create_from_format("Y-m-d", $row[1]);
242 $date_obj_last = date_create_from_format("Y-m-d", $row[1]);
243 $firstday = date_format(date_add($date_obj_first,date_interval_create_from_date_string("1 days")), "l, F jS");
244 $lastday = date_format(date_add($date_obj_last,date_interval_create_from_date_string("5 days")), "l, F jS");
245 echo '<h2>Seat position from <b>'.$firstday.'</b> to <b>'.$lastday.'</b></h2><br/>';
246 echo '<table border=1 >';
247 for ($i=1; $i < 44; $i++) {
248 echo '<td>'.$row[$i+1].'</td>';
249 if($i%12 == 0 && $i<=24) echo '</tr>';
250 if($i == 20) echo '<td></td>';
251 if($i == 21) echo '<td></td>';
252 else if(($i-24)%7 == 0 && $i>24) echo '</tr>';
253 }
254 echo '</table>';
255 }
256 echo '<p style="text-align:center;font-style:italic;">Today is <b>' . date("l, F jS, Y") .'</b>.</p>';
257 /* free result set */
258 mysqli_free_result($result);
259 }
260 mysqli_close($conn);
261 ?>
262</body>
263</html>
264
265<!-- coded by nanoskill - first implemented 15-04-2018 -->