iMarkDesigns
iMarkDesigns

Reputation: 1262

PHP in_array match search value to array list

I have a search function on my page, where user can able to search US states to find property. However, in my database record, abbreviation only was set on each state name. So I download a copy of US states in array list format.

Code Fig. 1

$searchStates = array(
    'Alabama'          =>'AL',
    'Alaska'           =>'AK',
    'Arizona'          =>'AZ',
    'Arkansas'         =>'AR',
    'California'       =>'CA',
    'Colorado'         =>'CO',
    'Connecticut'      =>'CT',
    'Delaware'         =>'DE',
    'Florida'          =>'FL',
    'Georgia'          =>'GA',
    'Hawaii'           =>'HI',
    'Idaho'            =>'ID',
    'Illinois'         =>'IL',
    'Indiana'          =>'IN',
    'Iowa'             =>'IA',
    'Kansas'           =>'KS',
    'Kentucky'         =>'KY',
    'Louisiana'        =>'LA',
    'Maine'            =>'ME',
    'Maryland'         =>'MD',
    'Massachusetts'    =>'MA',
    'Michigan'         =>'MI',
    'Minnesota'        =>'MN',
    'Mississippi'      =>'MS',
    'Missouri'         =>'MO',
    'Montana'          =>'MT',
    'Nebraska'         =>'NE',
    'Nevada'           =>'NV',
    'New Hampshire'    =>'NH',
    'New Jersey'       =>'NJ',
    'New Mexico'       =>'NM',
    'New York'         =>'NY',
    'North Carolina'   =>'NC',
    'North Dakota'     =>'ND',
    'Ohio'             =>'OH',
    'Oklahoma'         =>'OK',
    'Oregon'           =>'OR',
    'Pennsylvania'     =>'PA',
    'Rhode Island'     =>'RI',
    'South Carolina'   =>'SC',
    'South Dakota'     =>'SD',
    'Tennessee'        =>'TN',
    'Texas'            =>'TX',
    'Utah'             =>'UT',
    'Vermont'          =>'VT',
    'Virginia'         =>'VA',
    'Washington'       =>'WA',
    'West Virginia'    =>'WV',
    'Wisconsin'        =>'WI',
    'Wyoming'          =>'WY'
);

Case:

My clients use to search state name, ex: Tennessee but in my result, it will display No Record due of abbreviation only was saved in my database. I would like to use this array list of $searchStates to match my abbreviation list of states. I tried to use in_array function, but it seems not working correctly. See my actual code.

Code Fig. 2

if (in_array( $search, $searchStates )) {

    $property->where(function($query) use ($search) {
        $query->where('property.state','like','%'.$search.'%')
            ->orWhere('property.contact_name','like','%'.$search.'%')
            ->orWhere('property.address','like','%'.$search.'%')
            ->orWhere('property.city','like','%'.$search.'%')
            ->orWhere('property.name','like','%'.$search.'%');
    });

}

Goal

I would like to use the $searchStates list to match my database record (which is in abbreviation format of the US states) and display the correct output in my search result page.

Question

Was this in_array function would work or should I use another function? I will appreciate your help.

Upvotes: 0

Views: 382

Answers (3)

iMarkDesigns
iMarkDesigns

Reputation: 1262

Ok, I end up doing remove other "queries" in my search function. Due to this, I realize the input will match not just the keywords base on what I need to search but instead, it will find what the nearest characters and match I have in the property result. So here's what end-up to my code.

if (array_key_exists( $search, $searchStates )) {
    $searchTerm = $searchStates[$search];

    $property->where(function($query) use ($searchTerm) {
        $query->where('property.state','like','%'.$searchTerm.'%');
    });
}

I have managed to choose this one, than the first one because of whenever I typed states full name, in the input field revert back to abbreviation.

"Ex. If I type California, after triggered the search, the California will turn to CA inside the search input."

Upvotes: 1

Poiz
Poiz

Reputation: 7617

Use array_key_exists instead like so:

    $searchStates = array(
        'Alabama'          =>'AL',
        'Alaska'           =>'AK',
        'Arizona'          =>'AZ',
        'Arkansas'         =>'AR',
        'California'       =>'CA',
        'Colorado'         =>'CO',
        'Connecticut'      =>'CT',
        'Delaware'         =>'DE',
        'Florida'          =>'FL',
        'Georgia'          =>'GA',
        'Hawaii'           =>'HI',
        'Idaho'            =>'ID',
        'Illinois'         =>'IL',
        'Indiana'          =>'IN',
        'Iowa'             =>'IA',
        'Kansas'           =>'KS',
        'Kentucky'         =>'KY',
        'Louisiana'        =>'LA',
        'Maine'            =>'ME',
        'Maryland'         =>'MD',
        'Massachusetts'    =>'MA',
        'Michigan'         =>'MI',
        'Minnesota'        =>'MN',
        'Mississippi'      =>'MS',
        'Missouri'         =>'MO',
        'Montana'          =>'MT',
        'Nebraska'         =>'NE',
        'Nevada'           =>'NV',
        'New Hampshire'    =>'NH',
        'New Jersey'       =>'NJ',
        'New Mexico'       =>'NM',
        'New York'         =>'NY',
        'North Carolina'   =>'NC',
        'North Dakota'     =>'ND',
        'Ohio'             =>'OH',
        'Oklahoma'         =>'OK',
        'Oregon'           =>'OR',
        'Pennsylvania'     =>'PA',
        'Rhode Island'     =>'RI',
        'South Carolina'   =>'SC',
        'South Dakota'     =>'SD',
        'Tennessee'        =>'TN',
        'Texas'            =>'TX',
        'Utah'             =>'UT',
        'Vermont'          =>'VT',
        'Virginia'         =>'VA',
        'Washington'       =>'WA',
        'West Virginia'    =>'WV',
        'Wisconsin'        =>'WI',
        'Wyoming'          =>'WY'
    );

    if (array_key_exists( $search, $searchStates )) {
        $searchTerm = $searchStates[$search];

        $property->where(function($query) use ($searchTerm) {
            $query->where('property.state','like','%'.$searchTerm.'%')
                  ->orWhere('property.contact_name','like','%'.$searchTerm.'%')
                  ->orWhere('property.address','like','%'.$searchTerm.'%')
                  ->orWhere('property.city','like','%'.$searchTerm.'%')
                  ->orWhere('property.name','like','%'.$searchTerm.'%');
        });

    }

Upvotes: 1

splash58
splash58

Reputation: 26153

Find abbreviation form of state name for usyng in query

if (isset($searchStates[$search])) {
     $search = $searchStates[$search];
     // rest of your code

Upvotes: 1

Related Questions