0

I don't know what they are exactly called but I call the query builders $this->db->select().

Is it possible to do this:

$this->db->select('*')
         ->from('table_name')
         if(1)
         {
          ->where('column_name',1)
         }
         ->order_by('column_name','ASC');

If not are there any alternatives I can do?

Note: I just want to make my code short and not create another function where the only difference is that there is a where() in the function.

Danish Ali
  • 2,354
  • 3
  • 15
  • 26
blur
  • 79
  • 1
  • 9
  • It's not at all clear what you need to do, nor what you've tried and are not able to sort out. There's a lot of code and explanation missing here in order to allow some understanding of what you intend – Javier Larroulet Dec 18 '18 at 01:35

1 Answers1

3

It is possible to do this:

$this->db->select('*')->from('table_name');
if(1)
{
    $this->db->where('column_name',1);
}
$this->db->order_by('column_name','ASC');
Sherif Salah
  • 2,085
  • 2
  • 9
  • 21
  • Thank you! I thought I can't separate them. – blur Dec 18 '18 at 01:45
  • while your answer is correct, maybe you would want to explain a bit more about the difference of what is happening, if you use the semicolon `;` or not. That would make the answer useful for later visitors as well! – Vickel Dec 18 '18 at 01:46
  • @blur, You can put Query Builder calls in any order with the exception of `get()` or `get_where()` which need to be last because they actually execute the query statement. – DFriend Dec 18 '18 at 04:05