db活动记录

时间:2012-10-12 00:22:11

标签: codeigniter

我正在尝试使用活动记录从我的数据库中获取一些数据,而且我遇到了以下问题。我想学习如何使用活动记录,因为在这段代码之后我有大量的连接和其他东西。否则我会写一个普通的查询。

        $this->db->where("sa.country", $country);


         // I NEED TO OPEN BRACKETS HERE SO THAT I CAN GET THE LANGUAGES IF THERE IS DATA IN ONE OF THE ARRAYS


        if (isset($lid[0]))$this->db->where("sa.lang", $lid[0]);
        if (isset($lid[1]))$this->db->or_where("sa.lang", $lid[1]);
        if (isset($lid[2]))$this->db->or_where("sa.lang", $lid[2]);
        if (isset($lid[3]))$this->db->or_where("sa.lang", $lid[3]);
        if (isset($lid[4]))$this->db->or_where("sa.lang", $lid[4]);


        //AND CLOSE THEM HERE

我的目标是从db获取特定国家/地区,并使用阵列中的相应语言。

3 个答案:

答案 0 :(得分:0)

这有帮助吗?

$this->db->where("sa.country", $country);

// loop through *set* languages
foreach($lid as $item)
{
    $this->db->where('sa.lang', $item);
}

// will return a query object
return $this->db->get('my_table');

答案 1 :(得分:0)

以下是我对解决方案感兴趣的人解决问题的方法。

$this->db->where("(`sa`.`lang` = '$lid[0]' OR `sa`.`lang` = '$lid[1]' OR `sa`.`lang` = '$lid[2]' OR `sa`.`lang` = '$lid[3]' OR `sa`.`lang` = '$lid[4]')");

答案 2 :(得分:0)

我认为你应该使用WHERE...IN...!请参阅帮助about 'where in'

使用ActiveRecord:

$this->db->where_in("sa.lang",$lid);
相关问题