CodeIgniter Forums
or_where problem with active record - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: or_where problem with active record (/thread-52677.html)



or_where problem with active record - El Forum - 06-20-2012

[eluser]bobbob[/eluser]
This is probably simple but I don't see it.
Code:
$pages = explode(",",$pages_string);
$this->db->select('name, text,page_number');
$this->db->where('article_id',$id);
$this->db->where('page_number',$pages[0]);
$this->db->or_where('page_number',$pages[1]);
$this->db->order_by('page_number');
$q = $this->db->get('articles');

This will write ors for all 3 conditions but as you guessed i want ors for the page numbers and AND for the article_id.
Can I do this with active record or do I need to just use $this->db->query()?


or_where problem with active record - El Forum - 06-20-2012

[eluser]CroNiX[/eluser]
You'd have to manually write that in a single where.

Something like:
Code:
$this->db->where('article_id = ' . $id . ' AND (page_number = ' . $pages[0] . ' OR page_number = ' . $pages[1] . ')');

The upcoming CI3 will have abilities to do compound AND/OR/etc.


or_where problem with active record - El Forum - 06-20-2012

[eluser]bobbob[/eluser]
Thanks. Was just checking. Looking forward to CI3!!


or_where problem with active record - El Forum - 06-20-2012

[eluser]CroNiX[/eluser]
Actually, you could use a WHERE IN for your query, since it has to be one page number or the other.

Code:
$this->db->where('article_id',$id);
$this->db->where_in('page_number', array($pages[0], $pages[1]));



or_where problem with active record - El Forum - 06-20-2012

[eluser]bobbob[/eluser]
Perfect and as $pages is an array:
Code:
$this->db->where_in('page_number',$pages);



or_where problem with active record - El Forum - 06-20-2012

[eluser]CroNiX[/eluser]
Cool, didn't know if you had more/unrelated data in your $pages array.


or_where problem with active record - El Forum - 06-21-2012

[eluser]CodeIgniteMe[/eluser]
didn't know CI3 will be coming very soon. looking forward to it.