5

i am using jQuery autocompleter 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

<script type="text/javascript">
$(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 style='font-weight:bold;color:Black;'>" + 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/>");
              }
          }
      });
  });
</script>
     ---------------
     ---------------
        <form class="searchform1" action="<?php echo base_url().'search/searchresult/pgn/grid/'?>" method="POST"> 
                        <img src="<?php echo base_url()?>css/images/icons/searchicon.png" style="vertical-align:middle;"/>  
                        <input id="autocomplete" value="<?php  if(isset($srchvalue)){echo $srchvalue ;} ?>" class="deletable"   type="text" name="keywords" style="height: 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"/>

                        <input type="submit" value="&nbsp;&nbsp;Search&nbsp;&nbsp;" class="searchbtn"/>
       </form> 
Dan
  • 2,086
  • 11
  • 71
  • 137

2 Answers2

4

Avoid using alot of like operators. Full text search is the way to go. Full txtsearch are case-insensitive by default. So you will have no problem with that.

Here is how to integrate mysql fulltext with codeigniter

Using Match and Against in MySQL and CodeIgniter

Community
  • 1
  • 1
Muhammad Nasir
  • 1,796
  • 1
  • 14
  • 22
  • http://stackoverflow.com/questions/2876789/case-insensitive-for-sql-like-wildcard-statement – Muhammad Nasir Mar 09 '13 at 04:53
  • please modify my code and add your logic code. Then lets test it – Dan Mar 09 '13 at 04:55
  • Is this one nice or its difficult to integrate such query with codeigniter? – Muhammad Nasir Mar 09 '13 at 04:55
  • i tried like this: but i am gettting error like "calling non object on $res->result_array() . My code: `$this->load->database(); $this->db->select("book_title","auth_firstname"); $this->db->where ("MATCH (`book_title`, `auth_firstname`) AGAINST ('{$keyword}')"); $this->db->or_where("book_title LIKE '%{$keyword}%'"); $this->db->or_where("auth_firstname LIKE '%{$keyword}%'"); $res = $this->db->get('bookdetails');$ret = array(); foreach ($res->result_array() as $row)` – Dan Mar 09 '13 at 05:53
  • did you add the fulltext search to your table or you just run the query? – Muhammad Nasir Mar 09 '13 at 06:05
  • you can add fulltext search to your table by : ALTER TABLE table_name ADD FULLTEXT(field1, field2); – Muhammad Nasir Mar 09 '13 at 06:08
  • please modify my code and post it as answer. its very difficult – Dan Mar 09 '13 at 06:11
0

Do you intentionally use a case sensitive collation on your table? Changing to a case insensitive collation would solve your problem. Another solution would be to match your string in lower (or upper) case. Something like this:

$this->db->or_like('LOWER(auth_lastname)', strtolower($keyword));

Also, full text search is not possible if you use InnoDB as storage engine in mysql

jonixj
  • 397
  • 3
  • 11