2016-07-26 2 views
0

На данный момент мой код выполняет поиск данных, но вам нужно ввести полное имя, чтобы отобразить результат. Я думаю, мне нужно разбить полные имена на имена и фамилии. Таким образом, любой может быть найден и найден.PHP - поиск первых имен, а также полное имя

Я хочу, чтобы найти и профили отображения в соответствии с FULLNAME ИЛИ FirstName

-I это нужно, чтобы отобразить результаты для людей этого имени. Но в моем файле csv (где хранятся данные) полные имена сохраняются. «Боб Джеральд»

Я предполагаю, что мне нужно отделить значения в цикле для поиска первых имен? Я не очень уверен. Любая помощь будет принята с благодарностью.

Вот что у меня есть:

//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

Вы легенда !! Это то, чем я был. Работает в обаянии. Мне нравится то, как он излагался, поскольку это имеет смысл для меня. Спасибо, –

Смежные вопросы