Guest User

Untitled

a guest
Jan 14th, 2020
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.76 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  7. <title>CRUD</title>
  8.  
  9. <!-- Jquery -->
  10. <script
  11. src="https://code.jquery.com/jquery-3.4.1.js"
  12. integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU="
  13. crossorigin="anonymous"></script>
  14.  
  15. <!-- Add Ajax -->.
  16. <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
  17.  
  18. <!-- Bootstrap Core -->
  19. <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
  20.  
  21. </head>
  22.  
  23. <body>
  24. <div class="all-content-wrapper">
  25. <section class="container">
  26. <!-- Title -->
  27. <div class="page-heading">
  28. <h2>Di Di Co</h2>
  29. </div>
  30. <!-- Form for adding a new product -->
  31. <div class ="form-group" >
  32. <form action="test4.php" method="post" id = "d_form">
  33. <div class ="row">
  34. <div class="col-md-offset-2 col-md-8">
  35. <div class="panel-body">
  36. <div class="col-md-6">
  37. <div class="form-group">
  38. <label for="product_name" class="required" >Product Name:</label>
  39. <input type="text" class="form-control" id="product_name" name="name" required />
  40. </div>
  41. <div class="form-group">
  42. <label for="weight" class="required" >Weight:</label>
  43. <input type="text" class="form-control dup-check" id="weight" name="weight" required />
  44. <span class="dup-chek-details"></span>
  45. </div>
  46. <div class="form-group">
  47. <label for="height" class="required" >Height:</label>
  48. <input type="text" class="form-control dup-check" id="height" name="height" required />
  49. <span class="dup-chek-details"></span>
  50. </div>
  51. <div class="form-group">
  52. <label for="length" class="required" >Length:</label>
  53. <input type="text" class="form-control dup-check" id="length" name="length" required />
  54. <span class="dup-chek-details"></span>
  55. </div>
  56. <div class="form-group">
  57. <label for="width" class="required" >Width:</label>
  58. <input type="text" class="form-control dup-check" id="width" name="width" required />
  59. <span class="dup-chek-details"></span>
  60. </div>
  61. <div class="form-group">
  62. <input type="submit" value = "Add Product">
  63. </div>
  64. </div>
  65. </div>
  66. </div>
  67. </div>
  68. </form>
  69. <!-- Table displaying products -->
  70. <div class="panel panel-default">
  71. <div class="panel-heading">Products:</div>
  72. <div class="panel-body">
  73. <div class="table-responsive">
  74. <table class="table table-striped table-hover dataTable">
  75. <thead>
  76. <tr>
  77. <th width="100px">Name</th>
  78. <th>Weight (kg)</th>
  79. <th>Dimensions (in)</th>
  80. </tr>
  81. </thead>
  82. <tbody>
  83. <?php
  84.  
  85. // Class definition
  86. class Product
  87. {
  88. // Elected to use public variables so I don't have to write getters / setters.
  89.  
  90. // Every product has a unique name
  91. private $name;
  92. // Every product has weight ( in KG )
  93. private $weight;
  94.  
  95. // Every product has similar dimensions ( in CM ).
  96. private $width;
  97. private $length;
  98. private $height;
  99.  
  100. // This function will return the dimensions of the product in a readable format.
  101. public function getDimensions()
  102. {
  103. echo $this->height . "cm x " . $this->length . "cm x " . $this->width . "cm";
  104. }
  105.  
  106. // This function will return the weight of a product in a readable format.
  107. public function getWeight()
  108. {
  109. echo $this->weight . "kg";
  110. }
  111.  
  112. // This function will return the name of a product.
  113. public function getName()
  114. {
  115. echo $this->name;
  116. }
  117. // Implementation of constructor to make new products easily.
  118. // Default destructor is sufficient so I did not override it.
  119. public function __construct($a1, $a2, $a3, $a4, $a5)
  120. {
  121. $this->name = $a1;
  122. $this->weight = $a2;
  123. $this->width = $a3;
  124. $this->length = $a4;
  125. $this->height = $a5;
  126. }
  127. }
  128.  
  129. // Here we build a product based on what the user has entered
  130. $stack = array();
  131. // Defining variables and setting them to empty.
  132. $name = $weight = $height = $length = $width = "";
  133.  
  134. // Storage for all of the products created
  135. $allProducts = array();
  136.  
  137. // Assuming we've been provided the data via post we can build the object for display.
  138. if ($_SERVER["REQUEST_METHOD"] == "POST")
  139. {
  140. $weight = $_POST["weight"];
  141. $height = $_POST["height"];
  142. $length = $_POST["length"];
  143. $width = $_POST["width"];
  144. $name = $_POST["name"];
  145.  
  146. $newProduct = new Product($name, $weight, $width, $length, $height);
  147. // If a new product is added, put it in.
  148. array_push($allProducts, $newProduct);
  149. }
  150.  
  151. // Here I will create a table with all of the product info
  152. $fiddle = new Product("Fiddle", "1", "60", "20", "10");
  153. $dish = new Product("Dish", ".1", "30", "30", "5");
  154. $spoon = new Product("Spoon", ".05", "15", "5", "2");
  155.  
  156. // Add in the default products to the container.
  157. array_push($allProducts, $fiddle, $dish, $spoon);
  158.  
  159. // Used to loop through the array of data.
  160. $index = 0;
  161.  
  162. // Populate table based on our stored data.
  163. while
  164. (($row = $allProducts[$index]))
  165. {
  166. ?>
  167. <tr id = <?php echo $row->getName() ?>>
  168. <td><?php echo $row->getName() ?></td>
  169. <td><?php echo $row->getWeight()?></td>
  170. <td><?php echo $row->getDimensions() ?></td>
  171. </tr>
  172.  
  173. <?php
  174. //Increment the counter to loop through the array
  175. $index++;
  176. // So if we have arrived at an undefined part, instead of checking the undefined value we exit the loop (bug fix).
  177. if (!isset($allProducts[$index]))
  178. {
  179. break;
  180. }
  181. ?>
  182.  
  183. <?php
  184. // Close loop, seperated it to be concise.
  185. }
  186. ?>
  187. </tbody>
  188. </table>
  189. </div>
  190. </div>
  191. </div>
  192. </section>
  193. </div>
  194.  
  195.  
  196. <!-- Add validation of form on client side with JS -->
  197. <script>
  198.  
  199. $(document).ready(function(e){
  200. $("#d_form").validate({
  201. // Specify the validation rules
  202. rules: {
  203. product_name: {
  204. required: true,
  205. minlenght: 1
  206. },
  207. weight: {
  208. required: true,
  209. number:true
  210. },
  211. height: {
  212. required: true,
  213. number:true
  214. },
  215. length: {
  216. required: true,
  217. number:true
  218. },
  219. width: {
  220. required: true,
  221. number:true
  222. },
  223. },
  224.  
  225. submitHandler: function(form) {
  226. form.submit();
  227. }
  228. });
  229. });
  230. </script>
  231. </body>
  232. </html>
Advertisement
Add Comment
Please, Sign In to add comment