-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
160 lines (150 loc) · 4.61 KB
/
index.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
require_once 'header.php';
echo <<<_END
<script>
function handleClick(cell){
params = "seat="+cell.id
request = new ajaxRequest()
request.open("POST", "updateseat.php", true)
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
O('operation').innerHTML = "...";
request.onreadystatechange = function()
{
if (this.readyState == 4){
if (this.status == 200){
if (this.responseText != null){
var response = this.responseText;
var array = response.split(";",2);
if(array.length == 2){
if(array[1]=='reserved'){
O(array[0]).style.backgroundColor = 'yellow'
O('operation').innerHTML = "Reservation performed";
}
else if(array[1] == 'delete'){
O(array[0]).style.backgroundColor = 'green'
O('operation').innerHTML = "Reservation removed";
}
else if(array[1] == 'nothing'){
O(array[0]).style.backgroundColor = 'red'
O('operation').innerHTML = "Reservation not performed because the seat is already sold";
}
else if(array[1] == 'expired'){
alert("Your session expired, log in to perform operations");
location.reload();
}
}
}
}
}
}
request.send(params)
}
function buySeats(){
request = new ajaxRequest()
request.open("POST", "buyseats.php", true)
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
request.onreadystatechange = function(){
if (this.readyState == 4){
if (this.status == 200){
if (this.responseText != null){
var response = this.responseText
if(response == 'nothing'){
alert("Select one seat before buying")
}
else if(response == 'impossible'){
alert("The seat you reserved were reserved/bought by other in the meantime")
}
else if(response == 'possible'){
alert("The purchase is done")
}
else if(response == 'expired'){
alert("Your session expired, log in to performs operations");
}
else{
alert("Your request is not valid");
}
}
location.reload()
}
}
}
request.send()
}
</script>
_END;
$num_col = 6;
$num_row = 10;
$bought = 0;
$reserved = 0;
$i = 0;
$j = 0;
echo '<div class="mine">'.
'<table border=4 class="stats" cellspacing=0><tr><td></td>';
$letterAscii = ord('A');
if(isset($_SESSION['email'])){
$email=$_SESSION['email'];
$_SESSION['seats']=array();
$_SESSION['maxcol']=$letterAscii+$num_col;
$_SESSION['maxrow']=$num_row;
}
else{
$email="";
}
for($j = 0; $j < $num_col; $j++){
$letter=chr($letterAscii+$j);
echo "<th>$letter</td>";
}
echo "</tr>";
for($i = 1; $i <= $num_row; $i++){
echo "<tr><th scope='row'>$i</th>";
for($j = 0; $j < $num_col; $j++){
$index=chr($letterAscii+$j);
$index=$index.$i;
$result = queryMySql("SELECT status, email FROM Seat WHERE seat='$index'");
$color="";
$function="";
if ($result->num_rows){
$row = $result->fetch_assoc();
if($row['status'] == 'b'){
$color='red';
$bought++;
}
elseif($row['status']=='r' && $row['email'] != $email){
$color='orange';
$reserved++;
}
else{
$color='yellow';
array_push($_SESSION['seats'], $index);
}
}
else{
$color = 'green';
}
if($color=='red' || $email==""){
$function = '';
$cursor = "default";
}
else{
$function = "onclick='handleClick(this)'";
$cursor = "pointer";
}
echo <<<_END
<td id='$index' style='background-color:$color; cursor: $cursor;' $function>$index</td>
_END;
}
echo "</tr>";
}
$size=0;
if (!isset($_SESSION['email'])){
$tot = ($num_row*$num_col);
echo "Number of seat reserved by others: <span id='reserved' style='color:orange;'>$reserved </span><br> ";
echo "Number of seat bought: <span id='bought' style='color:red;'> $bought </span><br>";
echo "Number of seat free: <span id='free' style='color:green;'> ".($tot-$bought-$reserved-$size)."</span><br>";
echo "Number of total seat: $tot<br><br>";
}
else{
echo "<span id='operation'></span><br><br>";
}
echo '</div></div>';
?>