if(isset($get['last']) || isset($get['first'])){
$query = '';
$search_params = [];
if(isset($get['first']) && !isset($get['last'])){
//search only with first name
$query .= ' AND name LIKE :first ';
$search_params[':first'] = '%'.$get['first'].'%';
$args['first'] = $get['first'];
}
else if(!isset($get['first']) && isset($get['last'])){
//search only with last name
$query .= ' AND name LIKE :last ';
$search_params[':last'] = '%'.$get['last'].'%';
$args['last'] = $get['last'];
}
else{
//search with both first and last name
$query = ' AND (name LIKE :first OR name LIKE :last) ';
$search_params[':first'] = '%'.$get['first'].'%';
$search_params[':last'] = '%'.$get['last'].'%';
$args['first'] = $get['first'];
$args['last'] = $get['last'];
}
if($args['admin']){
//if the user is the admin of the account they can see all transactions
$args['transaction'] = admin_example($id, $query, $search_params);
}else{
//if the user is a member of the account and was involved in the transaction
$args['transaction'] = member_example($id,$user->email,$query,$search_params);
}
}else{
$args['transaction'] = false;
}
//get archived transaction the user was involved in matching search parameters
function admin_example($account_id,$query_segment,$search_params)
{
$params = array_merge([':id'=>$account_id],$search_params);
return R::getAll('SELECT name,tx_id,otp,property_type,property,ins_documents,active FROM transaction WHERE account_id=:id AND active="0" '.$query_segment.' ORDER BY name ASC',
$params
);
}
//get archived transaction the user was involved in for the current account and matching search parameters
function member_example($account_id,$email,$query_segment,$search_params)
{
//gets transactions the account member is able to view.
$params = array_merge([':account'=>$account_id,':email'=>$email],$search_params);
return R::getAll('SELECT name,tx_id,otp,property,property_type,ins_documents,active FROM
transaction WHERE account_id=:account AND (primary_email=:email OR secondary_email=:email) AND active="0" '.$query_segment.' ORDER BY name ASC',
$params
);
}
SELECT
name,tx_id,otp,property_type,property,ins_documents,active
FROM
transaction
WHERE
account_id='1' AND active='0' AND (name LIKE '%ABC%' OR name LIKE '%DEF%')
ORDER BY
name ASC
Предположим следующее:
[*]Я печатаю «ABC» при вводе имени и «DEF» при вводе фамилии.
[*]В таблице есть 1 строка, в которой выполняется условие active="0".
[*]Столбец имени указанной строки содержит фамилию и имя, разделенные запятой, например Flip, Tre.
Похоже, что независимо от того, что введено в поля ввода, запись всегда извлекается, хотя она не должна соответствовать ни одному из операторов LIKE. Я не знаю, почему , и мне интересно, могу ли я еще что-нибудь сделать? Я уже добавил индекс в столбец name, но не добавил кубика. Я также пытался использовать REGEXP, но мне не удалось получить ничего подобного.
[code]if(isset($get['last']) || isset($get['first'])){ $query = ''; $search_params = []; if(isset($get['first']) && !isset($get['last'])){ //search only with first name $query .= ' AND name LIKE :first '; $search_params[':first'] = '%'.$get['first'].'%'; $args['first'] = $get['first']; } else if(!isset($get['first']) && isset($get['last'])){ //search only with last name $query .= ' AND name LIKE :last '; $search_params[':last'] = '%'.$get['last'].'%'; $args['last'] = $get['last']; } else{ //search with both first and last name $query = ' AND (name LIKE :first OR name LIKE :last) '; $search_params[':first'] = '%'.$get['first'].'%'; $search_params[':last'] = '%'.$get['last'].'%'; $args['first'] = $get['first']; $args['last'] = $get['last']; } if($args['admin']){ //if the user is the admin of the account they can see all transactions $args['transaction'] = admin_example($id, $query, $search_params); }else{ //if the user is a member of the account and was involved in the transaction $args['transaction'] = member_example($id,$user->email,$query,$search_params); } }else{ $args['transaction'] = false; }
//get archived transaction the user was involved in matching search parameters function admin_example($account_id,$query_segment,$search_params) { $params = array_merge([':id'=>$account_id],$search_params); return R::getAll('SELECT name,tx_id,otp,property_type,property,ins_documents,active FROM transaction WHERE account_id=:id AND active="0" '.$query_segment.' ORDER BY name ASC', $params ); }
//get archived transaction the user was involved in for the current account and matching search parameters function member_example($account_id,$email,$query_segment,$search_params) { //gets transactions the account member is able to view. $params = array_merge([':account'=>$account_id,':email'=>$email],$search_params); return R::getAll('SELECT name,tx_id,otp,property,property_type,ins_documents,active FROM transaction WHERE account_id=:account AND (primary_email=:email OR secondary_email=:email) AND active="0" '.$query_segment.' ORDER BY name ASC', $params ); } [/code]
[b]Запрос[/b]
[code]SELECT name,tx_id,otp,property_type,property,ins_documents,active FROM transaction WHERE account_id='1' AND active='0' AND (name LIKE '%ABC%' OR name LIKE '%DEF%') ORDER BY name ASC [/code]
[b]Предположим следующее:[/b]
[*]Я печатаю «ABC» при вводе имени и «DEF» при вводе фамилии. [*]В таблице есть 1 строка, в которой выполняется условие active="0". [*]Столбец имени указанной строки содержит фамилию и имя, разделенные запятой, например Flip, Tre.
Похоже, что независимо от того, что введено в поля ввода, запись всегда извлекается, хотя она не должна соответствовать ни одному из операторов LIKE. Я не знаю, почему , и мне интересно, могу ли я еще что-нибудь сделать? Я уже добавил индекс в столбец name, но не добавил кубика. Я также пытался использовать REGEXP, но мне не удалось получить ничего подобного.