通过数组codeigniter从数据库中获取结果

Posted

技术标签:

【中文标题】通过数组codeigniter从数据库中获取结果【英文标题】:fetch result from database by array codeigniter 【发布时间】:2021-07-24 08:06:08 【问题描述】:

我有一张食物表,我将类别 ID 存储为逗号分隔,如下图所示 food table

我在模型中创建了一个方法,它以数组(类别 ID)作为参数,并从参数中获取 ID 与数组匹配的食物。

我做了以下查询

if(count($categoryIds) > 0 && count($allergyIds) == 0 )
    $tempArr = array();
    foreach($categoryIds as $eachCategoryId)
        $sql = "Select food.food_id,food_name,food_image,food_thumbnail,description,food_variations.price as price,is_active
        from food
        join food_variations on food_variations.food_id = food.food_id
        where FIND_IN_SET($eachCategoryId,category_id)
        and food.restaurant_id = $restaurantId
        and food.is_active = 1
        and food.is_deleted = 0
        and food.food_status = 3
        and food_variations.food_variation_id = food.default_food_variation_id";
        $result = $this->db->query($sql)->result_array();
        array_push($tempArr, $result);
    
    echo "<pre>";print_r($tempArr);

下面是上面查询的结果

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [food_id] => 10
                    [food_name] => Rama Mckee
                    [food_image] => 
                    [food_thumbnail] => 
                    [description] => asdfs
                    [price] => 34
                    [is_active] => 1
                )

            [1] => Array
                (
                    [food_id] => 6
                    [food_name] => Rishi
                    [food_image] => 
                    [food_thumbnail] => 
                    [description] => test
                    [price] => 120
                    [is_active] => 1
                )

            [2] => Array
                (
                    [food_id] => 5
                    [food_name] => test
                    [food_image] => http://localhost/gastroapp/assets/uploads/food_images/a5918726b920e7cbfc7f90e1afc48091.jpg
                    [food_thumbnail] => http://localhost/gastroapp/assets/uploads/food_images/thumb/a5918726b920e7cbfc7f90e1afc48091.jpg
                    [description] => test
                    [price] => 120
                    [is_active] => 1
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [food_id] => 10
                    [food_name] => Rama Mckee
                    [food_image] => 
                    [food_thumbnail] => 
                    [description] => asdfs
                    [price] => 34
                    [is_active] => 1
                )

            [1] => Array
                (
                    [food_id] => 7
                    [food_name] => ezhva
                    [food_image] => 
                    [food_thumbnail] => 
                    [description] => ddsfsd
                    [price] => 20
                    [is_active] => 1
                )

            [2] => Array
                (
                    [food_id] => 8
                    [food_name] => test
                    [food_image] => 
                    [food_thumbnail] => 
                    [description] => test
                    [price] => 22
                    [is_active] => 1
                )

            [3] => Array
                (
                    [food_id] => 6
                    [food_name] => Rishi
                    [food_image] => 
                    [food_thumbnail] => 
                    [description] => test
                    [price] => 120
                    [is_active] => 1
                )

        )

)

我得到了重复的结果,我认为这也可能导致性能问题。

以下是当我每种食物只有一个类别时的查询,这给了我想要的结果。

return $this->db->select('food.food_id,food_name,food_image,food_thumbnail,description,food_variations.price as price,is_active')
            ->from('food')
            ->join('food_variations', 'food_variations.food_id = food.food_id')
            ->where_in('category_id',$categoryIds)
            ->where('food.restaurant_id', $restaurantId)
            ->where('food.is_active', '1')
            ->where('food.is_deleted', '0')
            ->where('food.food_status','3')
            ->where('food_variations.food_variation_id IN( select food_variation_id from food_variations where food_variation_id = food.default_food_variation_id )')
            ->get()
            ->result_array();

请帮忙。

【问题讨论】:

对于mysql,建议使用字符串列来保存静态数据,而不是只添加ID。您必须存储具有相关值的对象,而不仅仅是整数。如果你想要它的关系数据,最好使用中间表。 我已经更新了我的问题。请立即查看。 【参考方案1】:

首先你需要将你的列表插入到一个数组中

$list = ids column
$list = array_map("intval", explode(",", str_replace(',' , '', $list)));

现在您的列表保存在数组 $list 中

现在你可以调用它了

foreach($list as $value) 
$sql = "SELECT * FROM menu WHERE user_id = '$user_id' AND id = '$value' AND deleted = '0' AND active = '1';";
$result = mysqli_query($dBconnection, $sql);
$check = mysqli_num_rows($result);
if ($check>0) 

while($row = mysqli_fetch_assoc($result)) 
cho $row['id'];


 else 
echo 'empty list';


【讨论】:

【参考方案2】:

对于 mysql,建议使用字符串列来保存静态数据,而不是仅添加 ID。您必须存储具有相关值的对象,而不仅仅是整数。如果你想要它是关系数据,最好使用中间表。

就像你需要一个额外的表 category_products 表来存储 category_idproduct_id 因为你有 many-to-Many 关系(一个产品有很多类别,一个类别有很多产品)

【讨论】:

以上是关于通过数组codeigniter从数据库中获取结果的主要内容,如果未能解决你的问题,请参考以下文章

无法在 Codeigniter 中从模型获取查询结果到控制器

Codeigniter 未显示 MySQL 查询获取的数组的正确结果。

获取 CodeIgniter ActiveRecord 结果中每一行的关联数组

如何使用codeigniter从mysql中获取多个多维数组中的数据?

CodeIgniter - 数据库结果数组,但只有一行

在连接中获取多行(codeigniter)