Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7972827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:57:35+00:00 2026-06-04T07:57:35+00:00

I have a form where users can enter product id and its quantity. What

  • 0

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.

Could you please show me how my controller should look like if I want to achieve this using Codeigniter’s default validation library?

This is my db table name: product

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

This is my View File — My Form (check this link to see the 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>

Edited Part New

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T07:57:35+00:00Added an answer on June 4, 2026 at 7:57 am

    You can use a callback function. read 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 will check in the database the available quantity. It is recommended to check it using a model.

    EDIT:

    I would advise against using the form validation like mentioned above for this type of validation because you have two arrays which need to match based on their index. When using the callback function you do not know which product’s quantity you are working with.

    My recommendation:

    $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]);
       }
    }
    

    Using this code, the is_available_in_stock function you wrote should work.

    Second EDIT:

    $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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form where users can enter the names and emails addresses of
I have a form where a user can enter the number of students that
I have a very small form where a user can enter their zip code
I have a form where users can enter an isbn and it will try
I have a very simple form where users can enter their email and click
I want to build a form where users can enter some data in a
I have a form where users can enter data that ultimately would be used
I have a form where users can enter tags like tag1,tag2,tag3. I explode that
I have a web form where the users can enter the parts of the
I have a form where users can enter the amount they spend on their

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.