How to populate SQL Select statement with dynamically (according to input parameters) generated Where conditions…? Once i got an a tiny idea and after that it became like stile of generating such kind of statements, it can be useful or not but i like it and want to share it …
So…
For example you have to populate Select statement with “where” conditions coming from an filter array, for an good result i will show an filter combined from field names as keys and filters as values :

$q = “SELECT `t`.`f` FROM `t` WHERE 1 “;
//populating sql filter
if (isset($filter) and is_array($filter)) {

foreach ($filter as $key=>$value) {
$q .= sprintf(” AND `%s` = ‘%s’”,$key, $value);
}

}

In this example i expect that filter should be the “equal” for all attributes, but this topic not for that.
Nicest thing is “WHERE 1″ in the first line: it allows you to not worry for content of coming filter, is there exists any useful (valid) parameter or not, and of course no cutting strings function in the end of populating (i like this point more).
in other words you say- “if i have no filters, than i have one … which is always true”, and i keep that one above others …
thanks