• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
or_where problem with active record

#1
[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()?

#2
[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.

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

#4
[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]));

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

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

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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.