0

ユーザーが製品 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 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           
4

1 に答える 1

1

コールバック関数を使用できます。ここを読む

$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データベースで利用可能な数量をチェックします。模型で確認することをお勧めします。

編集:

インデックスに基づいて一致する必要がある2つの配列があるため、このタイプの検証には上記のようなフォーム検証を使用しないことをお勧めします。コールバック関数を使用する場合、作業している製品の数量がわかりません。

私の推奨事項:

$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関数が機能するはずです。

2番目の編集:

$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;
于 2012-05-23T07:27:36.500 に答える