2016-07-26 27 views
0

Im Moment sucht mein Code Daten, aber Sie müssen den vollständigen Namen eingeben, um ein Ergebnis anzuzeigen. Ich denke, ich muss meine vollständigen Namen in Vornamen und Nachnamen aufteilen. So kann man entweder suchen und finden.PHP - Suchen Sie nach Vornamen sowie den vollständigen Namen

Ich will es und Anzeigeprofile finden nach Vollername oder Vorname

-I es brauchen für die Menschen dieser Vornamen Ergebnisse anzuzeigen. Aber in meiner csv-Datei (wo die Daten gespeichert werden), werden die vollständigen Namen gespeichert. "Bob Gerald"

Ich rate, ich brauche Werte in der Schleife zu trennen, um auch Vornamen zu suchen? Ich bin nicht wirklich sicher .. Jede Hilfe würde sehr geschätzt werden.

Hier ist, was ich habe:

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

Antwort

1

Bitte verwenden Sie diese in der fullname Spalte suchen:

$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])); 
}); 

Ich hoffe, dies wird Ihnen helfen.

+0

Sie Legende !! Genau danach war ich. Funktioniert ein Charme. Ich liebe die Art und Weise, wie sie sich auf den Weg macht, da es für mich einen Sinn ergibt. Danke, –