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