Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="utf-8" />
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <title>CRUD</title>
- <!-- Jquery -->
- <script
- src="https://code.jquery.com/jquery-3.4.1.js"
- integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU="
- crossorigin="anonymous"></script>
- <!-- Add Ajax -->.
- <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
- <!-- Bootstrap Core -->
- <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
- </head>
- <body>
- <div class="all-content-wrapper">
- <section class="container">
- <!-- Title -->
- <div class="page-heading">
- <h2>Di Di Co</h2>
- </div>
- <!-- Form for adding a new product -->
- <div class ="form-group" >
- <form action="test4.php" method="post" id = "d_form">
- <div class ="row">
- <div class="col-md-offset-2 col-md-8">
- <div class="panel-body">
- <div class="col-md-6">
- <div class="form-group">
- <label for="product_name" class="required" >Product Name:</label>
- <input type="text" class="form-control" id="product_name" name="name" required />
- </div>
- <div class="form-group">
- <label for="weight" class="required" >Weight:</label>
- <input type="text" class="form-control dup-check" id="weight" name="weight" required />
- <span class="dup-chek-details"></span>
- </div>
- <div class="form-group">
- <label for="height" class="required" >Height:</label>
- <input type="text" class="form-control dup-check" id="height" name="height" required />
- <span class="dup-chek-details"></span>
- </div>
- <div class="form-group">
- <label for="length" class="required" >Length:</label>
- <input type="text" class="form-control dup-check" id="length" name="length" required />
- <span class="dup-chek-details"></span>
- </div>
- <div class="form-group">
- <label for="width" class="required" >Width:</label>
- <input type="text" class="form-control dup-check" id="width" name="width" required />
- <span class="dup-chek-details"></span>
- </div>
- <div class="form-group">
- <input type="submit" value = "Add Product">
- </div>
- </div>
- </div>
- </div>
- </div>
- </form>
- <!-- Table displaying products -->
- <div class="panel panel-default">
- <div class="panel-heading">Products:</div>
- <div class="panel-body">
- <div class="table-responsive">
- <table class="table table-striped table-hover dataTable">
- <thead>
- <tr>
- <th width="100px">Name</th>
- <th>Weight (kg)</th>
- <th>Dimensions (in)</th>
- </tr>
- </thead>
- <tbody>
- <?php
- // Class definition
- class Product
- {
- // Elected to use public variables so I don't have to write getters / setters.
- // Every product has a unique name
- private $name;
- // Every product has weight ( in KG )
- private $weight;
- // Every product has similar dimensions ( in CM ).
- private $width;
- private $length;
- private $height;
- // This function will return the dimensions of the product in a readable format.
- public function getDimensions()
- {
- echo $this->height . "cm x " . $this->length . "cm x " . $this->width . "cm";
- }
- // This function will return the weight of a product in a readable format.
- public function getWeight()
- {
- echo $this->weight . "kg";
- }
- // This function will return the name of a product.
- public function getName()
- {
- echo $this->name;
- }
- // Implementation of constructor to make new products easily.
- // Default destructor is sufficient so I did not override it.
- public function __construct($a1, $a2, $a3, $a4, $a5)
- {
- $this->name = $a1;
- $this->weight = $a2;
- $this->width = $a3;
- $this->length = $a4;
- $this->height = $a5;
- }
- }
- // Here we build a product based on what the user has entered
- $stack = array();
- // Defining variables and setting them to empty.
- $name = $weight = $height = $length = $width = "";
- // Storage for all of the products created
- $allProducts = array();
- // Assuming we've been provided the data via post we can build the object for display.
- if ($_SERVER["REQUEST_METHOD"] == "POST")
- {
- $weight = $_POST["weight"];
- $height = $_POST["height"];
- $length = $_POST["length"];
- $width = $_POST["width"];
- $name = $_POST["name"];
- $newProduct = new Product($name, $weight, $width, $length, $height);
- // If a new product is added, put it in.
- array_push($allProducts, $newProduct);
- }
- // Here I will create a table with all of the product info
- $fiddle = new Product("Fiddle", "1", "60", "20", "10");
- $dish = new Product("Dish", ".1", "30", "30", "5");
- $spoon = new Product("Spoon", ".05", "15", "5", "2");
- // Add in the default products to the container.
- array_push($allProducts, $fiddle, $dish, $spoon);
- // Used to loop through the array of data.
- $index = 0;
- // Populate table based on our stored data.
- while
- (($row = $allProducts[$index]))
- {
- ?>
- <tr id = <?php echo $row->getName() ?>>
- <td><?php echo $row->getName() ?></td>
- <td><?php echo $row->getWeight()?></td>
- <td><?php echo $row->getDimensions() ?></td>
- </tr>
- <?php
- //Increment the counter to loop through the array
- $index++;
- // So if we have arrived at an undefined part, instead of checking the undefined value we exit the loop (bug fix).
- if (!isset($allProducts[$index]))
- {
- break;
- }
- ?>
- <?php
- // Close loop, seperated it to be concise.
- }
- ?>
- </tbody>
- </table>
- </div>
- </div>
- </div>
- </section>
- </div>
- <!-- Add validation of form on client side with JS -->
- <script>
- $(document).ready(function(e){
- $("#d_form").validate({
- // Specify the validation rules
- rules: {
- product_name: {
- required: true,
- minlenght: 1
- },
- weight: {
- required: true,
- number:true
- },
- height: {
- required: true,
- number:true
- },
- length: {
- required: true,
- number:true
- },
- width: {
- required: true,
- number:true
- },
- },
- submitHandler: function(form) {
- form.submit();
- }
- });
- });
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment