sql - More elegant way in PHP to generate a query from array elements -
when need loop on while generating query each element, use like
$querystr = "insert tablename (x,y) values "; ($i = 0 ; $i < $len ; $i++) { $querystr .= "( ".$thing[$i]['x'].", ".$thing[$i]['b']."), "; } //extra code remove last comma string
would there alternative? don't mind performance (knowing length of array not big), looks nicer.
a slight improvement rid of last part (removing latest comma). can first create array of values, use implode function like:
$querystr = "insert tablename (x,y) values "; ($i = 0 ; $i < $len ; $i++) { $values[] = "( ".$thing[$i]['x'].", ".$thing[$i]['b'].")"; } $querystr .= implode(',', $values);
Comments
Post a Comment