Bước 9: Xây dựng buildConditions WHERE (ok)
PreviousBước 8: Tạo phương thức setEncoding cho class Mysql (ok)NextChú ý xây dựng một câu lệnh sql đầy đủ cần các 8 yếu tố sau :( (ok)
Last updated
Last updated
<?php
$condition = array (
'email' => 'admin@gmail.com',
'password' => '7c4a8d09ca3762af61e59520943dc26494f8941b',
);
function buildConditions($condition) {
// $condition is
// array (
// 'email' => 'admin@gmail.com',
// 'password' => '7c4a8d09ca3762af61e59520943dc26494f8941b',
// )
$keywords = array(
'>=',
'<=',
'>',
'<',
'IN',
'NOT',
'IS',
'LIKE',
'!=',
'<>'
);
if (empty($condition)) return;
$sql = '';
$index = 0;
$conditionArray = array();
foreach ($condition as $field => $sub) {
$math = '';
$subVal = explode(' ', $sub);
// $subVal is
// array (
// 0 => 'admin@gmail.com',
// )
if (!in_array($subVal[0], $keywords)) {
$math = '=';
}
$conditionArray[] = " " . $field . " " . $math . " '" . $sub . "'";
// array (
// 0 => "email = 'admin@gmail.com'",
// 1 => "password = '7c4a8d09ca3762af61e59520943dc26494f8941b'",
// )
}
return " WHERE " . implode(' AND ', $conditionArray);
// "WHERE email = 'admin@gmail.com' AND password = '7c4a8d09ca3762af61e59520943dc26494f8941b'"
};
echo buildConditions($condition);
// "WHERE email = 'admin@gmail.com' AND password = '7c4a8d09ca3762af61e59520943dc26494f8941b'"
?>