Welcome Guest, Not a member yet? Register   Sign In
Active record and dropdown values
#1

[eluser]jprateragg[/eluser]
I'm trying to use active record since most people here seem to prefer it. How do you handle values in dropdown/select fields? Do you use ->where or ->like? I'm currently doing this:

Code:
$query = $this->db->select("id, is_consolidation, be_no, CASE entity_type_id WHEN 1 THEN
CONCAT(last_name, ', ', first_name) WHEN 2 THEN last_name END AS entity_name", FALSE)
->like($this->input->post())->order_by('last_name, first_name')->get('member');

I have a few dropdown/select fields with a default "blank" value. I can't use ->like I am here because it will match values that are single digits "2", "23". I can't use ->where because then I get an error because of an empty value.

How do you handle this? When I construct traditional queries, I usually check to see if the variable is empty and if not, then append that portion of the SQL statement. I'd like to see how this can be handled with active record. Thanks!
#2

[eluser]CroNiX[/eluser]
Just use some logic to build the query.
Code:
$this->db->select('something');

//add an additional select and where if a condition is met
if (some_condition)
{
  $this->db->select('more_stuff');
  $this->db->where('some_condition', $some_var);
}

$this->db->where('something_else <>', $some_other_var);
$this->db->get('some_table');
#3

[eluser]jprateragg[/eluser]
That's what I figured I would have to do. Just wanted to check. Thanks!




Theme © iAndrew 2016 - Forum software by © MyBB