php - foreach loop - correct (most efficient way to handle a first case
scenario )
I have a function that builds a MySQL query from the supplied arguments.
My current code is:
($args can be an empty array or up to a set of 5 field_names=>array_of_ids
...)
if( !(empty( $args )) )
{
$flag = 0;
$sql_append = '';
foreach( $args as $field_name => $id_array )
{
if( $flag == 0 )
{
$where_connector = " WHERE ";
$flag = 1;
}
else
{
$where_connector = " AND ";
}
${ $field_name . '_string'} = join(',',${ $field_name . '_ids'});
$sql_append .= $where_connector . 'link_id IN ($ids)";
}
}
I'm self-taught and so constantly worry about best practices. I seem to
remember some sort of function that handles arguments, perhaps in a way
that can be applied here more efficiently. Any ideas?
No comments:
Post a Comment