Advertisement
Guest User

Untitled

a guest
Jul 4th, 2016
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.85 KB | None | 0 0
  1. <?php
  2. function resultToArray($result) {
  3. $rows = array();
  4. while($row = $result->fetch_assoc()) {
  5. $rows[] = $row;
  6. }
  7. return $rows;
  8. }
  9. $servername = "localhost";
  10. $username = "username";
  11. $password = "password";
  12. $dbname = "discgolf";
  13. $mysqli = new mysqli("$servername", "$username", "$password", "$dbname");
  14. if ($mysqli->connect_error){
  15. die("Connection failed: " . $mysqli->connect_error);
  16. }
  17. if(isSet($_POST["action"])){
  18. switch($_POST["action"]){
  19. case "removeRow":
  20. $row_id = $_POST["ID"];
  21. $stmt = $mysqli->prepare("DELETE FROM discideas WHERE id = ?");
  22. $stmt->bind_param("i", $row_id);
  23. $stmt->execute();
  24.  
  25. exit();
  26. break;
  27. case "saveRows":
  28. $input_values = $_POST["input_values"];
  29. foreach($input_values as $input_row){
  30. if($input_row[0] == "no"){
  31. $type = $input_row[1];
  32. $brand = $input_row[2];
  33. $name = $input_row[3];
  34. $plastic = $input_row[4];
  35.  
  36. $stmt = $mysqli->prepare("INSERT INTO discideas(type, brand, name, plastic) VALUES(?,?,?,?)");
  37. $stmt->bind_param("ssss", $type, $brand, $name, $plastic);
  38. $stmt->execute();
  39. }else{
  40. $id = $input_row[0];
  41. $type = $input_row[1];
  42. $brand = $input_row[2];
  43. $name = $input_row[3];
  44. $plastic = $input_row[4];
  45.  
  46. $stmt = $mysqli->prepare("UPDATE discideas SET type=?, brand=?, name=?, plastic=? WHERE id = ?");
  47. $stmt->bind_param("ssssi", $type, $brand, $name, $plastic, $id);
  48. $stmt->execute();
  49. }
  50. }
  51. $results = $mysqli->query("SELECT id, type, brand, name, plastic FROM discideas;");
  52. $newResults = resultToArray($results);
  53. echo json_encode($newResults);
  54. exit;
  55.  
  56. break;
  57. }
  58. }
  59. ?>
  60. <!DOCTYPE html>
  61. <html lang="en">
  62. <head>
  63. <meta charset="UTF-8">
  64. <title>Document</title>
  65. <!-- Jquery -->
  66. <script src="https://code.jquery.com/jquery-2.2.4.js" integrity="sha256-iT6Q9iMJYuQiMWNd9lDyBUStIq/8PuOW33aOqmvFpqI=" crossorigin="anonymous"></script>
  67. <!-- Bootstrap -->
  68. <script src="smart/node_modules/bootstrap/dist/js/bootstrap.js"></script>
  69. <link rel="stylesheet" href="../disc/smart/node_modules/bootstrap/dist/css/bootstrap.css">
  70. <link rel="stylesheet" href="../disc/smart/node_modules/bootstrap/dist/css/bootstrap-theme.css">
  71. <style type="text/css">
  72.  
  73. #discideas caption
  74. {
  75. text-align: center;
  76. font-weight: bold;
  77. }
  78.  
  79. #discideas
  80. {
  81. width: 500px;
  82. }
  83.  
  84. #edit
  85. {
  86. text-align: center;
  87. width: 500px;
  88. }
  89.  
  90. #wat
  91. {
  92. width: 500px;
  93. }
  94. </style>
  95. <script>
  96. $(document).ready(function(){
  97. $(document).on("click", "#edittable", function(e){
  98. e.preventDefault();
  99. $(this).val("Save");
  100. $(this).attr("id", "saveBtn");
  101. $("#add").show();
  102. $("#cancel").show();
  103.  
  104. $("table tbody tr:not(.empty)").each(function(){
  105. $(" td", this).each(function(){
  106. var cell_content = $(this).text();
  107. $(this).html("<input type='text' value='"+cell_content+"'>");
  108. });
  109. $(this).append("<td class='removeCell'><a href='#' class='deleteRow' data-id='"+ $(this).data("id") +"'>Kustuta</a></td>");
  110.  
  111. });
  112.  
  113. return false;
  114. });
  115. $(document).on("click", "#cancel", function(e){
  116. e.preventDefault();
  117. $("#saveBtn").val("Edit");
  118. $("#saveBtn").attr("id", "edittable");
  119. $("#add").hide();
  120. $("#cancel").hide();
  121.  
  122. $("table tbody tr:not(.empty)").each(function(){
  123. var isEmpty = 0;
  124. $(" td input", this).each(function(){
  125. if($(this).val() != ""){
  126. isEmpty++;
  127. }
  128. var cell_content = $(this).val();
  129. $(this).parent("td").html(cell_content);
  130. });
  131. if(isEmpty == 0){
  132. $(this).remove();
  133. }
  134. });
  135. $(".removeCell").remove();
  136.  
  137. return false;
  138. });
  139. $(document).on("click", ".deleteRow", function(e){
  140. e.preventDefault();
  141.  
  142. var that = this;
  143. var DBID = $(this).data("id");
  144. $.ajax({
  145. "url": "randomtest.php", // Muuda faili nime
  146. "method": "POST",
  147. "data": {
  148. "action": "removeRow",
  149. "ID": DBID,
  150. },
  151. "success": function(response){
  152. $(that).parent("td").parent("tr").remove();
  153. }
  154. });
  155.  
  156. return false;
  157. });
  158. $(document).on("click", "#saveBtn", function(e){
  159. e.preventDefault();
  160. $(this).val("Edit");
  161. $(this).attr("id", "edittable");
  162. $("#add").hide();
  163. $("#cancel").hide();
  164. $(".removeCell").remove();
  165.  
  166. var input_values = [];
  167. $("table tbody tr").each(function(rowKey, rowEl){
  168. var single_row = [];
  169. if(typeof $(this).data("id") !== "undefined"){
  170. single_row.push($(this).data("id"));
  171. }else{
  172. single_row.push("no");
  173. }
  174. $("td input", this).each(function(key, el){
  175. single_row.push($(this).val());
  176. });
  177. input_values.push(single_row);
  178. });
  179.  
  180. $.ajax({
  181. "url": "randomtest.php", //Muuda faili nimi
  182. "method": "POST",
  183. "dataType": "JSON",
  184. "data": {
  185. "action": "saveRows",
  186. "input_values": input_values
  187. },
  188. "success": function(response){
  189. var newHTML = "";
  190. response.forEach(function(el){
  191. newHTML += '<tr data-id="'+el.id+'">';
  192. newHTML += '<td>'+el.type+'</td>';
  193. newHTML += '<td>'+el.brand+'</td>';
  194. newHTML += '<td>'+el.name+'</td>';
  195. newHTML += '<td>'+el.plastic+'</td>';
  196. newHTML += '</tr>';
  197. });
  198. $(".table tbody").html(newHTML);
  199. }
  200. });
  201.  
  202. return false;
  203. });
  204. $(document).on("click", "#add", function(e){
  205. e.preventDefault();
  206. $(".empty").remove();
  207.  
  208. var inputEl = "<tr>";
  209. inputEl += "<td>";
  210. inputEl += "<input type='text' placeholder='Type...'>";
  211. inputEl += "</td>";
  212. inputEl += "<td>";
  213. inputEl += "<input type='text' placeholder='Brand...'>";
  214. inputEl += "</td>";
  215. inputEl += "<td>";
  216. inputEl += "<input type='text' placeholder='Name...'>";
  217. inputEl += "</td>";
  218. inputEl += "<td>";
  219. inputEl += "<input type='text' placeholder='Plastic...'>";
  220. inputEl += "</td>";
  221. inputEl += "</tr>";
  222. $("table tbody").append(inputEl);
  223.  
  224. return false;
  225. });
  226. });
  227. </script>
  228. </head>
  229. <body>
  230.  
  231. <?php
  232. // $stmt = $mysqli->prepare("SELECT * FROM discideas;");
  233. // $stmt->bind_result($id, $type, $brand, $name, $plastic);
  234. // $stmt->execute();
  235. $results = $mysqli->query("SELECT * FROM discideas;");
  236. ?>
  237.  
  238. <table class="table" border="1" id="discideas">
  239. <caption>Disc ideas</caption>
  240. <thead>
  241. <tr>
  242. <th>Type</th>
  243. <th>Brand</th>
  244. <th>Name</th>
  245. <th>Plastic</th>
  246. </tr>
  247. </thead>
  248. <tbody>
  249. <?php
  250. if($results->num_rows === 0){
  251. ?>
  252. <tr class='empty'>
  253. <td colspan='5' align='center'>
  254. <b>Ühtegi ideed ei leitud!</b>
  255. </td>
  256. </tr>
  257. <?php
  258. }else{
  259. foreach($results as $result){
  260. if(!empty($result)){
  261. ?>
  262. <tr data-id="<?php echo $result["id"]; ?>">
  263. <td><?php echo $result["type"]; ?></td>
  264. <td><?php echo $result["brand"]; ?></td>
  265. <td><?php echo $result["name"]; ?></td>
  266. <td><?php echo $result["plastic"]; ?></td>
  267. </tr>
  268. <?php
  269. }
  270. }
  271. }
  272. $mysqli->close();
  273. ?>
  274. </tbody>
  275. </table>
  276. <div id="edit">
  277. <input type="button" id="add" value="Add row" style="display:none;">
  278. <input type="button" id="edittable" value="Edit">
  279. <input type="button" id="cancel" value="Cancel" style="display:none;">
  280. </div>
  281.  
  282. <br>
  283.  
  284. <p id="wat">
  285. Vajutad Edit nuppu, Edit nupu asemele tekib sama koha peale nupp Save.
  286. Tabeli ridadele, kus on tekst, tekib teksti asemele input field ja sinna sisse jääb see sama väärtus, mis enne tabelis oli. Muudad teksti input fieldis ja vajutad nuppu save, uuendab baasis andmed ära ja kuvab uusi andmeid.
  287. </p>
  288. <p>Panin siia mingi asja, mis tegime kunagi Romiliga, aga ma ei suuda läbi hammustada ja järgi teha.</p>
  289. <a href="recipes.7z">recipes.7z</a>
  290.  
  291. </body>
  292. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement