如何使php / mysqli DAL更干燥

时间:2018-08-10 02:38:27

标签: php mysql ajax mysqli

我有一堂课,我正尝试尽可能避免参加WET。到目前为止,我已经成功地分配了Mysqli对象以及通过该类通过jquery ajax发送到文件的各个数据变量。我将动作从jquery发送到functions.php,在其中使用switch语句执行不同的方法。我忍不住想,有些事情我可以做得更好,以确保我的代码更干燥。我正在寻找有关如何使代码看起来更好的建议,我想使用诸如this one之类的DAL,但是我不太确定如何在检索到数据后将其包装在html中。此外,我不认为这会减少代码的复杂度。

<?php
class Mysql
{
    private $sql;
    private $query;
    private $id;
    private $product_name;
    private $label;
    private $starting_inventory;
    private $minimum_required;
    public function __construct()
    {
        $this->sql = new mysqli('localhost', 'root', '*****', 'ivm');
        if ($this->sql->connect_error) {
            die("Connection failed: " . $this->sql->connect_error);
        }
        $this->id = (isset($_POST['id'])) ? $_POST['id'] : 3;
        $this->product_name =(isset($_POST['product_name'])) ? $_POST['product_name']:3;
        $this->label=(isset($_POST['label'])) ? $_POST['label']:3;
        $this->starting_inventory=(isset($_POST['starting_inventory'])) ? $_POST['starting_inventory']:3;
        $this->minimum_required=(isset($_POST['minimum_required'])) ? $_POST['minimum_required']:3;
    }
    public function vendors()
    {
        $queryText = "SELECT * from vendors";
        $query=$this->sql->query($queryText);
        if ($query->num_rows > 0) {
            while ($row = $query->fetch_assoc()) {
                echo '<option value="'.$row["supplier"].'">'.$row["supplier"].'</option>';
            }
        } else {
            echo "0 results";
        }
        $this->sql->close();
    }
    public function items()
    {
        $queryText = "SELECT * from products";
        $query=$this->sql->query($queryText);
        if ($query->num_rows > 0) {
            while ($row = $query->fetch_assoc()) {
                echo '<option value="'.$row["ProductName"].'">'.$row["ProductName"].'</option>';
            }
        } else {
            echo "0 results";
        }
        $this->sql->close();
    }
    public function get_incoming_product()
    {
        $queryText = "SELECT incoming.PurchaseDate, products.ProductName, incoming.NumReceived, vendors.supplier
        FROM incoming, vendors, products
        WHERE vendors.id = incoming.SupplierId and incoming.ProductId = products.id and incoming.id = $this->id limit 10";
        $query= $this->sql->query($queryText);
        if ($query->num_rows > 0) {
            // echo view('products', 'open');
            while ($row = $query->fetch_assoc()) {
                echo '

                <div class="container manage-table">
                    <h1>Manage Product</h1>
                    <div class="form-row">
                    <div class="form-group col-sm-6">
                    <label for="product-name">Product Name</label>
                    <select class="form-control item-dropdown">
                    </select>
                    <label for="purchase-date">Purchase Date</label>
                    <input id="purchase-date" value="'.$row["PurchaseDate"].'" class="purchase-date form-control"></input>
                    </div>
                    <div class="form-group col-sm-6">
                    <label for="number-received">Number Received</label>
                    <input id="number-received" value="'.$row["NumReceived"].'" class="number-received form-control"></input>
                    <label for="vendor">Vendor</label>
                    <select class="form-control vendor-dropdown">
                    </select>
                    </div>
                    </div>
                    <button data-id="'.$row["id"].'" data-action="edit_product" class="get edit-product btn btn-primary">Confirm</button></br>
                    <p class="parCreate"></p>
                </div>
                                 ';
            }
            // echo view('products', 'close');
        } else {
            echo "0 results";
        }
        $this->sql->close();
    }
    public function view_incoming()
    {
        $queryText = "SELECT incoming.PurchaseDate, products.ProductName, incoming.NumReceived, vendors.supplier, incoming.id
        FROM incoming, vendors, products
        WHERE vendors.id = incoming.SupplierId and incoming.ProductId = products.id limit 10";
        $query = $this->sql->query($queryText);
        echo view('incoming', 'open');
        if ($query->num_rows > 0) {
            while ($row = $query->fetch_assoc()) {
                echo '
                <tr class="edit_row">
         <td>'.$row["PurchaseDate"].'
                 <div data-id="'.$row["id"].'" class="manage">
                 <i data-id="'.$row["id"].'" data-action="confirm_delete" class="fas fa-times edit incoming get"></i>
                 <i data-id="'.$row["id"].'" data-action="" class="fas fa-plus edit get"></i>
                 <i data-id="'.$row["id"].'" data-action="get_incoming_product" class="fas fa-pencil-alt edit single-incoming get"></i>
                 </div>
         </td>
         <td>'.$row["ProductName"].'</td>
         <td>'.$row["NumReceived"].'</td>
         <td>'.$row["supplier"].'</td>
         </tr>
         ';
            }
        } else {
            echo "0 results";
        }
        echo view('incoming', 'close');
        $this->sql->close();
    }
    public function delete_product()
    {
        $queryText = "DELETE from products where id = $this->id";
        if ($this->sql->query($queryText) === true) {
            echo 'Record updated successfully '.$id;
        } else {
            echo "Error updating record: " . $this->sql->error;
        }
        $this->sql->close();
    }
    public function confirm_delete()
    {
        $queryText = "SELECT * from products where id = $this->id";
        $query= $this->sql->query($queryText);
        if ($query->num_rows > 0) {
            // echo view('products', 'open');
            while ($row = $query->fetch_assoc()) {
                echo '
                <div class="container manage-table">
                    <h1>Delete Product</h1>
                    <h2 class="">Are you sure? Delete '.$row["ProductName"].'?</h2>
                    <button data-id="'.$row["id"].'" data-action="delete_product" class="get delete-product btn btn-primary">Confirm</button></br>
                    <p class="parCreate"></p>
                </div>
                                 ';
            }
            // echo view('products', 'close');
        } else {
            echo "0 results";
        }
        $this->sql->close();
    }
    public function get_product()
    {
        $queryText = "SELECT * from products where id = $this->id";
        $query= $this->sql->query($queryText);
        if ($query->num_rows > 0) {
            // echo view('products', 'open');
            while ($row = $query->fetch_assoc()) {
                echo '

                <div class="container manage-table">
                    <h1>Manage Product</h1>
                    <div class="form-row">
                    <div class="form-group col-sm-6">
                    <label for="product-name">Product Name</label>
                    <input id="product-name" value="'.$row["ProductName"].'" class="product-name form-control"></input>
                    <label for="label">Label</label>
                    <input id="label" value="'.$row["Label"].'" class="label form-control"></input>
                    </div>
                    <div class="form-group col-sm-6">
                    <label for="starting-inventory">Starting Inventory</label>
                    <input id="starting-inventory" value="'.$row["StartingInventory"].'" class="starting-inventory form-control"></input>
                    <label for="minimum-required">Minimum Required</label>
                    <input id="minimum-required" value="'.$row["MinimumRequired"].'" class="minimum-required form-control"></input>
                    </div>
                    </div>
                    <button data-id="'.$row["id"].'" data-action="edit_product" class="get edit-product btn btn-primary">Confirm</button></br>
                    <p class="parCreate"></p>
                </div>
                                 ';
            }
            // echo view('products', 'close');
        } else {
            echo "0 results";
        }
        $this->sql->close();
    }
    public function edit_product()
    {
        $queryText = "UPDATE products
        set ProductName = '$this->product_name',
        Label = '$this->label',
        StartingInventory = $this->starting_inventory,
        MinimumRequired = $this->minimum_required
        where id = $this->id
        ";
        if ($this->sql->query($queryText) === true) {
            echo "Record updated successfully";
        } else {
            echo "Error updating record: " . $this->sql->error;
        }
        $this->sql->close();
    }
    public function view_products()
    {
        $queryText = "SELECT * from products";
        $query= $this->sql->query($queryText);
        if ($query->num_rows > 0) {
            echo view('products', 'open');
            while ($row = $query->fetch_assoc()) {
                echo '
                <tr class="edit_row">
                 <td>'.$row["ProductName"].'
                 <div data-id="'.$row["id"].'" class="manage">
                 <i data-id="'.$row["id"].'" data-action="confirm_delete" class="fas fa-times edit get"></i>
                 <i data-id="'.$row["id"].'" data-action="" class="fas fa-plus edit get"></i>
                 <i data-id="'.$row["id"].'" data-action="get_product" class="fas fa-pencil-alt edit get"></i>
                 </div>
                 </td>
                 <td>'.$row["Label"].'</td>
                 <td>'.$row["StartingInventory"].'</td>
                 <td>'.$row["MinimumRequired"].'</td>
                 </tr>
                 ';
            }
            echo view('products', 'close');
        } else {
            echo "0 results";
        }
        $this->sql->close();
    }

    public function add_new_product()
    {
        $prodName= (isset($_POST['prodName'])) ? $_POST['prodName'] : 3;
        $starting= (isset($_POST['starting'])) ? $_POST['starting'] : 3;
        $minimum= (isset($_POST['minimum'])) ? $_POST['minimum'] : 3;
        $queryText = "insert into products (ProductName, StartingInventory, MinimumRequired)
        VALUES ('$prodName', $starting, $minimum)";
        $query= $this->sql->query($queryText);
        $this->sql->close();
    }
    public function add_purchase()
    {
        $queryText = "SELECT * FROM products";
        $query= $this->sql->query($queryText);
        if ($query->num_rows > 0) {
            // echo view('current_inv', 'open');
            echo '<option>Select a product:</option>';
            while ($row = $query->fetch_assoc()) {
                echo '
         <option>'.$row["ProductName"].'</option>
         ';
            }
            // echo view('current_inv', 'close');
        } else {
            echo "0 results";
        }
        $this->sql->close();
    }
    public function prod_dropdown()
    {
        $queryText = "SELECT * FROM products";
        $query= $this->sql->query($queryText);
        if ($query->num_rows > 0) {
            // echo view('current_inv', 'open');
            echo '<option>Select a product:</option>';
            while ($row = $query->fetch_assoc()) {
                echo '
         <option>'.$row["ProductName"].'</option>
         ';
            }
            // echo view('current_inv', 'close');
        } else {
            echo "0 results";
        }
        $this->sql->close();
    }
    public function get_current_inv()
    {
        $queryText = 'SELECT p.id, coalesce(ig.NumReceived,0) as NumReceived,
        coalesce(og.NumShipped,0) as NumShipped, p.Label,
        coalesce((p.StartingInventory-og.NumShipped+ig.NumReceived), p.StartingInventory)
        as OnHand, p.ProductName,
        p.StartingInventory, p.MinimumRequired
        from products p
        left outer join (
            select productid, sum(NumReceived) as NumReceived
            from incoming
            group by productid
        ) as ig on p.id = ig.productid
        left outer join (
            select productid, sum(NumberShipped) as NumShipped
            from outgoing
            group by productid
        ) as og on p.id = og.productid';
        $query= $this->sql->query($queryText);
        if ($query->num_rows > 0) {
            echo view('current_inv', 'open');
            while ($row = $query->fetch_assoc()) {
                echo '
                <tr>
         <td>'.$row["ProductName"].'</td>
         <td>'.$row["Label"].'</td>
         <td>'.$row["StartingInventory"].'</td>
         <td>'. $row["NumShipped"].'</td>
         <td>'.$row["NumReceived"].'</td>
         <td>'.$row["OnHand"].'</td>
         <td>'.$row["MinimumRequired"].'</td>
         </tr>
         ';
            }
            echo view('current_inv', 'close');
        } else {
            echo "0 results";
        }
        $this->sql->close();
    }
    public function get_incoming()
    {
        $queryText = "SELECT incoming.PurchaseDate, products.ProductName, incoming.NumReceived, vendors.supplier
        FROM incoming, vendors, products
        WHERE vendors.id = incoming.SupplierId and incoming.ProductId = products.id limit 10";
        $query = $this->sql->query($queryText);
        echo view('incoming', 'open');
        if ($query->num_rows > 0) {
            while ($row = $query->fetch_assoc()) {
                echo '
                <tr>
         <td>'.$row["PurchaseDate"].'</td>
         <td>'.$row["ProductName"].'</td>
         <td>'.$row["NumReceived"].'</td>
         <td>'. $row["supplier"].'</td>
         </tr>
         ';
            }
        } else {
            echo "0 results";
        }
        echo view('incoming', 'close');
        $this->sql->close();
    }
    public function get_outgoing()
    {
        $queryText = "SELECT * FROM outgoing limit 10 offset 0";
        $query = $this->sql->query($queryText);
        if ($query->num_rows > 0) {
            echo view('outgoing', 'open');
            while ($row = $query->fetch_assoc()) {
                echo '
                <tr>
         <td>'.$row["First"].'</td>
         <td>'.$row["Middle"].'</td>
         <td>'.$row["Last"].'</td>
         <td>'. $row["ProductId"].'</td>
         <td>'.$row["NumberShipped"].'</td>
         <td>'.$row["OrderDate"].'</td>
         </tr>
         ';
            }
            echo view('outgoing', 'close');
        } else {
            echo "0 results";
        }
        $this->sql->close();
    }


    public function get_archive($table, $name, $page)
    {
        $page--;
        $page *= 9;
        return "SELECT * FROM $table limit 10 offset $page";
    }
    public function num_shipped($pid)
    {
        return "SELECT SUM(NumberShipped) FROM outgoing WHERE ProductId = $pid";
    }
}
$action= (isset($_POST['action'])) ? $_POST['action'] : 3;
$object = new Mysql();
switch ($action) {
  // case 'get_current_inv': get_current_inv();
  case 'get_current_inv': $object->get_current_inv();
  break;
  case 'get_outgoing': $object->get_outgoing();
  break;
  case 'get_incoming': $object->get_incoming();
  break;
  case 'prod_dropdown': $object->prod_dropdown();
  break;
  case 'add_new_product': $object->add_new_product();
  break;
  case 'view_products': $object->view_products();
  break;
  case 'get_product': $object->get_product();
  break;
  case 'edit_product': $object->edit_product();
  break;
  case 'confirm_delete': $object->confirm_delete();
  break;
  case 'delete_product': $object->delete_product();
  break;
  case 'view_incoming': $object->view_incoming();
  break;
  case 'get_incoming_product': $object->get_incoming_product();
  break;
  case 'items': $object->items();
  break;
  case 'vendors': $object->vendors();
  break;
}

