TMNuclear
TMNuclear

Reputation: 1175

PHP Search Engine - Search for multiple keywords

I have this code for my search engine:

    if(isset($_POST['search']))
    {
        $search = $_POST['search'];
        $terms = explode(",", $search);
        $projectlistquery = "
            SELECT project.accid, customer.custid, projectid, project.datefrom, custnamecode, dateto, daluur, project.gebruikersid, gebruikers.gebruikersid, 
            type, typeid, typename, custsurname, custmidname, custforename, acc1cust, acc2date, acc3nrofproj, acc4user, project.crew, project.jobinstr, project.projectinfo, usersurname, usermidname, userforename
            FROM project
                INNER JOIN type on project.type = type.typeid
                INNER JOIN customer on project.custid = customer.custid
                INNER JOIN accountingcode on project.accid = accountingcode.accid
                INNER JOIN gebruikers on project.gebruikersid = gebruikers.gebruikersid         
            WHERE
        ";
        $i = 0;
        foreach ($terms as $each) 
        {
            if ($i++ > 0) 
            $projectlistquery .= ' OR ';

            $projectlistquery .= "concat(custsurname, custforename, custmidname, custpostal, custphone1, custphone2, custfax, custnamecode, acc1cust, acc2date, acc3nrofproj, acc4user, typename, usersurname, usermidname, userforename) LIKE '%$each%' ";
        }
        $projectlistquery .= " ORDER BY projectid ";
    }
    else
    $projectlistquery= "    // query to show all records.

Now this works totally fine for searching 1 key.

If I search for a name: Hans van Meulen, it shows all names with Hans, all names with 'van' in it and Meulen. So if I have Dude van Berkel in my database, he will be shown too. And I want to search for the 3 strings together.

Is this achieveable with IMPLODE? $terms = implode(",", $search); returns a MySQL syntax error at: 'ORDER BY projectid', though.
Or do I have to adjust my SQL?

Upvotes: 1

Views: 704

Answers (1)

Alex
Alex

Reputation: 421

You just remove the explode(",", $search); part so that your search-string is intact. What you are doing now is basically tearing it apart for searching on every word. That should leave you with the "Hans van Meulen" as the search string.

Upvotes: 1

Related Questions