如何在CodeIgniter中根据数组product_ID获取product_qty列数据
Fix: Fetch Product Quantities by ID Array in CodeIgniter
Let's break down the issues in your current code first, then fix them step by step:
Key Issues in Your Code
Controller (My_Controller.php)
- You're passing the entire product ID array to
getQty()every time in the loop, instead of the single ID at index$i. - The
returnstatement inside the for loop exits the function immediately after the first iteration—so you'll only ever get data for the first product ID. - You have a syntax error:
return product_quantity ;should bereturn $product_quantity;(missing$).
Model (move_To_Store.php)
- In the foreach loop, you're using
$product_ID(the whole array) in thewhereclause instead of$key(the individual ID). - You're directly
echoing data from the model, which is bad practice—models should return data to controllers, not output it. - You're running a separate database query for each ID, which is inefficient. We can use a single query with
where_into fetch all needed quantities at once. - If a product ID doesn't exist in the table,
row()will returnnull, and accessing->total_product_quantitywill throw a fatal error.
Corrected Code
Updated Controller (My_Controller.php)
public function addMTStore_post() { $productIds = $this->post('product_ID'); // Check if we have valid product IDs if (!empty($productIds) && is_array($productIds)) { // Get all quantities in one call $productQuantities = $this->move_To_Store->getQty($productIds); // Return the result (adjust the response format as needed for your API) return $this->response($productQuantities, REST_Controller::HTTP_OK); } else { // Handle invalid input return $this->response(['error' => 'Invalid product IDs provided'], REST_Controller::HTTP_BAD_REQUEST); } }
Updated Model (move_To_Store.php)
public function getQty($productIds = array()) { // Guard clause: return empty array if no IDs are provided if (empty($productIds)) { return array(); } // Fetch all matching products in a single query $this->db->select('product_ID, total_product_quantity'); $this->db->from('product'); $this->db->where_in('product_ID', $productIds); $query = $this->db->get(); // Convert result to a associative array (product ID as key, quantity as value) $quantities = array(); foreach ($query->result() as $row) { $quantities[$row->product_ID] = $row->total_product_quantity; } // Return the array of quantities return $quantities; }
What This Does
- Efficient Query: Uses
where_into fetch all required product quantities in one database call, instead of multiple queries. - Clean Separation of Concerns: The model returns data (not echoes it), and the controller handles the response.
- Error Handling: Includes checks for invalid input and empty ID arrays to avoid fatal errors.
- Useful Output: Returns an associative array where the keys are product IDs and values are their quantities—easy to use in your application.
For example, if you pass [1,2,3] as product IDs, the returned array will look like:
array( 1 => 50, 2 => 30, 3 => 40 )
内容的提问来源于stack exchange,提问作者B. Jayshri
相关产品推荐
相关产品推荐

