Missing operators in table search

Issue #278 resolved
Michel Corne created an issue

The operators IS NULL, IS NOT NULL, IN () and NOT IN () would be nice to have to search a table. Please, feel free to leverage the code below to add them. Add line #2349 before the LIKE operator:

echo "<option value='IN'>IN (...)</option>";
echo "<option value='NOT IN'>NOT IN (...)</option>";
echo "<option value='IS NULL'>IS NULL</option>";
echo "<option value='IS NOT NULL'>IS NOT NULL</option>";

Add line #2174:

$value = $_POST[$field_index];

if ($operator == 'IS NULL' or $operator == 'IS NOT NULL') {
    $arr[$j++] = $db->quote_id($field) . " " . $operator;

} elseif ($operator == 'IN' or $operator == 'NOT IN') {
    $value = trim($value, '() ');
    $values = preg_split('/[ ,]+/', $value, -1, PREG_SPLIT_NO_EMPTY);
    $values = array_map([$db, 'quote'], $values);
    $value = implode(', ', $values);
    $arr[$j++] = $db->quote_id($field) . " " . $operator . '(' . $value . ')';

} elseif($value!="" || $operator=="!= ''" || $operator=="= ''") { ...}

Comments (2)

  1. Christopher Kramer

    Okay, I added the requested parameters with commit #9723e4f. My code is based on yours but I did some changes.

    Especially, I don't consider it useful to allow values separated by space for IN or NOT IN, as values might contain spaces. It would not be possible to do something like this: WHERE title IN ("General Manager", "Sales Manager") because this would end up like: WHERE title IN ("General", "Manager", "Sales", "Manager") I named the parameters IN(..., ...) and NOT IN(..., ...) to make clear values should be separated by comma and require values separated by comma.

    Also, I adjusted the highlighting of search values so the IN operator will highlight the matching values.

    You can check out the latest development version here.

  2. Log in to comment