dongwen9947 2016-07-26 02:42
浏览 39
已采纳

PHP - 搜索名字和全名

At the moment my code searches data, but you need to type in the full name to display a result. I think I need to split my fullnames into first names and lastnames. So either can be searched and found.

I want it to find and display profiles according to fullname OR firstname

-I need it to display results for people of that first name. But in my csv file (which is where the data is stored), the full names are stored E.g. "Bob Gerald"

I am guessing I need to separate values in the loop to search first names as well? Im not really sure.. Any help would be greatly appreciated.

Here is what I have:

//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条回答 默认 最新

  • douji1853 2016-07-26 03:10
    关注

    Please use this to search in the fullname column:

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

    I hope this will help you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?