elle yang
elle yang

Reputation: 71

Codeigniter: Model query doesn't retrieve any data

On my database I have fields that has value of

1 Blue White

2 White Blue

3 Blue

4 White

for every ids.

I have a controller where I declare $color="White Blue" and passed it on the model. On my model I use 'explode' to separate the two words and use a for-each to extract.

Here's my model:

  public function test($color){

    $arry = explode(" ", $color); 

    foreach($arry as $colr){
    $this->db->where("tags like '%$colr%' or 
            tags like '%$colr'or 
            tags like '$colr%' ");
            }


    $q=$this->db->get('test');
    return $q;

    }

When I try to run it on localhost, it gives me Call to a member function result() error. I know that this kind of error means it doesn't retrieve any data from my database. When I tried to put an error on my query, I found out that instead if using 'OR' after the first for-each it uses 'AND'. Do you have any idea on how I can make this work?

Upvotes: 0

Views: 159

Answers (2)

jtheman
jtheman

Reputation: 7491

public function test($color){

    $arry = explode(" ", $color); 
    foreach($arry as $colr) $this->db->or_where("tags like '%$colr%' "); 
    $q=$this->db->get('test');
    return $q;

}

Upvotes: 1

JvdBerg
JvdBerg

Reputation: 21856

tags like '%$colr'or

is missing a space?

tags like '%$colr' or

Futhermore, putting the $colr between '%' should be sufficient. No need for the other lines.

So, this should do the trick:

public function test($color){

    $arry = explode(" ", $color); 

    foreach($arry as $colr){
      $this->db->where("tags like '%$colr%' "); 
    }

    $q=$this->db->get('test');
    return $q;

}

Upvotes: 1

Related Questions