Search code examples
phpcodeignitercodeigniter-3

How can i make a stop if quantity is not enough? PHP


can anyone teach me how can i make a stop if the quantity is not enough? i'm trying to develop a pos and inventory system using codeigniter framework and here is my portion of code

if($prodid){
    $inputQuantity = (int)$this->input->post('qty')[$x];
    $this->db->where('id', $prodid);
    $this->db->set('qty', "qty-$inputQuantity", false);
    $this->db->update('products');
}

how can i make it stop if the quantity of the products reach 0 and when i process a new transaction with 0 qty of the products it will alert me of not enough stock can anyone help?


Solution

  • You may want check for availabe product stock

    
    // in some model
    public function check_stock($productId)
    {
       $product = $this->db->get_where($table_name, ['product_id' => $productId])->row();
       
       if(isset($product))
       {
          if($product->stock == 0)
          {
              // There is no more products of this type
              // For example
              die("No more stock");
          }
          else
          {
              // Do other stuff is there is enought stock
              // For example
              // Let the user buy the product
          }
       }
    }
    

    I'm assuming you have a products table with stock and product_id columns