function view($view, $operate)
{
    if ($operate=='close') {
        return '</tbody>
        </table>
        </div>
        ';
    }
    switch ($view) {
    case 'products':
    return '
    <div class="table-wrapper">
    <table class="fl-table">
      <thead>
        <tr>
            <th>Name</th>
            <th>Label</th>
            <th>Starting Inventory</th>
            <th>Minimum Required</th>
        </tr>
        </thead>
        <tbody>
        ';
    case 'current_inv':
      return '
      <div class="table-wrapper">
      <table class="fl-table">
        <thead>
          <tr>
              <th>Name</th>
              <th>Label</th>
              <th>Current Inventory</th>
              <th>Shipped</th>
              <th>Incoming</th>
              <th>On Hand</th>
              <th>Minimum Required</th>
          </tr>
          </thead>
          <tbody>
          ';
      case 'incoming':
        return '
      <div class="table-wrapper">
      <table class="fl-table">
        <thead>
        <tr>
            <th>Purchase Date</th>
            <th>Product Name</th>
            <th>Number Received</th>
            <th>Supplier</th>
        </tr>
        </thead>
        <tbody>
          ';
      case 'outgoing':
        return '
        <div class="table-wrapper">
        <table class="fl-table">
          <thead>
            <tr>
            <th>First Name</th>
            <th>Middle Name</th>
            <th>Last Name</th>
            <th>Product ID</th>
            <th>Number Shipped</th>
            <th>Order Date</th>
        </tr>
        </thead>
        <tbody>
              ';
    }
}

0 个答案:

没有答案