Ajax Livesearch search in two colums

I use the ajax livesearch from here: Ajax Livesearch

I want to let users search for cities in the database and it works perfectly so far. When I type a letter it will show me the cities and the zipcode. Now I try to make the same searchbox also work using a cypher so that the cities will be shown if the user search by zipcodes. But I am stuck completely. Maybe some one has some experience with searchboxes and can help me out.

In the config.php i defined the search column:

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

To get the results from the database I have this:

$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);

And this will show the results on my site:

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

So if I type the letters it will also give me the right zipcode from the column 'Postal_Code'. But I guess it is also possible to do it the other way, I type the number and it shows me the city and the zip? In the config.php i can only one column define

const SEARCH_COLUMN = 'Name';

Maybe it's possible to use a second const like:

const SEARCH_COLUMN2 = 'Postal_Code';

? But how can I use it in the db query than? It's a bit difficult for me to explain my problem so I hope you will understand me any way :) Thanks

Answers


If you add another constant to your config, you could then just add an OR clause to your query:

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

Need Your Help

Python: Simple Dictionary referencing Problem

python dictionary

I have a simple problem that i cannot solve. I have a dictionary:

Something like pubsubhubbub that does not depend on google app engine

python rss feed pubsubhubbub atom-feed

I am looking for something like PubSubHubbub that does not depend on google app engine to run.