Juergen Posted October 24, 2016 Share Posted October 24, 2016 Hello @ all, I have tried to implement an Ajax search from UIKit into processwire, but I cannot get it to work. Has someone implemented it and can give me a hint. My problem is the target page (page for the search results) which should output the search results in a Json format. I know how to create the Json format with a foreach loop but I dont know how to find all matching pages. I am able to get the search term with this code snippet: $q = $sanitizer->text($_REQUEST['search']) After this I have added the code for the search page recommended by Ryan, but this wont work. Here is the complete code: <?php if ($config->ajax) { require_once('inc/func.inc'); require_once('_strings.php'); include('paginator/Paginator.php'); include('paginator/PagesPaginator.php'); $search_array = array(); //$q = $sanitizer->text($input->post->q); $q = $sanitizer->text($_REQUEST['search']); // did $q have anything in it? if ($q) { $input->whitelist('q', $q); $q = $sanitizer->selectorValue($q); $templateselector = "template!='profile', template!='activation', template!='privacy-policy-item', template!='serviceitem', template!='single-event', template!=productpricelistitem', template!='partner'"; if ($user->isLoggedin()) $templateselector .= ", has_parent!=2"; $paginator = new PagesPaginator(); $results = $paginator(array( "headline|title~=$q, $templateselector", "introtext~=$q, $templateselector", "body~=$q, $templateselector", "summary~=$q, $templateselector", "firmennameslogan|firmenwortlaut|street|postalcode|place|region|country|geschaeftsfuehrung|vertretung|registergericht|inhaltverantwortung|fachgruppe|subgroup|geldinstitut|ecgbehoerde~=$q, $templateselector", "position~=$q, $templateselector", "privacytable.title|privacytable.body~=$q, $templateselector", "partnertable.title~=$q, $templateselector", "downloadrepeater.downloadfiledesc|downloadrepeater.downloadfieldtitle~=$q, $templateselector" ), $input->pageNum, 25); foreach ($results as $result) { $title = $result->title; $summary = $result->summary; $link = $result->url; $search_array[] = array( 'title' => $title, 'summary' => $summary, 'url' => $link // ID is the url segment ); } } header('Content-Type: application/json'); $searchresults_json = json_encode($search_array, true); echo '{ "results": ['; echo $searchresults_json; echo ']}'; } Maybe the input variable ($input) doesnt work in this case. Best regards Link to comment Share on other sites More sharing options...
Juergen Posted October 24, 2016 Author Share Posted October 24, 2016 Ok I have figured it out - the code works but the Json Array made problems (json_encode doesnt work correctly in this case). So here is the complete code as inspiration for all who want to use UIKit Ajax search function in processwire. <?php if ($config->ajax) { require_once('inc/func.inc'); require_once('_strings.php'); include('paginator/Paginator.php'); include('paginator/PagesPaginator.php'); $numberofitems = 3;//How many items should be displayed $search_array = array(); $q = $sanitizer->text($_REQUEST['search']); // did $q have anything in it? if ($q) { $input->whitelist('q', $q); $q = $sanitizer->selectorValue($q); $templateselector = "template!='profile', template!='activation', template!='privacy-policy-item', template!='serviceitem', template!='single-event', template!=productpricelistitem', template!='partner'"; if ($user->isLoggedin()) $templateselector .= ", has_parent!=2"; $paginator = new PagesPaginator(); $searchresults = $paginator(array( "headline|title~=$q, $templateselector", "introtext~=$q, $templateselector", "body~=$q, $templateselector", "summary~=$q, $templateselector", "firmennameslogan|firmenwortlaut|street|postalcode|place|region|country|geschaeftsfuehrung|vertretung|registergericht|inhaltverantwortung|fachgruppe|subgroup|geldinstitut|ecgbehoerde~=$q, $templateselector", "position~=$q, $templateselector", "privacytable.title|privacytable.body~=$q, $templateselector", "partnertable.title~=$q, $templateselector", "downloadrepeater.downloadfiledesc|downloadrepeater.downloadfieldtitle~=$q, $templateselector" ), $input->pageNum, $numberofitems);//show only 3 items $resultjson = ""; foreach ($searchresults as $key => $result) { $title = $result->title; $summary = $result->summary; $link = $result->url; if($key == $numberofitems - 1){//last item $resultjson .= '{"title":"'.$title.'", "url":"'.$link.'", "text":"'.$summary.'"}'; } else { $resultjson .= '{"title":"'.$title.'", "url":"'.$link.'", "text":"'.$summary.'"},'; } } } header('Content-Type: application/json'); echo '{ "results": ['; echo $resultjson; echo ']}'; } else { //put here the standard search code for non Ajax } 1 Link to comment Share on other sites More sharing options...
kongondo Posted October 24, 2016 Share Posted October 24, 2016 Is there any reason you are using $_REQUEST['search'] instead of $input->post->search (or $input->get as the case might be)...? Link to comment Share on other sites More sharing options...
kongondo Posted October 24, 2016 Share Posted October 24, 2016 Also...json_encode(array, true) is not valid...That function, does not take such a second parameter http://php.net/manual/en/function.json-encode.php Link to comment Share on other sites More sharing options...
Juergen Posted October 24, 2016 Author Share Posted October 24, 2016 No, but at the first trials it doesnt work, but it works too after all the changes. Link to comment Share on other sites More sharing options...
Juergen Posted October 25, 2016 Author Share Posted October 25, 2016 23 hours ago, kongondo said: Also...json_encode(array, true) is not valid...That function, does not take such a second parameter I use this syntax in another json array for events which I have copied from another entry and in this case it works. I get all my events in json. I have tried json_encode($searchresult_array); too but it doesnt work in this case. Maybe the json array will be created but UIKit doesnt fetch it. I have to figure out if the syntax of the json array is not in the right syntax for UIKit in this case. Link to comment Share on other sites More sharing options...
kongondo Posted October 25, 2016 Share Posted October 25, 2016 Your true is probably being converted to an integer 1. The second parameter of json_encode is $options and it expects an integer representing a Bitmask of JSON constants. 3 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now