Alex S England
Alex S England

Reputation: 3

How can I specify multiple columns to be searched with PHP?

I recently built a website for work because our techs need an easy way to find out what keys go to which properties we provide service to. I've gotten the site working with live search (Ajaxlivesearch.com) and it's linked to my MySQL database. Everything works great except it only searches one column currently, the Address column. I'd like to be able to search two columns, Address and Property_Name at the same time.

Here's the current code, or at least the part that deals with searching the db.

<?php

namespace AjaxLiveSearch\core;

if (count(get_included_files()) === 1) {
    exit('Direct access not permitted.');
}

/**
 * Class Config
 */
class Config
{
    /**
     * @var array
     */
    private static $configs = array(
        // ***** Database ***** //
        'dataSources'           => array(
            'ls_query' => array(
                'host'               => '',
                'database'           => '',
                'username'           => '',
                'pass'               => '',
                'table'              => '',
                // specify the name of search columns
                'searchColumns'      => array('Address'),
                // specify order by column. This is optional
                'orderBy'            => '',
                // specify order direction e.g. ASC or DESC. This is optional
                'orderDirection'     => '',
                // filter the result by entering table column names
                // to get all the columns, remove filterResult or make it an empty array
                'filterResult'       => array(),
                // specify search query comparison operator. possible values for comparison operators are: 'LIKE' and '='. this is required.
                'comparisonOperator' => 'LIKE',
                // searchPattern is used to specify how the query is searched. possible values are: 'q', '*q', 'q*', '*q*'. this is required.
                'searchPattern'      => 'q*',
                // specify search query case sensitivity
                'caseSensitive'      => false,
                // to limit the maximum number of result uncomment this:
                'maxResult' => 10,
                // to display column header, change 'active' value to true
                'displayHeader' => array(
                    'active' => true,
                    'mapper' => array(
                        'Property_Name' => 'Property Name',
                        'Address' => 'Address',
                        'Key' => 'Key',
                        'Property_Manager' => 'Property Manager',
                        'Door_Code' => 'Door Code'
                    )
                ),
                // add custom class to <td> and <th>
                // To hide a column use class 'ls_hide'
                'columnClass' => array(
                    'Count' => 'ls_hide',
                    'Reserve' => 'ls_hide'
                ),
                'type'               => 'mysql',
            ),

I've taken the connection info out for obvious reasons.

I tried 'searchColumns' => array('Address' AND 'Property_Name'), thinking that would search both columns but that didn't work at all.

Upvotes: 0

Views: 150

Answers (2)

Alex S England
Alex S England

Reputation: 3

And of course as soon as I post the question I figure it out, maybe it will help someone else though. To look at multiple columns in one array you need to separate them with a comma (,) the working code is:

<?php

namespace AjaxLiveSearch\core;

if (count(get_included_files()) === 1) {
    exit('Direct access not permitted.');
}

/**
 * Class Config
 */
class Config
{
    /**
     * @var array
     */
    private static $configs = array(
        // ***** Database ***** //
        'dataSources'           => array(
            'ls_query' => array(
                'host'               => '',
                'database'           => '',
                'username'           => '',
                'pass'               => '',
                'table'              => '',
                // specify the name of search columns
                'searchColumns'      => array('Address', 'Property_Name'),
                // specify order by column. This is optional
                'orderBy'            => '',
                // specify order direction e.g. ASC or DESC. This is optional
                'orderDirection'     => '',
                // filter the result by entering table column names
                // to get all the columns, remove filterResult or make it an empty array
                'filterResult'       => array(),
                // specify search query comparison operator. possible values for comparison operators are: 'LIKE' and '='. this is required.
                'comparisonOperator' => 'LIKE',
                // searchPattern is used to specify how the query is searched. possible values are: 'q', '*q', 'q*', '*q*'. this is required.
                'searchPattern'      => 'q*',
                // specify search query case sensitivity
                'caseSensitive'      => false,
                // to limit the maximum number of result uncomment this:
                'maxResult' => 10,
                // to display column header, change 'active' value to true
                'displayHeader' => array(
                    'active' => true,
                    'mapper' => array(
                        'Property_Name' => 'Property Name',
                        'Address' => 'Address',
                        'Key' => 'Key',
                        'Property_Manager' => 'Property Manager',
                        'Door_Code' => 'Door Code'
                    )
                ),
                // add custom class to <td> and <th>
                // To hide a column use class 'ls_hide'
                'columnClass' => array(
                    'Count' => 'ls_hide',
                    'Reserve' => 'ls_hide'
                ),
                'type'               => 'mysql',
            ),
 Run code snippetCopy snippet to answer

Upvotes: 0

Don&#39;t Panic
Don&#39;t Panic

Reputation: 41810

I'm not familiar with Ajaxlivesearch, but it looks like searchColumns takes an array, so:

 'searchColumns' => array('Address', 'Property_Name'),

will probably work.

(array('Address' AND 'Property_Name') is a syntax error.)

Upvotes: 1

Related Questions