开发者

How to put conditions on a search engine, built on CodeIgniter

开发者 https://www.devze.com 2023-04-05 09:23 出处:网络
A CodeIgniter application has a search engine, and I want to add conditions to the results. Some of the code that are in the Models:

A CodeIgniter application has a search engine, and I want to add conditions to the results. Some of the code that are in the Models:

$this->db->select(users.country_symbol);

and

function getUsers($conditions=array())
{
    //Check For Conditions
    if (is_array($conditions) and count($conditions)>0)     
        $this->db->where($conditions); 
}

O开发者_如何转开发n The controller I have:

$users = $this->search_model->getUsers(NULL)

How can I change NULL into a condition that request for users with the same country_symbol as the $this->loggedInUser->country_symbol?


You could modify your function like this:

function getUsers($conditions = array())
{
    if (is_array($conditions) && count($conditions) > 0) {
        foreach ($conditions as $condition) {
            $this->db->where($condition['where'], $condition['value']);
        }
    }

    // return results...
}

And then you could do this:

$conditions = array(
    array('where' => 'country_symbol =', 'value' => $this->loggedInUser->country_symbol),
    array('where' => 'zip_code', 'value' => '12345'),
    // and so on...
)

$users = $this->search_model->getUsers($conditions);

I haven't tested this code but that's the basic idea. You'll need to loop through the conditions array and apply each condition with $this->db->where() or a similar function.

0

精彩评论

暂无评论...
验证码 换一张
取 消