产品可用性检查

时间:2012-05-23 07:02:33

标签: php codeigniter-2

我有一个表单,用户可以在其中输入产品ID及其数量。我想要实现的是当用户提交表单时,在控制器中我想检查数据库以查看提供的产品的数量是否有库存,如果不是我想在表单中显示验证错误消息。如果一切正常,我想继续下一步。

如果我想使用Codeigniter的默认验证库来实现这一目标,请告诉我控制器的外观如何?

这是我的数据库表名:product

 product_id     category_id     product_name    product_price   product_stock
    1                1          Mango Juice         25                100
    2                2           Pepsi              10                  0

这是我的查看档案 - 我的表格(点击此链接查看form

<form name="form" action="base_url/my_controller/function" method="post">
   <label>One</label> 
  Product ID:<input type="text" name="productid[]" value=""> 
  Product Quantity: <input type="text" name="quantity[]" value=""> <br>

 <label>Two</label> 
 Product ID:<input type="text" name="productid[]" value=""> 
 Product Quantity: <input type="text" name="quantity[]" value=""> <br>

 <!-- there may be more inputs like above (users can create new inputs 
 as many as they want) // I have a jquery function to create new rows-->
 <input type="submit" value="submit">
 </form>

编辑新部分

function test(){

$productid = ($_POST['productid']);
$quantity = ($_POST['quantity']);


for($i = 0; $i < count($productid); $i++){
     $result=$this->enquiry($productid[$i],$quantity[$i]);

     }

/* Now I am stuck here. I don't understand how to find out if the
      products are available or not. If not I want to show the 
      error message in the form :(   */

}//function ends

function enquiry($productid,$quantity){

 $query = $this->db->query("SELECT product_stock FROM products 
                                  WHERE product_id=$productid");

if ($query->num_rows() > 0){

          foreach ($query->result() as $row){

      $product_stock=$row->product_stock;              

    }                 
 }                 
   if($product_stock>$quantity) { return FALSE;   }         

    else { return  TRUE; }              
 }//function Ends           

1 个答案:

答案 0 :(得分:1)

您可以使用回调功能。阅读here

$this->form_validation->set_rules('quantity[]','Quantity', 'required|callback_quantity_check');

public function quantity_check($quantity) {

     if ($this->is_available_in_stock($this->input->post('product_id[]'),$quantity)) {
        return TRUE;
     }
     else 
     {
        $this->form_validation->set_message('quantity_check', 'error');
        return FALSE;
     }
}

is_available_in_stock将在数据库中检查可用数量。建议使用模型进行检查。

编辑:

我建议不要使用上面提到的表单验证进行此类验证,因为你有两个需要根据索引匹配的数组。使用回调功能时,您不知道您正在使用哪个产品的数量。

我的建议:

$product_ids = $this->input->post('product_id[]');
$quantities = $this->input->post('quantity[]');

//ensure the arrays are identical in size.
if (count($product_ids) == count($quantities) {

   for ($counter == 0; $counter < count($product_ids) ; $counter++) {
       $result = $this->is_available_in_stock($product_ids[counter], $quantities[counter]);
   }
}

使用此代码,您编写的is_available_in_stock函数应该有效。

第二次编辑:

$not_available_products = array();
for($i = 0; $i < count($productid); $i++){
     $result=$this->enquiry($productid[$i],$quantity[$i]);
     if ($result == FALSE) {
          array_push($not_available_products, array($productid[$i] => $quantity[$i]));
     }
}
return $not_available_products;