kerry sun
kerry sun

Reputation: 68

Making a live search function

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!

Upvotes: 0

Views: 311

Answers (1)

Abhinav
Abhinav

Reputation: 408

    $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.

Upvotes: 1

Related Questions