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)'); } } 
  • Look at the PDO class php.net/manual/ru/class.pdo.php , and if you need to customize, then create a child class. Do not reinvent the regular curve bike. - Ordman
  • @DmitryOnGamer The first curve is a PDO? - Sergey
  • @Dmitry On gamer why do I need PDO? I wrote that the task is to expand the class mysqli. Is there a specific solution to my problem from you? I do not understand where to dig further ... - Dmitry Goncharov

1 answer 1

Problem solved. Flexible WHERE Builder is written.

 public function where($array = null, $w = 'AND') { if(!empty($array) && is_array($array)) { foreach($array as $key => $value) { if(is_array($value)) { $separator = strtoupper($key); switch($separator) { case 'OR': $where[] = '('.$this->where($value, 'OR').')'; break; case '>': case '<': case '<>': case '>=': case '<=': case 'LIKE': foreach($value as $key_array => $value_array) { if(isset($key_array[$value_array])) { $key_array = $this->filter($key_array); $value_array = $this->filter($value_array); $where[] = '`'.$key_array.'` '.$separator.' "'.$value_array.'"'; } else { $error = true; } } break; case 'BETWEEN': case 'IN': foreach($value as $key_array => $value_array) { if(is_array($value_array)) { $key_array = $this->filter($key_array); $where[] = '`'.$key_array.'` '.$separator.' ("'.implode('", "', $value_array).'")'; } else { $error = true; } } break; default: $where[] = '('.$this->where($value, 'AND').')'; break; } if($error == true) { $this->error_msg('Неверный синтаксис (Оператор "'.$separator.'")'); } } else { $key = $this->filter($key); $value = $this->filter($value); $where[] = '`'.$key.'` = "'.$value.'"'; } } $where = implode(' '.$w.' ', $where); $this->where = $where; return $where; } else { $this->error_msg('Параметры не определены (WHERE)'); } }