You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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 return statement 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 be return $product_quantity; (missing $).

Model (move_To_Store.php)

  • In the foreach loop, you're using $product_ID (the whole array) in the where clause 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_in to fetch all needed quantities at once.
  • If a product ID doesn't exist in the table, row() will return null, and accessing ->total_product_quantity will 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

  1. Efficient Query: Uses where_in to fetch all required product quantities in one database call, instead of multiple queries.
  2. Clean Separation of Concerns: The model returns data (not echoes it), and the controller handles the response.
  3. Error Handling: Includes checks for invalid input and empty ID arrays to avoid fatal errors.
  4. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:17:52