Welcome Guest, Not a member yet? Register   Sign In
how to make case insensitive search
#1

[eluser]ashutosh[/eluser]
i am using jQuery `autcompleter` for searching.

From my below code my search is happening only on case-sensitive , i want to make case insensitive search.
eg. `$array={the king, The king, The man}`

And my search keyword is "The" then currently I am getting only the output like `{The king, The man}`
I am not getting `the king` because it is in lower case.

what I want is ,if I write "the" then I should get all the three results.


Please help me to solve my problem

view.php
Code:
[removed]
    $(document).ready(function() {
            $(function() {
                    $( "#autocomplete" ).autocomplete({
                        
                        
                            source: function(request, response) {

                         $.ajax({ url: "<?php echo base_url().'search/suggestions'?>",
                                    data: { term: $("#autocomplete").val()},
                                    dataType: "json",
                                    type: "POST",
                                    success: function(data){

                                            response(data);
                                    }
                            });
                    },
                    
                    minLength: 3,
                    select: function (a, b) {
        $(this).val(b.item.value);
        $(".searchform1").submit()
    }
                    });
            });
            $('#autocomplete').focus();
    });
    
          
      function monkeyPatchAutocomplete() {

          // Don't really need to save the old fn,
          // but I could chain if I wanted to
          var oldFn = $.ui.autocomplete.prototype._renderItem;

          $.ui.autocomplete.prototype._renderItem = function( ul, item) {
              var re = new RegExp("^" + this.term, "i") ;
              var t = item.label.replace(re,"<span >" + this.term + "</span>");
              return $( "<li></li>" )
                  .data( "item.autocomplete", item )
                  .append( "<a>" + t + "</a>" )
                  .appendTo( ul );
          };
      }


      $(document).ready(function() {

          monkeyPatchAutocomplete();

          $("#input1").autocomplete({
              // The source option can be an array of terms.  In this case, if
              // the typed characters appear in any position in a term, then the
              // term is included in the autocomplete list.
              // The source option can also be a function that performs the search,
              // and calls a response function with the matched entries.
              source: function(req, responseFn) {
                  addMessage("search on: '" + req.term + "'<br/>");
                  var re = $.ui.autocomplete.escapeRegex(req.term);
                  var matcher = new RegExp( "^" + re, "i" );
                  var a = $.grep( wordlist, function(item,index){
                      //addMessage("&nbsp;&nbsp;sniffing: '" + item + "'<br/>");
                      return matcher.test(item);
                  });
                  addMessage("Result: " + a.length + " items<br/>");
                  responseFn( a );
              },

              select: function(value, data){
                  if (typeof data == "undefined") {
                      addMessage('You selected: ' + value + "<br/>");
                  }else {
                      addMessage('You selected: ' + data.item.value + "<br/>");
                  }
              }
          });
      });
    [removed]
         ---------------
         ---------------
            &lt;form class="searchform1" acti echo base_url().'search/searchresult/pgn/grid/'?&gt;" method="POST"&gt;
                            <img src="&lt;?php echo base_url()?&gt;css/images/icons/searchicon.png" >  
                            &lt;input id="autocomplete" value="&lt;?php  if(isset($srchvalue)){echo $srchvalue ;} ?&gt;" class="deletable"   type="text" name="keywords"  30px;width: 450px;font-size: 16px;border: 0;border-bottom:solid 4px #dfdfdf;border-top:solid 4px #dfdfdf;"  maxlength="70" placeholder="  Search for Title, Author Name, Isbn , Publisher"/&gt;
                            
                            &lt;input type="submit" value="&nbsp;&nbsp;Search&nbsp;&nbsp;" class="searchbtn"/&gt;
           &lt;/form&gt;

controller.php
Code:
function suggestions()
     {
$this->load->model('Booksmodel');
$term = $this->input->post('term');
          //  $term='The Sleepwalke';
        $json_array = $this->Booksmodel->GetMatchAutocomplete($term);
        echo json_encode($json_array);
    }
model.php
Code:
function GetMatchAutocomplete($keyword)
        {
        $this->load->database();  
        $this->db->limit('10');  
        $this->db->select('book_title,auth_firstname,isbn,auth_lastname,publisher_name');  
        $this->db->like('book_title', $keyword);
        $this->db->or_like('auth_firstname', $keyword);
        $this->db->or_like('isbn', $keyword);
        $this->db->or_like('auth_lastname', $keyword);
        $this->db->or_like('publisher_name', $keyword);  
      //  $this->db->group_by(array("book_title", "auth_firstname", "isbn", "auth_lastname", "publisher_name"));
        $res = $this->db->get('bookdetails');
        $ret = array();
        foreach ($res->result_array() as $row)
        {
            foreach ($row as $val)
            {
                if (FALSE !== strpos($val, $keyword))
                {
                    $ret[] = $val;
                    break;
                }
            }
        }
        return $ret;            
        }
#2

[eluser]TheFuzzy0ne[/eluser]
strpos() has a case-insensitive version too. I leave you to find out what it is. Smile
#3

[eluser]ashutosh[/eluser]
i used this
Code:
stripos() - Find the position of the first occurrence of a case-insensitive substring in a string
instead of strpos(). Its working fine
#4

[eluser]TheFuzzy0ne[/eluser]
Well done. Smile




Theme © iAndrew 2016 - Forum software by © MyBB