0

i am working on a search engine in jquery mobile I want to split words in an array for a database search the code I am using is:

var sear=$("#search").val();

var db = openDatabase("Database", "1.0", "PhoneGap Demo", 200000);
db.transaction(function (tx) {
  tx.executeSql("SELECT * FROM DEMO WHERE (D_Indications LIKE ?);",
                ["%"+sear+"%"],        
                function (tx, results) {
                  $('#output').show();
                  var len = results.rows.length, i;
                  var row=results.rows.item(i);
                  for (i = 0; i < len; i++) {
                    $('#output2').append('<tr><td>'+ row.Cat_name +'</td><td>'+ row.Drug_Caty +'</td><td>'+ row.G_Name +'</td><td>'+ row.B_Name +'</td><td>'+ row.D_Indications +'</td></tr>').trigger('create');        
                  }
                });
});

I want it to split words after space and search each word individually in database. For eg. if a user types "hello world" I want it to search "%hello%" & "%world%" separately in the database

4

1 回答 1

0

您可以通过拆分来定义它

   text = "Hello World"
   var SpaceCount = text.split(' ').length -1;

然后你可以通过 SpaceCount 定义你有多少空间。

那么有一个条件:

if (SpaceCount==1){
  var lines = text.split(' ')
       $.each(lines, function(key, line) {


          var db = openDatabase("Database", "1.0", "PhoneGap Demo", 200000);
            db.transaction(function (tx) {
           tx.executeSql("SELECT * FROM DEMO WHERE (D_Indications LIKE ?);",
            ["%"+ line +"%"],        
            function (tx, results) {
              $('#output').show();
              var len = results.rows.length, i;
              var row=results.rows.item(i);
              for (i = 0; i < len; i++) {
                $('#output2').append('<tr><td>'+ row.Cat_name +'</td><td>'+ row.Drug_Caty +'</td><td>'+ row.G_Name +'</td><td>'+ row.B_Name +'</td><td>'+ row.D_Indications +'</td></tr>').trigger('create');        
              }
            });
       }

}
于 2013-06-21T08:12:44.257 回答