title: Product availability checking
body: <p>I have a form where users can enter product id and its quantity. What I am trying to acheive is when the user submits the form, in the controller I want to check in database to see if the provided product's quantity is available in stock, if not I want to display a validation error message in the form. If everything is okay I want to proceed to the next step. </p>
  
  <p>Could you please show me how my controller should look like if I want to achieve this using Codeigniter's default validation library?</p>
  
  <p>This is my db table name: product</p>
  
  <pre><code> product_id     category_id     product_name    product_price   product_stock
      1                1          Mango Juice         25                100
      2                2           Pepsi              10                  0
  </code></pre>
  
  <p>This is my View File -- My Form (check this link to see the <a href="http://jsfiddle.net/ms3eB/" rel="nofollow">form</a>)</p>
  
  <pre><code>&lt;form name="form" action="base_url/my_controller/function" method="post"&gt;
     &lt;label&gt;One&lt;/label&gt; 
    Product ID:&lt;input type="text" name="productid[]" value=""&gt; 
    Product Quantity: &lt;input type="text" name="quantity[]" value=""&gt; &lt;br&gt;
  
   &lt;label&gt;Two&lt;/label&gt; 
   Product ID:&lt;input type="text" name="productid[]" value=""&gt; 
   Product Quantity: &lt;input type="text" name="quantity[]" value=""&gt; &lt;br&gt;
  
   &lt;!-- 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--&gt;
   &lt;input type="submit" value="submit"&gt;
   &lt;/form&gt;
  </code></pre>
  
  <h2>Edited Part New</h2>
  
  <pre><code>function test(){
  
  $productid = ($_POST['productid']);
  $quantity = ($_POST['quantity']);
  
  
  for($i = 0; $i &lt; count($productid); $i++){
       $result=$this-&gt;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-&gt;db-&gt;query("SELECT product_stock FROM products 
                                    WHERE product_id=$productid");
  
  if ($query-&gt;num_rows() &gt; 0){
  
            foreach ($query-&gt;result() as $row){
  
        $product_stock=$row-&gt;product_stock;              
  
      }                 
   }                 
     if($product_stock&gt;$quantity) { return FALSE;   }         
  
      else { return  TRUE; }              
   }//function Ends           
  </code></pre>