2016-12-30 56 views
0

我試圖做的是做一個驗證時,不是空的我的數組然後它會顯示成功添加,但在此之前,我想實現如果庫存大於min_stock,如果max_stock小於庫存,則執行一些味精。那麼如果不是重複....檢查它是否是複製它的工作原理,但它不是工作,當我嘗試實現這個我怎麼能驗證屬性之前發送我的數據到數據庫im新codeigniter

if ($stock > $min_stock) { 
      $this->json(array('min_stock' => 'el stock no puede ser mayor al min')); 
     }elseif ($max_stock < $stock) { 
      $this->json(array('min_stock' => 'el stock no puede ser mayor al max')); 
     }else{ 
     } 

控制器

public function addProduct(){ 
     $descripcion = $this->input->post('description'); 
     $cost_price = $this->input->post('cost_price'); 
     $selling_price = $this->input->post('selling_price'); 
     $wprice = $this->input->post('wprice'); 
     $min_stock = $this->input->post('min_stock'); 
     $stock = $this->input->post('stock'); 
     $max_stock = $this->input->post('max_stock'); 
     $data = array(
      'descripcion' => $descripcion, 
      'precio_compra' => $cost_price, 
      'precio_venta' => $selling_price, 
      'precio_mayoreo' => $wprice, 
      'existencia_minima' => $min_stock, 
      'existencia' => $stock, 
      'existencia_maxima' => $max_stock 
     ); 
     //$this->json(array('msg' => 'successfully added')); 
     //$this->json(array('duplicate' => 'product already exists')); 

     if (!empty($this->products->addProduct($data))) { 
      -->> before this $this->json(array('msg' => 'successfully added')); 
     }else{ 
      $this->json(array('duplicate' => 'product already exists')); 
     } 

     // the below code how can I implement into 
     if ($stock > $min_stock) { 
      $this->json(array('min_stock' => 'el stock no puede ser mayor al min')); 
     }elseif ($max_stock < $stock) { 
      $this->json(array('min_stock' => 'el stock no puede ser mayor al max')); 
     }else{ 
     } 
    } 

模型

public function addProduct($data){ 
     $this->db->select('descripcion'); 
     $this->db->from('storelte_articulos'); 
     $this->db->where('descripcion',$data['descripcion']); 
     $query = $this->db->get(); 
     return $query->num_rows() == 0 ? $this->db->insert('storelte_articulos',$data) : false; 
    } 

回答

0

重寫您的代碼在您的控制器中的$ data數組之後:

if ($stock > $min_stock) { 
 
    $this->json(array('min_stock' => 'el stock no puede ser mayor al min')); 
 
}elseif ($max_stock < $stock) { 
 
    $this->json(array('min_stock' => 'el stock no puede ser mayor al max')); 
 
} else { 
 
    if (!$this->products->isExistsProduct($data)) { 
 
     $this->products->addProduct($data) 
 
     $this->json(array('msg' => 'successfully added')); 
 
    }else{ 
 
     $this->json(array('duplicate' => 'product already exists')); 
 
    } 
 
}

重寫你的型號如下:

public function isExistsProduct($data){ 
 
     $this->db->select('descripcion'); 
 
     $this->db->from('storelte_articulos'); 
 
     $this->db->where('descripcion',$data['descripcion']); 
 
     $query = $this->db->get(); 
 
     return $query->num_rows() == 0 ? false : true; 
 
    } 
 

 
public function addProduct($data){ 
 
     $this->db->insert('storelte_articulos',$data) 
 
    }

+0

它充分驗證之前仍然發送的數據是正確的 –

+0

你想傳遞隨着min_stock消息msg放入$ this-> json? –

+0

我想驗證股票是否大於最小值,如果最大值大於股票,在發送數據之前 –

相關問題