2016-07-26 53 views
0

私のコードはデータを検索しますが、結果を表示するにはフルネームを入力する必要があります。私はフルネームをファーストネームとラストネームに分割する必要があると思います。だからどちらかを検索して見つけることができます。私はフルネームOR FIRSTNAMEPHP - ファーストネームとフルネームを検索

-Iによると、それはプロファイルを見つけて表示したい

はそれが最初の名前の人々のために結果を表示する必要があります。しかし、私のcsvファイル(データが格納されている場所)には、フルネームが保存されています。 "Bob Gerald"

私は最初の名前を検索するためにループ内の値を分離する必要があると思いますか?私は本当に確信していません..どんな助けも大歓迎です。ここで

は私が持っているものです。

//This gathers the values from the form (search bar) and assigns it to the variable $SearchThis 
//isset() 
$SearchThis = isset($_POST['Search']) ? $_POST['Search'] : ''; 
//empty() 
$SearchThis = !empty($_POST['Search']) ? $_POST['Search'] : ''; 



// Grabs the csv file (and its existing data) and makes it into an array 
$csv = array(); 
$lines = file('data/StaffData.csv', FILE_IGNORE_NEW_LINES); 
foreach ($lines as $key => $value) 
{ 
    $csv[$key] = str_getcsv($value); 
} 



//A new array which will display the search results 
$new_csv = array(); 

//This displays which rows have matched the search (it is put in an array) 

//Looks through full names 
$keys = array_keys(array_column($csv, 0), $SearchThis); // original code 
foreach($keys as $index) {        // Iterate over the keys 
    $new_csv[] = $csv[$index];       // Copy the matching rows to our new array 
} 
//Looks through phone numbers 
$keys = array_keys(array_column($csv, 1), $SearchThis); // original code 
foreach($keys as $index) {        // Iterate over the keys 
    $new_csv[] = $csv[$index];       // Copy the matching rows to our new array 
} 
//Looks through gender 
$keys = array_keys(array_column($csv, 2), $SearchThis); // original code 
foreach($keys as $index) {        // Iterate over the keys 
    $new_csv[] = $csv[$index];       // Copy the matching rows to our new array 
} 
//Looks through Birthday 
$keys = array_keys(array_column($csv, 3), $SearchThis); // original code 
foreach($keys as $index) {        // Iterate over the keys 
    $new_csv[] = $csv[$index];       // Copy the matching rows to our new array 
} 

//Looks through Type of work 
$keys = array_keys(array_column($csv, 4), $SearchThis); // original code 
foreach($keys as $index) {        // Iterate over the keys 
    $new_csv[] = $csv[$index];       // Copy the matching rows to our new array 
} 

答えて

1

fullname列で検索するためにこれを使用してください:

$new_csv = array_filter($csv, function ($item) use($SearchThis) { 
    //Match the full name 
    if ($item[0] === $SearchThis) { 
     return true; 
    } 
    //Split the fullname by space characters into array 
    //and see if it contains the $SearchThis 
    return in_array($SearchThis, preg_split("/[\s]+/", $item[0])); 
}); 

私は、これはあなたを助けることを願っています。

+0

あなたは伝説です!これは私が後にしたものです。魅力を発揮します。私はそれが私に意味をなさないように設定されている方法が大好きです。 ありがとうございます、 –