CodeIgniter "like()" function with % wildcard inside search terms

JrrMaster

Let's say I have function like this:

 public function get_list($category = '', $limit = 10, $offset = 0) {
        if (!empty($category))
            $this->db->where('category', $category);
        $search = $this->input->get('search');
        if (!empty($search))
            $this->db->or_like(array('foo_column'=>$search));
        $query = $this->db->get('table_name', $limit, $offset);
        //echo $this->db->last_query();
        return $query->result();
 }

Produce query as :

SELECT * FROM table_name WHERE foo_column LIKE '%match something%'

As you can see the % wildcard can be added in both side, before and after.

And how if I want to produce like:

... WHERE foo_column LIKE '%match%something%'?

FYI, I use str_replace() function to change space to % but codeigniter always escape it with slash. It Produces query like:

... WHERE foo_column LIKE '%match\%something%'

This is useful when to search match another something with keyword match something, and wildcard on first and/or after seems doesn't work.

Narendrasingh Sisodia

In order to achieve such kind of functionality I've updated your code with some different conditions as like.

Note: Here I've manually placed values of categories and search

public function get_list($category = '', $limit = 10, $offset = 0) {
    $category = 'electronics';
    if (!empty($category)) {
        $this->db->where('category', $category);
    }
    $search = 'match something';
    if (preg_match('/\s/', $search) > 0) {
        $search = array_map('trim', array_filter(explode(' ', $search)));
        foreach ($search as $key => $value) {
            $this->db->or_like('foo_column', $value);
        }
    } else if ($search != ''){
        $this->db->like('foo_column', $search);
    }
    $query = $this->db->get('table_name', $limit, $offset);
    return $query->result();
}

Here $search = 'match something' and this'll generate the query as follows:

SELECT * FROM (`table_name`) WHERE `category` = 'electronics' AND  
`foo_column` LIKE '%match%' OR `foo_column` LIKE '%something%' LIMIT 10

If $search = 'match something another' then it'll generate the query as

SELECT * FROM (`table_name`) WHERE `category` = 'electronics' AND 
`foo_column` LIKE '%match%' OR `foo_column` LIKE '%something%' OR 
`foo_column` LIKE '%another%' LIMIT 10

and if $search = 'match' the it'll generate the query as

SELECT * FROM (`table_name`) WHERE `category` = 'electronics' AND 
`foo_column` LIKE '%match%' LIMIT 10

and if $search = '' the it'll generate the query as

SELECT * FROM (`table_name`) WHERE `category` = 'electronics' LIMIT 10

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

CodeIgniter "like()" function with % wildcard inside search terms

From Dev

Codeigniter like wildcard

From Dev

Using codeigniter like query with wildcard

From Dev

How to search terms with wildcard in elasticsearch using querystringquery

From Dev

wildcard search using replace function

From Dev

Excel Function, Search for Max wildcard (*)

From Dev

MySQL - LIKE with trailing wildcard vs range search

From Dev

function(msg) - Search inside

From Dev

T-SQL special characters to escape for LIKE operator wildcard search

From Dev

How to write wildcard search query in couchdb where name like 'a%'

From Dev

how to create search function in codeigniter

From Dev

how to create search function in codeigniter

From Dev

Codeigniter search function throws an error

From Dev

how to search and highlight terms excluding content inside a tag?

From Dev

Why do Google Trends for search terms show strong weekly fluctuations for terms like selenium?

From Dev

Wildcard subdomain and CodeIgniter routes

From Dev

return multiple values inside function method in codeigniter

From Dev

Codeigniter Call Parent Controller Function Inside Model

From Dev

How to mix PHP with a string inside CodeIgniter function?

From Dev

return multiple values inside function method in codeigniter

From Dev

mysql union like query with multiple table search in codeigniter

From Dev

Prepared statement LIKE wildcard

From Dev

numerical wildcard in LIKE query?

From Dev

SQL LIKE wildcard assistance

From Dev

numerical wildcard in LIKE query?

From Dev

sql: like with wildcard

From Dev

Powershell Like Wildcard issue

From Dev

Ctags like function code search for javascript with Nodeclipse

From Dev

Highlight search terms in a string

Related Related

  1. 1

    CodeIgniter "like()" function with % wildcard inside search terms

  2. 2

    Codeigniter like wildcard

  3. 3

    Using codeigniter like query with wildcard

  4. 4

    How to search terms with wildcard in elasticsearch using querystringquery

  5. 5

    wildcard search using replace function

  6. 6

    Excel Function, Search for Max wildcard (*)

  7. 7

    MySQL - LIKE with trailing wildcard vs range search

  8. 8

    function(msg) - Search inside

  9. 9

    T-SQL special characters to escape for LIKE operator wildcard search

  10. 10

    How to write wildcard search query in couchdb where name like 'a%'

  11. 11

    how to create search function in codeigniter

  12. 12

    how to create search function in codeigniter

  13. 13

    Codeigniter search function throws an error

  14. 14

    how to search and highlight terms excluding content inside a tag?

  15. 15

    Why do Google Trends for search terms show strong weekly fluctuations for terms like selenium?

  16. 16

    Wildcard subdomain and CodeIgniter routes

  17. 17

    return multiple values inside function method in codeigniter

  18. 18

    Codeigniter Call Parent Controller Function Inside Model

  19. 19

    How to mix PHP with a string inside CodeIgniter function?

  20. 20

    return multiple values inside function method in codeigniter

  21. 21

    mysql union like query with multiple table search in codeigniter

  22. 22

    Prepared statement LIKE wildcard

  23. 23

    numerical wildcard in LIKE query?

  24. 24

    SQL LIKE wildcard assistance

  25. 25

    numerical wildcard in LIKE query?

  26. 26

    sql: like with wildcard

  27. 27

    Powershell Like Wildcard issue

  28. 28

    Ctags like function code search for javascript with Nodeclipse

  29. 29

    Highlight search terms in a string

HotTag

Archive