Advertisement
Guest User

Untitled

a guest
Mar 9th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.02 KB | None | 0 0
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Krist
  5. * Date: 08-Mar-18
  6. * Time: 10:38
  7. */
  8. $servername = "localhost";
  9. $username = "root";
  10. $password = "123456789";
  11. $dbname = "havnarbio";
  12.  
  13. $conn = new mysqli($servername, $username, $password, $dbname);
  14.  
  15. if($conn->connect_error){
  16. die("Connection failed: " .$conn->connect_error);
  17. }
  18. $sql = "SELECT * FROM ticket where State=1";
  19. $result = $conn->query($sql);
  20. $tickets = array();
  21.  
  22. $index = 0;
  23.  
  24. while($row = mysqli_fetch_assoc($result)){ // loop to store the data in an associative array.
  25. $tickets[$index] = $row;
  26. $index++;
  27. }
  28.  
  29. ?>
  30.  
  31. <!DOCTYPE html>
  32. <html>
  33. <head>
  34. <meta charset="UTF-8">
  35. <title>Cinema Seating Plan Canvas</title>
  36. <script src="jquery-3.2.1.min.js"></script>
  37. <link rel="stylesheet" href="css/normalize.css" />
  38. <link rel="stylesheet" href="css/foundation.min.css" />
  39. <link rel="stylesheet" href="css/main.css" />
  40.  
  41. </head>
  42. <body>
  43. <canvas id="canvas1" width="500" height="355"></canvas>
  44. <script>
  45.  
  46. var tickets = <?php echo json_encode($tickets); ?>;
  47. // var tickets= [];
  48.  
  49.  
  50.  
  51. const STATE_GREEN = "rgb(13,114,0)";
  52. const STATE_BLUE = "rgb(0,0,255)";
  53. const STATE_WHITE = "rgb(255,255,255)";
  54.  
  55. //tickets.push({pos: [1, 1], state: STATE_WHITE, id: 5, available: 1});
  56.  
  57. const SEAT_WIDTH = 22;
  58. const SEAT_HEIGHT = 22;
  59. var seats = [ ];
  60. var infoSeats = [ ];
  61. var canvas = document.getElementById("canvas1");
  62. var ctx = canvas.getContext("2d");
  63. function createSeats() {
  64. //Creating the seats
  65. var seatId = 1;
  66. for (var i = 1; i < 320; i += 40) {
  67. for (var j = 1; j < 300; j += 30) {
  68. seats.push({pos: [j, i], state: STATE_GREEN, id: seatId, available: 0});
  69. seatId++;
  70. }
  71. }
  72. infoSeats.push({pos:[340, 1], state: STATE_GREEN});
  73. infoSeats.push({pos:[340,30], state: STATE_WHITE});
  74. infoSeats.push({pos:[340,60], state: STATE_BLUE});
  75. addMovieTitle();
  76. draw();
  77. canvas.addEventListener('click', function (e) {
  78. var seatMinX;
  79. var seatMaxX;
  80. var seatMinY;
  81. var seatMaxY;
  82. for(var i = 0; i < seats.length; i++) {
  83. seatMinX = seats[i].pos[0];
  84. seatMaxX = seats[i].pos[0] + 30;
  85. seatMinY = seats[i].pos[1];
  86. seatMaxY = seats[i].pos[1] + 30;
  87. if(seatMinX < e.pageX && e.pageX < seatMaxX) {
  88. if(seatMinY < e.pageY && e.pageY < seatMaxY) {
  89. if(seats[i].state !== STATE_WHITE && seats[i].state === STATE_BLUE){
  90. seats[i].state = STATE_GREEN
  91. }
  92. else if(seats[i].state !== STATE_WHITE && seats[i].state === STATE_GREEN){
  93. seats[i].state = STATE_BLUE;
  94. }
  95. draw();
  96. addPrice();
  97. }
  98. }
  99. }
  100. }, false);
  101. }
  102. function draw() {
  103. ctx.clearRect(0,0,canvas.width, canvas.height); //Clearing the canvas for redrawing
  104. seats.forEach(function(x) {
  105. ctx.fillStyle = x.state;
  106. ctx.strokeRect(x.pos[0], x.pos[1], SEAT_WIDTH, SEAT_HEIGHT);
  107. ctx.fillRect(x.pos[0], x.pos[1], SEAT_WIDTH, SEAT_HEIGHT);
  108. });
  109. infoSeats.forEach(function(x) {
  110. ctx.fillStyle = x.state;
  111. ctx.strokeRect(x.pos[0], x.pos[1], SEAT_WIDTH, SEAT_HEIGHT);
  112. ctx.fillRect(x.pos[0], x.pos[1], SEAT_WIDTH, SEAT_HEIGHT);
  113. });
  114.  
  115. if(tickets.length > 0){
  116. tickets.forEach(function(x) {
  117. if(x.state === 1){
  118. x.state = STATE_WHITE;
  119. }
  120. for(var i = 0; i < seats.length; i++){
  121. x.pos[0] = seats[i].pos[0];
  122. x.pos[1] = seats[i].pos[1];
  123. }
  124.  
  125. ctx.fillStyle = x.state;
  126. ctx.strokeRect(x.pos[0], x.pos[1], SEAT_WIDTH, SEAT_HEIGHT);
  127. ctx.fillRect(x.pos[0], x.pos[1], SEAT_WIDTH, SEAT_HEIGHT);
  128. });
  129. }
  130.  
  131.  
  132.  
  133. ctx.strokeRect(5,320,280,25);
  134. ctx.fillStyle = "black";
  135. ctx.font = ("20px Arial");
  136. ctx.fillText("Screen", 108,340);
  137. ctx.fillText("Available", 370, 20);
  138. ctx.fillText("Sold", 370, 49);
  139. ctx.fillText("Selected", 370, 79);
  140. }
  141. function submitSeats() {
  142. /*for (var i = 0; i < seats.length; i++) {
  143. if (seats[i].state === STATE_BLUE) {
  144. seats[i].state = STATE_WHITE;
  145. seats[i].available = 1;
  146. //Send id og available to server
  147. }
  148. }*/
  149. var selected = seats.filter(function (it) {
  150. return it.state === STATE_BLUE
  151. }).map(function (it) {
  152. return it.id;
  153. });
  154. $.post("bookSeats.php", {seats: selected})
  155. .fail(function (xhr) {
  156. alert(xhr.status);
  157. })
  158. .done(function (data, status, xhr) {
  159. alert(status + ':' + xhr.status);
  160. alert("Tín bílegging er skrásett");
  161. draw();
  162. });
  163.  
  164.  
  165. }
  166. function addPrice() {
  167. var tempPrice = 0;
  168. for (var i = 0; i < seats.length; i++) {
  169. if (seats[i].state === STATE_BLUE) {
  170. tempPrice += 90;
  171. }
  172. }
  173. var text = "Tín samlaði prísur er: ";
  174. var price = text.concat(tempPrice.toString());
  175. $("#totalPrice").text(price);
  176. }
  177. function addMovieTitle() {
  178. var title; //Get movie title from database
  179. $("#movieTitle").text(title);
  180. }
  181. window.addEventListener("load",createSeats, false);
  182. </script>
  183. <form method="post" action="bookSeats.php"></form>
  184. <button id="submit" onclick="submitSeats()">Keyp</button>
  185. <p id="totalPrice"></p>
  186. <h1 id="movieTitle"></h1>
  187.  
  188. </body>
  189. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement