Ajax Livesearch搜索分为两列


Ajax Livesearch search in two colums

我从这里使用ajax livesearch: ajax livesearch

我想让用户在数据库中搜索城市,到目前为止它工作得很好。当我输入一个字母时,它会显示城市和邮政编码。现在,我尝试使用密码使相同的搜索框也可以工作,以便如果用户按邮政编码搜索,将显示城市。但我完全卡住了。也许有人有使用搜索框的经验,可以帮我一下。

config.php中我定义了搜索列:

const USER_TABLE = 'cities';
const SEARCH_COLUMN = 'Name';

从数据库中获取结果,我有这个:

$db = DB::getConnection();
    // get the number of total result
    $stmt = $db->prepare('SELECT COUNT(id) FROM ' . Config::USER_TABLE . ' WHERE ' . Config::SEARCH_COLUMN . ' LIKE :query');
    $search_query = $query.'%';
    $stmt->bindParam(':query', $search_query, PDO::PARAM_STR);
    $stmt->execute();
    $number_of_result = $stmt->fetch(PDO::FETCH_COLUMN);
    // initialize variables
    $HTML = '';
    $number_of_pages = 1;
    if ( (int) $number_of_result !== 0)
    {
        if ($items_per_page === 0)
        {
            // show all
            $stmt = $db->prepare('SELECT * FROM ' . Config::USER_TABLE . ' WHERE ' . Config::SEARCH_COLUMN . ' LIKE :query ORDER BY ' .Config::SEARCH_COLUMN);
            $search_query = $query.'%';
            $stmt->bindParam(':query', $search_query, PDO::PARAM_STR);
        }
        else
        {
            /*
             * pagination
             *
             * calculate total pages
             */
            if ($number_of_result < $items_per_page)
                $number_of_pages = 1;
            elseif ($number_of_result > $items_per_page)
                $number_of_pages = floor($number_of_result / $items_per_page) + 1;
            else
                $number_of_pages = $number_of_result / $items_per_page;
            /*
             * pagination
             *
             * calculate start
             */
            $start = ($current_page > 0 ) ? ($current_page - 1) * $items_per_page : 0;
            $stmt = $db->prepare('SELECT * FROM ' . Config::USER_TABLE . ' WHERE ' . Config::SEARCH_COLUMN . ' LIKE :query ORDER BY '.Config::SEARCH_COLUMN.' LIMIT ' . $start . ', ' . $items_per_page);
            $search_query = $query.'%';
            $stmt->bindParam(':query', $search_query, PDO::PARAM_STR);
        }
        // run the query and get the result
        $stmt->execute();
        $results = $stmt->fetchAll(PDO::FETCH_ASSOC);

这将显示结果在我的网站:

foreach($results as $result)
        {
            $HTML .= "<tr><td>{$result['Name']}</td><td>{$result['Postal_Code']}</td></tr>";
        }

所以如果我输入字母,它也会从'Postal_Code'列中给我正确的邮政编码。但我想也可以用另一种方式,我输入号码,它就会显示城市和邮政地址。在config.php中,我只能用一列定义

const SEARCH_COLUMN = 'Name';

也许可以使用第二个const:

const SEARCH_COLUMN2 = 'Postal_Code';

?但我怎么能使用它在数据库查询比?对我来说解释我的问题有点困难,所以我希望你能理解我:)由于

如果您在配置中添加另一个常量,则可以在查询中添加OR子句:

' WHERE ' . Config::SEARCH_COLUMN . ' LIKE :query OR '. Config::SEARCH_COLUMN2 . ' LIKE :query'