PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/setdata.php

https://bitbucket.org/dmdonnelly/developer-test
PHP | 207 lines | 176 code | 11 blank | 20 comment | 25 complexity | ab72e22c67a16c34a3bd888d56811498 MD5 | raw file
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7. <title>Databee Test - Set Test Data</title>
  8. <!-- Bootstrap -->
  9. <link href="css/bootstrap-4.0.0.css" rel="stylesheet">
  10. <!-- All SQL connection code contained to a seperate file -->
  11. <? include("connect.php"); ?>
  12. </head>
  13. <body>
  14. <div class="container mt-2">
  15. <div class="row">
  16. <div class="text-center col-md-3 col-12"></div>
  17. <div class="text-center col-md-5 col-12">
  18. <div class="col-12 card bg-light mx-auto">
  19. <h3 class="text-center card-header">Set Test Data</h3>
  20. <form class="card-body" action="<?=$PHP_SELF?>" method="post">
  21. <div class="form-group row">
  22. <label for="numVenue" class="col-form-label">Number of Venues to Add</label>
  23. <div class="input-group">
  24. <input type="number" min="0" class="form-control" id="numVenue" name="numVenue" >
  25. </div>
  26. </div>
  27. <div class="form-group row">
  28. <label for="numBookings" class="col-form-label">Number of 'Existing Bookings' to Add*</label>
  29. <div class="input-group">
  30. <input type="number" min="0" class="form-control" id="numBookings" name="numBookings" >
  31. </div>
  32. <sub style="line-height: 1; text-align: left;">* as data is generated randomly, there may be less entries than selected due to duplicate entries.</sub>
  33. </div>
  34. <div class="form-group row">
  35. <div class="input-group">
  36. <label for="doBEachV" class="col-form-label">
  37. <input type="checkbox" id="doBEachV" name="doBEachV" value="1" > Apply additional bookings to each venue?
  38. </label>
  39. </div>
  40. </div><div class="form-group row">
  41. <div class="input-group">
  42. <label for="doReset" class="col-form-label">
  43. <input type="checkbox" id="doReset" name="doReset" value="1" > Reset dataset before repopulating?
  44. </label>
  45. </div>
  46. </div>
  47. <input type="submit" name="submit" value="Submit">
  48. </form>
  49. </div>
  50. </div>
  51. <div class="text-center col-md-4 col-12"></div>
  52. </div>
  53. </div>
  54. <? if(isset($_POST['submit'])): ?>
  55. <hr>
  56. <div class="container mt-2">
  57. <div class="row">
  58. <div class="text-center col-md-3 col-12"></div>
  59. <div class="text-center col-md-5 col-12">
  60. <div class="col-12 card bg-light mx-auto">
  61. <h3 class="text-center card-header">Results</h3>
  62. <?
  63. // On a reset, delete both tables if they exist, then recreate them
  64. // This option can also be used to create the tables for the first time on a new setup
  65. if(isset($_POST['doReset'])) {
  66. ///////////////////////////////////////////////////////////////////////////////////////////////////////////
  67. // The tables below have key values allowing up to 9,999,999,999 entries. //
  68. // However, in a real-world setting, booking_id would need to have a higher maximum value than venue_id, //
  69. // as each venue can have between 264 and 279 bookings, depending on the number of days in the 3 months. //
  70. // I decided that it wasn't important enough to change for this test, as in order to exceed the maximum //
  71. // value of booking_id, there would need to be in excess of 26,384,225 venues. //
  72. // Additionally, the server I used to test this functionality on only allowed 75,000 queries per hour, //
  73. // so I wasn't able to stress-test this to anywhere near those limits //
  74. ///////////////////////////////////////////////////////////////////////////////////////////////////////////
  75. $result1 = mysql_query("DROP TABLE IF EXISTS `venue`");
  76. if (!$result1) {
  77. echo("<p>Error performing query: " . mysql_error() . "</p>");
  78. exit();
  79. }
  80. $result2 = mysql_query(
  81. "CREATE TABLE IF NOT EXISTS `venue` (
  82. `venue_id` int(10) NOT NULL AUTO_INCREMENT,
  83. `venue_name` varchar(30) NOT NULL,
  84. PRIMARY KEY (`venue_id`)
  85. )");
  86. if (!$result2) {
  87. echo("<p>Error performing query: " . mysql_error() . "</p>");
  88. exit();
  89. }
  90. $result3 = mysql_query("DROP TABLE IF EXISTS `venue_booking`");
  91. if (!$result3) {
  92. echo("<p>Error performing query: " . mysql_error() . "</p>");
  93. exit();
  94. }
  95. $result4 = mysql_query(
  96. "CREATE TABLE IF NOT EXISTS `venue_booking` (
  97. `booking_id` int(10) unsigned NOT NULL,
  98. `venue_id` int(10) unsigned NOT NULL,
  99. `date` date NOT NULL,
  100. `period` tinyint(1) unsigned NOT NULL,
  101. PRIMARY KEY (`booking_id`,`venue_id`,`date`,`period`)
  102. )");
  103. if (!$result4) {
  104. echo("<p>Error performing query: " . mysql_error() . "</p>");
  105. exit();
  106. }
  107. echo "Successfully reset the dataset.<br>"; // If no errors have occurred to halt operations, report success
  108. $booking_id = 1; // Reset the starting booking ID to 1
  109. } else {
  110. // If the dataset is not being reset, the existing values from it are required before adding more
  111. // This gets the number of existing venues, to ensure the unique key of the table is maintained
  112. $sql_query = "SELECT * FROM " . $db_venue;
  113. $result = mysql_query($sql_query);
  114. $num_venue = mysql_num_rows($result);
  115. // This gets the highest existing booking ID, so as to start adding entries one higher
  116. $sql_query = "SELECT MAX(`booking_id`) FROM " . $db_venue_book;
  117. $result = mysql_query($sql_query);
  118. $row = mysql_fetch_array($result);
  119. $booking_id = $row[0] + 1;
  120. }
  121. // The 'Add Venue' code is only run if the submitted data calls for additional venues to be added
  122. if ($_POST['numVenue'] > 0) {
  123. if(isset($_POST['doReset'])) {
  124. $venue_id = 1; // Set the initial venue ID to 1 on a full reset
  125. } else {
  126. $venue_id = $num_venue + 1; // Increment the existing maximum venue ID on standard entry
  127. }
  128. for ($i=1; $i <= $_POST['numVenue']; $i++) {
  129. // Rather than generating a random name, venues are just assigned a number matching their ID
  130. $data_string = "INSERT INTO `venue` VALUES (" . $venue_id . ", 'Venue " . $venue_id . "')";
  131. $result = mysql_query($data_string);
  132. if (!$result) {
  133. echo("<p>Error performing query: " . mysql_error() . "</p>");
  134. exit();
  135. }
  136. $venue_id++;
  137. }
  138. echo $_POST['numVenue'] . " venues have been inserted.<br>"; // If no errors have occurred to halt operations, report success
  139. } else {
  140. // If a reset has occurred, or there are just no venues in the system, further operations are cancelled
  141. if ($num_venue == 0 || isset($_POST['doReset'])) {
  142. echo "The dataset has no venues.<br>";
  143. exit();
  144. }
  145. }
  146. $startdate = strtotime("now"); // Random dates are selected starting from 'today'
  147. $enddate = strtotime("+3 month"); // Random dates are selected up to any point three months from 'today'
  148. $n = 0; // Counter for how many insertions are carried out, for outputted results
  149. // If a reset has occurred, the number of venues is the number inserted. If not, any inserted are then added to the existing number
  150. if(isset($_POST['doReset'])) {
  151. $num_venue = $_POST['numVenue'];
  152. } else {
  153. $num_venue += $_POST['numVenue'];
  154. }
  155. // If the option to assign additional bookings to each venue is selected, then for each venue, the chosen number of random bookings are generated and added
  156. // If thet option if not selected, then as part of generating the random bookings, a random venue is selected
  157. if(isset($_POST['doBEachV'])) {
  158. for ($i=1; $i <= $num_venue; $i++) {
  159. for ($j=1; $j <= $_POST['numBookings']; $j++) {
  160. $date = rand($startdate,$enddate);
  161. $period = rand(1,3);
  162. $data_string = "INSERT IGNORE INTO `venue_booking` VALUES (" . $booking_id . ", " . $i . ", '". date("Y-m-d",$date) ."', ". $period .")";
  163. $result = mysql_query($data_string);
  164. if (!$result) {
  165. echo("<p>Error performing query: " . mysql_error() . "</p>");
  166. exit();
  167. }
  168. $booking_id++;
  169. $n++;
  170. }
  171. }
  172. } else {
  173. for ($j=1; $j <= $_POST['numBookings']; $j++) {
  174. $date = rand($startdate,$enddate);
  175. $period = rand(1,3);
  176. $venue = rand(1,$num_venue);
  177. $data_string = "INSERT IGNORE INTO `venue_booking` VALUES (" . $booking_id . ", " . $venue . ", '". date("Y-m-d",$date) ."', ". $period .")";
  178. $result = mysql_query($data_string);
  179. if (!$result) {
  180. echo("<p>Error performing query: " . mysql_error() . "</p>");
  181. exit();
  182. }
  183. $booking_id++;
  184. $n++;
  185. }
  186. }
  187. echo $n . " existing bookings have been inserted.<br>" // If no errors have occurred to halt operations, report success
  188. ?>
  189. </div>
  190. </div>
  191. <div class="text-center col-md-4 col-12"></div>
  192. </div>
  193. </div>
  194. <? endif; ?>
  195. </body>
  196. </html>