Ok guys, i 'm starting here this topic about how we can improve the search functionality one step at a time. If any of you have and want to share your tips and ideas, you are all welcome. So Heads Up and let's start!
SEARCH FOR LISTINGS USING WILDCARDS OR/AND BY USERNAME
Add this function in your themes's function.php file:
function cbx_search_keyword_wildcard($params) {
if (isset($params['sPattern'])) {
$mSearch = Search::newInstance();
$query_elements = (array) json_decode($mSearch->toJson());
$pattern = $query_elements['sPattern'];
$query_elements['sPattern'] = str_replace('', '*', $pattern) . '*';
$mSearch->setJsonAlert($query_elements);
}
}
osc_add_hook('search_conditions', 'cbx_search_keyword_wildcard', 1);
This way you can use wildcards in your keywords but without the need of typing the asterisk *. This means that you can type merced without the need to type merced* to search for listings contain the word mercedes This is more convenience and more user friendly for sure.
Do you want also to be able to search for listings by username ? You can easily do that by replacing the above code with the following one...
function cbx_search_keyword_wildcard($params) {
if (isset($params['sPattern'])) {
$mSearch = Search::newInstance();
$query_elements = (array) json_decode($mSearch->toJson());
$pattern = $query_elements['sPattern'];
$query_elements['sPattern'] = str_replace('', '*', $pattern) . '*';
$mSearch->setJsonAlert($query_elements);
$mSearch->addJoinTable(count($query_elements['tables_join']), DB_TABLE_PREFIX."t_user u", "fk_i_user_id = u.pk_i_id", 'INNER');
$aPattern = explode(' ', $pattern);
$userNameCond = '';
foreach ($aPattern as $word) {
if ($word) $userNameCond .= sprintf(" || u.s_username = '%s'", $word);
}
$mSearch->addConditions("1 = 1 " . $userNameCond);
$mSearch->addGroupBy(DB_TABLE_PREFIX.'t_item.pk_i_id');
}
}
osc_add_hook('search_conditions', 'cbx_search_keyword_wildcard', 1);
Now if there a user with the username "John" in your site and type "John" in the search field you will get results with listings that belong only to this user.
Frontend Dev / UX-UI Designer / Illustrator / Father / Linux Enthusiast / Minimalist
SEARCH FOR LISTINGS BY THEIR ID
Do you want to search for listings by their id ?
You can download and use this plugin by [member=65]Drizzlethemes[/member]
Frontend Dev / UX-UI Designer / Illustrator / Father / Linux Enthusiast / Minimalist
SEARCH FOR LISTINGS BY THE CATEGORY NAME THEY BELONG
What about search for listings by the category name they belong??
Yes, you can do this very easily by using this code here:
function cbx_hide_categ_in_description($desc=null, $catId=null) {
$Cat = Category::newInstance()->toRootTree($catId);
$d = '###';
foreach($Cat as $c) {
$d = $d . $c["s_name"] .' ';
}
if(endsWith($desc, $d))
return str_replace($d,'',$desc);
else return $desc;
}
function endsWith($haystack=null, $needle=null)
{
$length = strlen($needle);
return $length === 0 ||
(substr($haystack, -$length) === $needle);
}
function cbx_addcategory($desc, $catId) {
$Cat = Category::newInstance()->toRootTree($catId);
$d = '###';
foreach($Cat as $c) {
$d = $d . $c["s_name"] .' ';
}
if(endsWith($desc, $d))
return $desc;
else return $desc . $d;
}
function cbx_filter_description($aItem) {
foreach(@$aItem['description'] as $key => $value) {
$aItem['description'][$key] = cbx_addcategory($value,$aItem['catId']);
}
return $aItem;
}
osc_add_filter('item_add_prepare_data', 'cbx_filter_description');
osc_add_filter('item_edit_prepare_data', 'cbx_filter_description');
With this code when you post or edit an item the name of the categories this item is belong to, will be add to the item's description textarea automatically so you will be able to search for items by their category name. For example if you have a category with the name "smartphones" and type smartphones in the search field you will get all listings that belong to the category "smartphones"
The code above supposed to hide of-course the category name from the item description when you view the item's detail page but for some reason doesnt work properly.
Maybe [member=3]WEBmods[/member] could help a little here as it is a very nice function.
Frontend Dev / UX-UI Designer / Illustrator / Father / Linux Enthusiast / Minimalist
Hi [member=61]subzero[/member] your post was really helpful..just wondering that is it possible to search by one of the custom field dropdown box?...
Thanks in advance
Can you give me some idea where to insert this code in functions.php? I'm using Letgo theme and there's a LOT of if( !function_exists( sub-routines.
Can you give me some idea where to insert this code in functions.php? I'm using Letgo theme and there's a LOT of if( !function_exists( sub-routines.
Just at the end of the file, but before "?>" if there's one.
Available for custom Osclass development. www.defected.dev
Works like a champ!
So, for some reason search functionality ceased working altogether today. I had to remove the user name search feature in order to get it working again. Wildcard still works like a champ. Don't know why the other one quit. I'm quite certain it was working when I first made the change. Not that I really NEED user name search. Just weird.
[member=61]subzero[/member] Have you had such problems? I didn't use the code above so have no idea.
Available for custom Osclass development. www.defected.dev
Ok guys, i 'm starting here this topic about how we can improve the search functionality one step at a time. If any of you have and want to share your tips and ideas, you are all welcome. So Heads Up and let's start!
SEARCH FOR LISTINGS USING WILDCARDS OR/AND BY USERNAME
Add this function in your themes's function.php file:
function cbx_search_keyword_wildcard($params) {
if (isset($params['sPattern'])) {
$mSearch = Search::newInstance();
$query_elements = (array) json_decode($mSearch->toJson());
$pattern = $query_elements['sPattern'];
$query_elements['sPattern'] = str_replace('', '*', $pattern) . '*';
$mSearch->setJsonAlert($query_elements);
}
}
osc_add_hook('search_conditions', 'cbx_search_keyword_wildcard', 1);This way you can use wildcards in your keywords but without the need of typing the asterisk *. This means that you can type merced without the need to type merced* to search for listings contain the word mercedes This is more convenience and more user friendly for sure.
Do you want also to be able to search for listings by username ? You can easily do that by replacing the above code with the following one...
function cbx_search_keyword_wildcard($params) {
if (isset($params['sPattern'])) {
$mSearch = Search::newInstance();
$query_elements = (array) json_decode($mSearch->toJson());
$pattern = $query_elements['sPattern'];
$query_elements['sPattern'] = str_replace('', '*', $pattern) . '*';
$mSearch->setJsonAlert($query_elements);
$mSearch->addJoinTable(count($query_elements['tables_join']), DB_TABLE_PREFIX."t_user u", "fk_i_user_id = u.pk_i_id", 'INNER');
$aPattern = explode(' ', $pattern);
$userNameCond = '';
foreach ($aPattern as $word) {
if ($word) $userNameCond .= sprintf(" || u.s_username = '%s'", $word);
}
$mSearch->addConditions("1 = 1 " . $userNameCond);
$mSearch->addGroupBy(DB_TABLE_PREFIX.'t_item.pk_i_id');
}
}
osc_add_hook('search_conditions', 'cbx_search_keyword_wildcard', 1);Now if there a user with the username "John" in your site and type "John" in the search field you will get results with listings that belong only to this user.
Does this wildcard functionality still work?
[member=61]subzero[/member] Have you had such problems? I didn't use the code above so have no idea.
[member=3]WEBmods[/member] [member=116]Izzyboi[/member] [member=121]Zero-Labs[/member] For me works pretty fine. Could you please upload the error log results here so we can check what is the issue?
Frontend Dev / UX-UI Designer / Illustrator / Father / Linux Enthusiast / Minimalist
[member=61]subzero[/member] Have you had such problems? I didn't use the code above so have no idea.
[member=3]WEBmods[/member] [member=116]Izzyboi[/member] [member=121]Zero-Labs[/member] For me works pretty fine. Could you please upload the error log results here so we can check what is the issue?
How do I get the error log results
Ok guys, i 'm starting here this topic about how we can improve the search functionality one step at a time. If any of you have and want to share your tips and ideas, you are all welcome. So Heads Up and let's start!
SEARCH FOR LISTINGS USING WILDCARDS OR/AND BY USERNAME
Add this function in your themes's function.php file:
function cbx_search_keyword_wildcard($params) {
if (isset($params['sPattern'])) {
$mSearch = Search::newInstance();
$query_elements = (array) json_decode($mSearch->toJson());
$pattern = $query_elements['sPattern'];
$query_elements['sPattern'] = str_replace('', '*', $pattern) . '*';
$mSearch->setJsonAlert($query_elements);
}
}
osc_add_hook('search_conditions', 'cbx_search_keyword_wildcard', 1);This way you can use wildcards in your keywords but without the need of typing the asterisk *. This means that you can type merced without the need to type merced* to search for listings contain the word mercedes This is more convenience and more user friendly for sure.
Do you want also to be able to search for listings by username ? You can easily do that by replacing the above code with the following one...
function cbx_search_keyword_wildcard($params) {
if (isset($params['sPattern'])) {
$mSearch = Search::newInstance();
$query_elements = (array) json_decode($mSearch->toJson());
$pattern = $query_elements['sPattern'];
$query_elements['sPattern'] = str_replace('', '*', $pattern) . '*';
$mSearch->setJsonAlert($query_elements);
$mSearch->addJoinTable(count($query_elements['tables_join']), DB_TABLE_PREFIX."t_user u", "fk_i_user_id = u.pk_i_id", 'INNER');
$aPattern = explode(' ', $pattern);
$userNameCond = '';
foreach ($aPattern as $word) {
if ($word) $userNameCond .= sprintf(" || u.s_username = '%s'", $word);
}
$mSearch->addConditions("1 = 1 " . $userNameCond);
$mSearch->addGroupBy(DB_TABLE_PREFIX.'t_item.pk_i_id');
}
}
osc_add_hook('search_conditions', 'cbx_search_keyword_wildcard', 1);Now if there a user with the username "John" in your site and type "John" in the search field you will get results with listings that belong only to this user.
Wildcards Doesn't work on my beta theme
Ok guys, i 'm starting here this topic about how we can improve the search functionality one step at a time. If any of you have and want to share your tips and ideas, you are all welcome. So Heads Up and let's start!
SEARCH FOR LISTINGS USING WILDCARDS OR/AND BY USERNAME
Add this function in your themes's function.php file:
function cbx_search_keyword_wildcard($params) {
if (isset($params['sPattern'])) {
$mSearch = Search::newInstance();
$query_elements = (array) json_decode($mSearch->toJson());
$pattern = $query_elements['sPattern'];
$query_elements['sPattern'] = str_replace('', '*', $pattern) . '*';
$mSearch->setJsonAlert($query_elements);
}
}
osc_add_hook('search_conditions', 'cbx_search_keyword_wildcard', 1);This way you can use wildcards in your keywords but without the need of typing the asterisk *. This means that you can type merced without the need to type merced* to search for listings contain the word mercedes This is more convenience and more user friendly for sure.
Do you want also to be able to search for listings by username ? You can easily do that by replacing the above code with the following one...
function cbx_search_keyword_wildcard($params) {
if (isset($params['sPattern'])) {
$mSearch = Search::newInstance();
$query_elements = (array) json_decode($mSearch->toJson());
$pattern = $query_elements['sPattern'];
$query_elements['sPattern'] = str_replace('', '*', $pattern) . '*';
$mSearch->setJsonAlert($query_elements);
$mSearch->addJoinTable(count($query_elements['tables_join']), DB_TABLE_PREFIX."t_user u", "fk_i_user_id = u.pk_i_id", 'INNER');
$aPattern = explode(' ', $pattern);
$userNameCond = '';
foreach ($aPattern as $word) {
if ($word) $userNameCond .= sprintf(" || u.s_username = '%s'", $word);
}
$mSearch->addConditions("1 = 1 " . $userNameCond);
$mSearch->addGroupBy(DB_TABLE_PREFIX.'t_item.pk_i_id');
}
}
osc_add_hook('search_conditions', 'cbx_search_keyword_wildcard', 1);Now if there a user with the username "John" in your site and type "John" in the search field you will get results with listings that belong only to this user.
Hi [member=61]subzero[/member] , thanks for the "partial" current solution for search by username. But exists a problem when username is created with two words, the underscore is used by default to replace space char.
The visitor will search with a space between two words and never with a underscore.
The problem with a space between in two words typed on search field will not get results to specific username with two words with underscore (single word).
example of a username with two words or more:
Digital name (search username with spaces) 0 results!
Digital_name (search username with underscores) 1 found!
Can you update your function to fix this?
Hi again, after 8 days. Seems the forum don't have anymore the same activity. Well, anyway, maybe someone can help a bit to find a solution for replace spaces with underscores to find usernames containing spaces. Typed on search field like: jonh wick... but on database is ever registered with underscore instead of a space, as: jonh_wick.
So... for find username is ever a unique string (no spaces). That's why searching by "jonh wick" not will get results to that username, as example.
Someone?