Good day!
I am writing a small class to expand work with MySQLi.
Actually, help is needed in the following: I can not write correctly the function where the operator is constructed.
I need it to be as flexible as possible, now I will explain.
Suppose having an array:
array( 'or' => array('login' => 'Admin', 'mail' => 'admin@glaz.com'), 'or' => array('like' => array('login' => 'A%'), 'mail' => 'sss@glaz.com'), 'login' => 'Stas Mikhailov' ); I received the following SQL query:
WHERE (login = Admin OR mail = admin@glaz.com) AND (login LIKE A% OR mail = sss@glaz.com) AND login = Stas Mikhailov Now I’ll explain a little bit what I want: There is an array, if it contains an array with the name or, then build it like this:
(key = value OR key2 = value2) If an array like appears, then build accordingly:
key LIKE value If there are no arrays, combine everything with AND.
My head is already buzzing with this, I just can not get the desired result. Here is what I tried to subtilize with OR and AND:
public function where($array = null) { if(!empty($array) && is_array($array)) { foreach($array as $key => $value) { if(is_array($value)) { $separator = strtolower($key); $separator = 'or' ? 'OR' : 'AND'; foreach($value as $key_array => $value_array) { $key_array = $this->filter($key_array); $value_array = $this->filter($value_array); $where_array[] = '`'.$key_array.'` = "'.$value_array.'"'; } $where[] = '('.implode(' '.$separator.' ', $where_array).')'; } else { $key = $this->filter($key); $value = $this->filter($value); $where[] = '`'.$key.'` = "'.$value.'"'; } } $this->where = ' WHERE '.implode(' AND ', $where); } else { die($this->class.': Параметры не определены (WHERE)'); } }