Exclude certain values from options_list (Table Class)

edited September 2011 in Feature Requests
Hi there,
i just hit this wall with the Table Class method 'options_list'. Thinking possibly i'm not the only one...

It accepts a $where array or string. If you wanted to exclude fields from being returned, you're forced to make the 'where' condition into a string (am I wrong?)

So if you wanted to exclude the category 'Music' from being returned you'd have to:

$where = 'published = "yes" AND name NOT LIKE "Music"';
...->options_list('id', 'name', $where);
because obviously if you use a $where array u can only specify positive comparisons ie KEY equals VALUE.

My suggestion is to add a parameter to the options_list method so that you can pass

options_list('key', 'value', $where, [$order], [$exclude])
where $exclude is an associative array mapping fields (column) and values. In the case above we'd have

$where = array('published' => 'yes');
$exclude = array('name' => 'Music', 'name'=>'Photos', 'id' => 0);
..->options_list('id', 'name', $where, NULL, $exclude);
Not that I have anything against $where strings, it's just that it would help keeping the code nice and tidy... :)
Or is that overkill...?

Comments

  • edited 11:09PM
    Using a string instead of an array is perfectly acceptable. CodeIgniter allows you to use negative comparisons in the array syntax as in the following:
    array('published' => 'yes', 'name !=' => 'Music' ); ..->options_list('id', 'name', $where, NULL);
    Also, in the example above, there are two key names of "name" so the first one is overwritten in the $exclude array.
  • edited 11:09PM
    oh, i didn't know you could do that! i only tried putting the != operator before the value... thanks for the tip! (am i wrong or this is not documented in the active record user guide entry?)
    PS about the duplicated array key in my example, i know, i confused 'name' as a key with 'name of field' as a variable :D
  • edited 11:09PM
    They do mention it here but it's not something that's completely obvious (search for != on the page):
    http://codeigniter.com/user_guide/database/active_record.html
  • edited 11:09PM
    duh! wish i was that eagle-eyed! :D
Sign In or Register to comment.