2016-07-23 83 views
0

我有我的頁面,在這裏用戶可以能夠搜索到美國各州找物業上的搜索功能。但是,在我的數據庫記錄中,僅在每個州名上設置了縮寫。所以我以陣列列表格式下載美國各州的副本。PHP in_array匹配搜索值的數組列表

碼圖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功能會工作或我應該使用其他功能?我會感謝你的幫助。在查詢

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

分享完整'碼圖。2'? – C2486

+0

'if(isset($ searchStates [$ search])){ $ search = $ searchStates [$ search];' – splash58

+0

你不能更新你的數據庫表的全名或者甚至創建一個新表並加入縮寫 – RamRaider

回答

1

好吧,我最終會做刪除我的搜索功能之外的「查詢」。由於這個原因,我意識到輸入將不僅僅匹配基於我需要搜索的關鍵字,而是會找到屬性結果中最接近的字符和匹配項。所以,這是我的代碼的最終結果。

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

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

我已成功地選擇這一項,不是因爲每當我鍵入的第一個州的全名,在輸入字段恢復到縮寫。

「實例如果鍵入加州,觸發後搜索,加州將轉向CA搜索裏面輸入」。

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

    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.'%'); 
     }); 

    } 
+0

我試過的建議,我用加州不過之後,它最終會顯示其它狀態,以及...請查看結果的屏幕抓​​取。 HTTPS://www.dropbox。com/s/hamrlz5u5da1d1j /截圖%202016-07-23%2018.22.45.png?dl = 0 這裏是實際結果,如果使用結果頁面中的狀態下拉列表。 https://www.dropbox.com/s/n8fzr9p4dz5k339/Screenshot%202016-07-23%2018.23.04.png?dl=0 – iMarkDesigns

1

使用array_key_exists usyng,而不是像這樣狀態的名字

+0

我試過這個,但是有些狀態仍然與搜索結果混合在一起。恩。我搜索「佛羅里達」,在我的輸出中,它顯示所有佛羅里達州,但結果裏面有「新澤西州佛蘭德斯」。 我猜測,「佛蘭德斯」似乎反映到關鍵字「FL」,不是嗎? – iMarkDesigns

+0

@MarkDesigns這僅僅是因爲你的查詢......你的搜索說匹配「聯繫人姓名」,「州」,「地址」,「城市」,「名稱」中的任一個或全部, (例如)在其中。這就是爲什麼你會得到那麼多結果...總之;只是重新審視你的查詢並從那裏解決它... – Poiz

+0

好吧,它現在有意義... – iMarkDesigns