doushadu0901 2018-02-05 05:57
浏览 51
已采纳

制作实时搜索功能

I've made a crud function where users register/log in to view their own contact list. The mysql database has tables of details such as name, mobile, email, company, title etc. I want to implement a live search function where the user can type in something such as e.g. first name + title or whatever random combination, and for the live search to be able to match the search field(s).

What is your recommendation in making something that fulfills the above?

Many thanks!

  • 写回答

1条回答 默认 最新

  • doubian0284 2018-02-05 06:20
    关注
        $result = array();
        $Query = "SELECT * FROM contact_list  WHERE ";
        $keyword = preg_split("/[\s,-]+/", $q);
            $flag = 0;
    
            while ($flag<count($keyword))
            {
                if($flag==0)
                    $Query.="  name LIKE '%".$keyword[$flag]."%' OR title LIKE '%".$keyword[$flag]."%'";
                else
                    $Query.=" OR name LIKE '%".$keyword[$flag]."%' OR title LIKE '%".$keyword[$flag]."%'";
                $flag++;
            }
            $Query .= " ORDER BY `name` ASC";
    
            $exec = $this->db->query($Query);
            foreach ($exec->result() as $row)
            {
                array_push($result,$row);
            }
    

    This code i have done in codigniter. you can change it as you needed.. i hope this is you want.

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

报告相同问题?