2016-12-30 3 views
0

空ではない配列を検証するときは、配列が正常に表示されますが、その前に実装したいif在庫がmin_stockより大きく、max_stockが在庫よりも小さい場合は、msgを実行します。それが動作重複していますかどうかを確認するために....を複製、私はこのデータベースにデータを送信する前にプロパティを検証する方法

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 into $ this-> json? –

+0

在庫が最小よりも大きいかどうかを検証したい場合は、maxが在庫よりも大きい場合、データを送信する前に –

関連する問題