• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[NEED HELP] Searching with different order of string or sentence

#1
[eluser]erviando[/eluser]
i want create a search method that i can Search with different order of string or sentence

for example in my database there is sentence "my name is the greatest name"

and i input with string "name greatest" in my search form

but with my code, i cannot show sentence "my name is the greatest name" with string "name greatest"

i can show sentence "my name is the greatest name" only if i input with macth order...

help me please....

js code
Code:
$(document).ready(function() {

// Get the "action" attribute specified in our form
var action = $('form').attr('action');

// Where we will show our results
var results = $('#results');

// Ajax search function
function ajax_search(search) {
  $.post(action, {
    search: search
    }, function(data) {
    
     // If we got some results
     if (data.length) {
      
      var el = $('<ul />');
      
      // For each of our search results
      $.each(data, function(i,item){
      
       $(el).append('<div class="row"><div class="spanundefined"><div class="alert"><span class="skripsi_nama">' + item.skripsi_nama + '</span> <br><span class="skripsi_judul">' + item.skripsi_judul_highlighed + '</span> <br> <span class="skripsi_abstrak">' + item.skripsi_abstrak + '</div></div></div>');
      });
      
      $(results).empty().append(el);
      //console.log(data);
     }
    
     // We got nothin'
     else {
      $(results).empty();
     }
    
   }, 'json');
}

// On keyup
$('#search').keyup(function() {
  
  // Get search string, remove all white space
  var search = $(this).val().replace(/^\s+|\s+$/g,"");
  
  // If our search string consists of at least one character
  if (search.length > 1) {
  
   // Run after 400 milliseconds
   clearTimeout($.data(this, 'timer'));
  
      var wait = setTimeout(function() {
    ajax_search(search)
   }, 400);

      $.data(this, 'timer', wait);

  }
  else
  {
   $(results).empty();
  }
});

});

controller code
Code:
function mencari()
{
  // If this page was POSTed to (and there is a search string)
  if ($search = $this->input->post('search', TRUE) )
  {
   // LIKE
   $this->db->select('*');
   $this->db->like('skripsi_judul', $search, 'both');
   $this->db->or_like('skripsi_nama', $search, 'both');
   $this->db->or_like('skripsi_abstrak', $search, 'both');
   $query = $this->db->get('skripsi');
   $results = array();
  
   if ($query->num_rows() > 0)
   {
    foreach ($query->result() as $row)
    {
     //$skripsi_judul_highlighted = str_ireplace($search , '<em>' . $search . '</em>', $row->skripsi_judul);
    
     // Where does our search appear in the full name?
     $start = stripos($row->skripsi_judul, $search);
    
     // Length of our search string
     $length = strlen($search);
    
     // Generate new sub string that reflects case of source
     $new_search = substr($row->skripsi_judul, $start, $length);
    
     // Generate highlighted search string
     $skripsi_judul_highlighted = str_replace($new_search , '<em>' . $new_search . '</em>', $row->skripsi_judul);
    
     $results[] = array(
      'skripsi_nama' => $row->skripsi_nama,
      'skripsi_judul' => $row->skripsi_judul,
      'skripsi_judul_highlighed' => $skripsi_judul_highlighted,
      'skripsi_abstrak' => $row->skripsi_abstrak,
     );
    }
   }
  
   // If it was an AJAX response
   if ($this->input->is_ajax_request())
   {
    // JSON headers
    $this->output->set_header("Cache-Control: no-cache, must-revalidate");
    $this->output->set_header("Expires: Mon, 4 Apr 1994 04:44:44 GMT");
    $this->output->set_header("Content-type: application/json");
    
    // Encode results as JSON
    echo json_encode($results);
   }
  
   // If it was not an AJAX response
   else
   {
    $data['results'] = $results;
    
    $this->load->view('admin/tabel_topikma_pencarian', $data);
   }
  }
  
  // If this page was not posted to
  else
  {
   $this->load->view('admin/tabel_topikma_pencarian');
  }
}


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.