我的页面上有搜索功能,用户可以搜索美国州找房产。但是,在我的数据库记录中,仅在每个州名称上设置了缩写。所以我以数组列表格式下载美国各州的副本。
代码图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'
);
案例:
我的客户用来搜索州名ex: Tennessee
,但在我的搜索结果中,它会显示无记录,因为缩写仅保存在我的数据库中。我想使用$searchStates
的这个数组列表来匹配我的状态缩写列表。我尝试使用in_array
函数,但似乎无法正常工作。看看我的实际代码。
代码图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.'%');
});
}
目标
我想使用$searchStates
列表来匹配我的数据库记录(它是美国各州的缩写格式),并在我的搜索结果页面中显示正确的输出。
问题
这个in_array
功能是否有用,还是应该使用其他功能?我将非常感谢你的帮助。
答案 0 :(得分:1)
在查询中查找usyng的州名缩写形式
if (isset($searchStates[$search])) {
$search = $searchStates[$search];
// rest of your code
答案 1 :(得分:1)
使用array_key_exists
代替:
$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.'%');
});
}
答案 2 :(得分:1)
好的,我最终会删除其他"查询"在我的搜索功能。因此,我意识到输入不仅会匹配我需要搜索的关键字,而是会找到最接近的字符和我在属性结果中匹配的内容。所以这就是我的代码的最终结果。
if (array_key_exists( $search, $searchStates )) {
$searchTerm = $searchStates[$search];
$property->where(function($query) use ($searchTerm) {
$query->where('property.state','like','%'.$searchTerm.'%');
});
}
我设法选择这个,而不是第一个因为每当我输入状态全名时,在输入字段中恢复为缩写。
"实施例。如果我输入加利福尼亚州,在触发搜索后,加利福尼亚州将在搜索输入中转向CA."