Quick search: search for two items at the same time

The recommended method of customizing your AppGini-generated application is through hooks. But sometimes you might need to add functionality not accessible through hooks. You can discuss this here.
Post Reply
facos79
Veteran Member
Posts: 115
Joined: 2014-10-29 12:31

Quick search: search for two items at the same time

Post by facos79 » 2023-10-05 09:40

Good morning everyone, does anyone know if it is possible to search for two entries in one or more columns at the same time using quick search?
In the attached photo for example I have a table where fruit sales are stored. Each row contains the product (one fruit). I would like to search through the quick search, in the entire table, for two fruits at the same time (for example the KIWI BOERICA and KIWI SORELI). For example, if I write 'BOERICA SORELI' in the search, the search does not return any results.
I would like to use the quick search and not the detailed search
Attachments
appgini-ricerca-veloce.JPG
appgini-ricerca-veloce.JPG (117.25 KiB) Viewed 4407 times

fdissait
Posts: 23
Joined: 2019-07-24 07:57

Re: Quick search: search for two items at the same time

Post by fdissait » 2023-10-05 16:41

Hello
Try searching : KIWI%o
You should have the 2 kiwi
% IS for any letter(s)

facos79
Veteran Member
Posts: 115
Joined: 2014-10-29 12:31

Re: Quick search: search for two items at the same time

Post by facos79 » 2023-10-05 18:59

Thank you. In this case it works because they are both KIWIs but what if they were different fruits and I wanted to search for example KIWIs and APPLES? (in the table in question the apples are not present but they could be there and the user might want to search for 2 or three different products at the same time.
Can you use the quick search or do you have to use the advanced search?
Thanks for your help

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1638
Joined: 2018-04-01 10:12

Re: Quick search: search for two items at the same time

Post by pbottcher » 2023-10-07 13:01

Hi,

the actual answer is no, you cannot do it without adding some code. AppGini uses by default a wildcard search on the string you enter.
So if you say that you use the "space" as a delimiter for your search, you might try this:

in the hooks/TABLENAME.php -> _init function

Code: Select all

if (isset($_REQUEST['SearchString'])) {
	$delimiter=' ';  // change this if you want to use a different delimiter
	$searchstring=$_REQUEST['SearchString'];
	$QuerySearchableFields=array();
			
	foreach($options->QueryFieldsQS as $fName => $fCaption)
		if(stripos($fName, '<img') === false)
			$QuerySearchableFields[$fName] = $fCaption;

	$sssarray = explode($delimiter,makeSafe($searchstring)); // safe search stringarray

	$options->QueryWhere = $options->QueryWhere ? $options->QueryWhere : " WHERE 1=1 ";
	$s=array();
	foreach($sssarray as $idx => $sss) {
		if(count($QuerySearchableFields))
			$s[]=implode(
					" LIKE '%{$sss}%' OR ", 
					array_keys($QuerySearchableFields)
				) . " LIKE '%{$sss}%'"; 
	}
	$options->QueryWhere.='AND ('.implode(' OR ',$s).' )';
	$_REQUEST['NewSearch']=$searchstring;
	$_REQUEST['SearchString']='';
}
In the _header function you need to add to the tableview case:

Code: Select all

if (isset($_REQUEST['NewSearch'])) {
	$search=$_REQUEST['NewSearch'];
	$header='<%%HEADER%%><script>$j(function() {$j("#SearchString").val("'.$search.'");$j(".table-responsive td:not([colspan])").mark("' . html_attr($$search) . '", { className: "text-bold bg-warning", diacritics: false, separateWordSearch: 1 });})</script>';
}
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

facos79
Veteran Member
Posts: 115
Joined: 2014-10-29 12:31

Re: Quick search: search for two items at the same time

Post by facos79 » 2023-10-10 18:34

Thanks for your help. I'll try to follow your advice this week. Thank you

facos79
Veteran Member
Posts: 115
Joined: 2014-10-29 12:31

Re: Quick search: search for two items at the same time

Post by facos79 » 2023-10-15 13:49

error:

Fatal error: Uncaught Error: Attempt to assign property "QueryWhere" on null in /data/vhosts/........../hooks/Movimenti_acquisto_peso.php:117 Stack trace: #0 /data/vhosts/......../Movimenti_acquisto_peso_view.php(259): Movimenti_acquisto_peso_header() #1 {main} thrown in /data/vhosts/........./hooks/Movimenti_acquisto_peso.php on line 117

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1638
Joined: 2018-04-01 10:12

Re: Quick search: search for two items at the same time

Post by pbottcher » 2023-10-15 15:24

Can you post your code here?
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

facos79
Veteran Member
Posts: 115
Joined: 2014-10-29 12:31

Re: Quick search: search for two items at the same time

Post by facos79 » 2023-10-15 15:37

what code do you need? I entered the code you posted. If you need more info I can send what you need. Thank you

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1638
Joined: 2018-04-01 10:12

Re: Quick search: search for two items at the same time

Post by pbottcher » 2023-10-15 15:56

Can you show the _init function with the code and the _header -> tableview case as mentioned above.
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

facos79
Veteran Member
Posts: 115
Joined: 2014-10-29 12:31

Re: Quick search: search for two items at the same time

Post by facos79 » 2023-11-26 13:54

Thanks, I tried again. The filter works but just add the code in the hook file of the corresponding table:

Code: Select all

if (isset($_REQUEST['SearchString'])) {
	$delimiter=' ';  // change this if you want to use a different delimiter
	$searchstring=$_REQUEST['SearchString'];
	$QuerySearchableFields=array();
			
	foreach($options->QueryFieldsQS as $fName => $fCaption)
		if(stripos($fName, '<img') === false)
			$QuerySearchableFields[$fName] = $fCaption;

	$sssarray = explode($delimiter,makeSafe($searchstring)); // safe search stringarray

	$options->QueryWhere = $options->QueryWhere ? $options->QueryWhere : " WHERE 1=1 ";
	$s=array();
	foreach($sssarray as $idx => $sss) {
		if(count($QuerySearchableFields))
			$s[]=implode(
					" LIKE '%{$sss}%' OR ", 
					array_keys($QuerySearchableFields)
				) . " LIKE '%{$sss}%'"; 
	}
	$options->QueryWhere.='AND ('.implode(' OR ',$s).' )';
	$_REQUEST['NewSearch']=$searchstring;
	$_REQUEST['SearchString']='';
}
You don't need to add the other part of code.

Post Reply