I have a function similar to the following:
public function foo ($cities = array('anaheim', 'baker', 'colfax') ) { $db = global instance of Zend_Db_Adapter_Pdo_Mysql... $query = 'SELECT name FROM user WHERE city IN ('.implode(',',$cities).')'; $result = $db->fetchAll( $query ); }
This works fine until someone passes $ cities as an empty array.
To prevent this error, I logically violated the request as follows:
$query = 'SELECT name FROM user'; if (!empty($cities)) { $query .= ' WHERE city IN ('.implode(',',$cities).')'; }
but it is not very elegant. I feel that there should be a better way to filter by list, but I'm not sure how to do it. Any tips?
source share