Advertisement
Guest User

certainty.php

a guest
Feb 18th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.71 KB | None | 0 0
  1. <?php
  2. require_once('db.php');
  3.  
  4. function getValues($struc) {
  5. $query = "SELECT DISTINCT option_name FROM Options WHERE struc = ? ORDER BY option_name";
  6. try {
  7. $stmt = $GLOBALS["link"]->prepare($query);
  8. $stmt->bindParam(1,$struc, PDO::PARAM_STR);
  9. $stmt->setFetchMode(PDO::FETCH_ASSOC);
  10. $stmt->execute();
  11. $result = $stmt->fetchAll();
  12. }
  13. catch(PDOException $e) {
  14. $error_message = $e->getMessage();
  15. die($error_message);
  16. }
  17.  
  18. return $result;
  19. }
  20. ?>
  21.  
  22. <html>
  23. <head>
  24. <script>
  25. $(function() {
  26. $( ".datepicker" ).datepicker({ dateFormat: 'yy-mm-dd' });
  27. });
  28. </script>
  29.  
  30. <title>Filter Results</title>
  31.  
  32. </head>
  33. <body>
  34.  
  35.  
  36.  
  37. <form action="certainty.php">
  38. <div class='pure-control-group'>
  39. <label for="species">Species</label>
  40. <select name='species'>
  41. <option value='any'>Any</option>
  42. <?php
  43. $strucArray = array("mammal","bird","noanimal","notinlist");
  44. foreach($strucArray as $struc) {
  45. echo "<optgroup label='" . ucwords($struc) . "'>";
  46. foreach(getValues($struc) as $specie) {
  47. echo "<option value='" . strip_tags($specie['option_name']) . "'>" . strip_tags($specie['option_name']) . "</option>";
  48. }
  49. echo "</optgroup>";
  50. }
  51. ?>
  52. </select>
  53. </div>
  54. <div class='pure-control-group'>
  55. <label for='sex'>Sex</label>
  56. <select name='sex'>
  57. <option value='any'>Any</option>
  58. <?php
  59. foreach(getValues('gender') as $gender) {
  60. echo "<option value='" . strip_tags($gender['option_name']) . "'>" . strip_tags($gender['option_name']) . "</option>";
  61. }
  62. ?>
  63. </select>
  64. </div>
  65. <div class='pure-control-group'>
  66. <label for='age'>Age</label>
  67. <select name='age'>
  68. <option value='any'>Any</option>
  69. <?php
  70. foreach(getValues('age') as $age) {
  71. echo "<option value='" . strip_tags($age['option_name']) . "'>" . strip_tags($age['option_name']) . "</option>";
  72. }
  73. ?>
  74. </select>
  75. </div>
  76. <div class='pure-control-group'>
  77. <label for='quality'>Quality</label>
  78. <select name='quality'>
  79. <option value='any'>Any</option>
  80. <?php
  81. foreach(getValues('quality') as $quality) {
  82. echo "<option value='" . strip_tags($quality['option_name']) . "'>" . strip_tags($quality['option_name']) . "</option>";
  83. }
  84. ?>
  85. </select>
  86. </div>
  87. <div class='pure-control-group'>
  88. <label for='camera'>Camera</label>
  89. <select name='camera'>
  90. <option value='any'>Any</option>
  91. <?php
  92. foreach(getValues('camera') as $camera) {
  93. echo "<option value='" . strip_tags($camera['option_name']) . "'>" . strip_tags($camera['option_name']) . "</option>";
  94. }
  95. ?>
  96. </select>
  97. </div>
  98. <div class='pure-control-group'>
  99. <label for='water'>Water</label>
  100. <select name='water'>
  101. <option value='any'>Any</option>
  102. <?php
  103. foreach(getValues('water') as $water) {
  104. echo "<option value='" . strip_tags($water['option_name']) . "'>" . strip_tags($water['option_name']) . "</option>";
  105. }
  106. ?>
  107. </select>
  108. </div>
  109. <div class='pure-control-group'>
  110. <label for='landuse'>Land Use</label>
  111. <select name='landuse'>
  112. <option value='any'>Any</option>
  113. <?php
  114. foreach(getValues('landuse') as $landuse) {
  115. echo "<option value='" . strip_tags($landuse['option_name']) . "'>" . strip_tags($landuse['option_name']) . "</option>";
  116. }
  117. ?>
  118. </select>
  119. </div>
  120. <div class='pure-control-group'>
  121. <label for='habitat'>Habitat</label>
  122. <select name='habitat'>
  123. <option value='any'>Any</option>
  124. <?php
  125. foreach(getValues('habitat') as $habitat) {
  126. echo "<option value='" . strip_tags($habitat['option_name']) . "'>" . strip_tags($habitat['option_name']) . "</option>";
  127. }
  128. ?>
  129. </select>
  130. </div>
  131. <!--
  132. <div class='pure-control-group'>
  133. <label for='habitatx'>HabitatX</label>
  134. <select name='habitatx'>
  135. <option value='any'>Any</option>
  136. <?php
  137. foreach(getValues('habitatx') as $habitatx) {
  138. echo "<option value='" . strip_tags($habitatx['option_name']) . "'>" . strip_tags($habitatx['option_name']) . "</option>";
  139. }
  140. ?>
  141. </select>
  142. </div>
  143. -->
  144. <div class='pure-control-group'>
  145. <label for='purpose'>Purpose</label>
  146. <select name='purpose'>
  147. <option value='any'>Any</option>
  148. <?php
  149. foreach(getValues('purpose') as $purpose) {
  150. echo "<option value='" . strip_tags($purpose['option_name']) . "'>" . strip_tags($purpose['option_name']) . "</option>";
  151. }
  152. ?>
  153. </select>
  154. </div>
  155. <div class='pure-control-group'>
  156. <label for='startdate'>Start Date</label>
  157. <input type='text' name='startdate' class='datepicker'>
  158. </div>
  159. <div class='pure-control-group'>
  160. <label for='enddate'>End Date</label>
  161. <input type='text' name='enddate' class='datepicker'>
  162. </div>
  163.  
  164. <div>
  165. <label for='minVotes'>Minimum Votes</label>
  166. <input type="text" name="minVotes">
  167. </div>
  168. <div>
  169. <label for="threshold">Evenness Threshold</label>
  170. <input type="text" name="threshold"><br/><br/>
  171. </div>
  172. <input type="submit" value="Submit">
  173.  
  174. </form>
  175.  
  176. <?php
  177.  
  178. $servername = "myeusql.dur.ac.uk";
  179. $username = "sxfm87";
  180. $password = "or54ange";
  181. $dbname = "Psxfm87_biodiversity";
  182.  
  183. $minVotes = $_REQUEST["minVotes"];
  184. $threshold = $_REQUEST["threshold"];
  185.  
  186. if ($minVotes != '' and $threshold != '') {
  187. if ($minVotes >= 0) {
  188. if ($threshold >= 0 and $threshold <= 1) {
  189.  
  190. $conn = new mysqli($servername, $username, $password, $dbname);
  191. $sql = "
  192. SELECT *
  193. FROM Photo
  194. INNER JOIN (SELECT photo_id, COUNT(*) AS votes FROM Animal GROUP BY photo_id) AS VoteCount
  195. ON VoteCount.photo_id = Photo.photo_id
  196. INNER JOIN (SELECT option_id, option_name FROM Options) AS AnimalNames
  197. ON AnimalNames.option_id = Photo.species
  198. WHERE evenness <= $threshold AND votes >= $minVotes
  199. ";
  200. $result = $conn->query($sql);
  201.  
  202. echo "Showing photos with at least ", $minVotes, " votes and evenness at most ", $threshold, " (", $result->num_rows , " results):<br/><br/>";
  203.  
  204. echo '<table style="width:75%"> <tr> <td>photo_id</td> <td>path</td> <td>species</td> <td>evenness</td> <td>votes</td> </tr>';
  205.  
  206. while ($row = $result->fetch_assoc()) {
  207. echo "<tr> <td>", $row["photo_id"], "</td> <td>", $row["dirname"], "/", $row["filename"], "</td> <td>", $row["option_name"], "</td> <td>", $row["evenness"], "</td> <td>", $row["votes"], "</td> </tr>" ;
  208. }
  209.  
  210. echo '</table>';
  211.  
  212. } else {
  213. echo "Threshold must be between 0 and 1.";
  214. }
  215. } else {
  216. echo "Minimum votes must be non-negative.";
  217. }
  218. } else {
  219. echo "Please enter a value into both fields.";
  220. }
  221.  
  222. ?>
  223.  
  224. </body>
  225.  
  226. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement