문제

I want search names using my search module with ignoring white spaces .

Ex: if i want to search a name like "A B Zilva"

i want to disply the results on dropdown even if i type the name like "AB Zilva".

currently search without space is not working

   $db = JFactory::getDbo();
$searchp=$_GET["term"];
$searchp = str_replace(' ','%%', trim($searchp));
//$searchp= preg_split('/ /', $searchp);
$query = $db -> getQuery(true);

 $query="SELECT * FROM content where title like '%".$searchp."%'AND categories_id=82 order by title ASC ";
$db -> setQuery($query);
// Load the results as a list of associated arrays.
$results = $db -> loadAssocList();
$json=array();
foreach ($results as $json_result) {
    $json[] = array('value' => $json_result["title"], 'label' => $json_result["title"]);
}

Please advice .

update :

jQuery.extend( jQuery.ui.autocomplete.prototype, {
              _renderItem: function( ul, item ) {
                  var term = this.element.val(),
                      regex = new RegExp( '(' + term + ')', 'gi' );
                  html = item.label.replace( regex , "<b>$&</b>" );
                  return jQuery( "<li></li>" )
                      .data( "item.autocomplete", item )
                      .append( jQuery("<a></a>").html(html) )
                      .appendTo( ul );
              }
          });
도움이 되었습니까?

해결책

The problem with your code is that when you replace space with %%, you get a search like:

WHERE title like '%AB%%Zilva%'

but this won't match when the title is A B Zilva. You need to remove spaces from both the search string and the column in the database. Do it like this:

$searchp = str_replace(' ', '', $searchp);

and then change the SQL to:

WHERE REPLACE(title, ' ', '') LIKE '%$searchp%'

For the Javascript highlighting, I think you need to do this:

var term = this.element.val().replace(/ /g, '').replace(/.(?=.)/g, '$& *'),

This will create a regular expression that allows for spaces between any of the characters in the search string.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top