kkalgidim Posted August 31, 2019 Share Posted August 31, 2019 Hi, i am using multi language system i have ajax search system (live search) When i enter the word into search box it find the related topics but also show the other langugae titles. How can i made it for current language? Here is the code that i use Quote // Check if ajax request if($config->ajax) { $searchresults = $pages->find("has_parent!=2"); // Find all published pages and save as $results header("Content-type: application/json"); // Set header to JSON echo $searchresults->toJSON(); // Output the results as JSON via the toJSON function } else { result attached as image Link to comment Share on other sites More sharing options...
kkalgidim Posted September 2, 2019 Author Share Posted September 2, 2019 any solution? Link to comment Share on other sites More sharing options...
bernhard Posted September 2, 2019 Share Posted September 2, 2019 On 8/31/2019 at 3:02 PM, kkalgidim said: $searchresults->toJSON(); What does this method do? Can you share the code or explain where it comes from? Link to comment Share on other sites More sharing options...
kkalgidim Posted September 2, 2019 Author Share Posted September 2, 2019 @bernhard I used the module below. I dont really know about json files. Just what i want is live search system. https://modules.processwire.com/modules/pages2-json/ What does this do? Simply adds method toJSON() to Page and PageArray elements, is capable to converting image and file arrays to URL containers, travels recursively all objects before outputs actual JSON. Link to comment Share on other sites More sharing options...
3fingers Posted September 3, 2019 Share Posted September 3, 2019 Does this works? if($config->ajax) { $language = $languages->getLanguage(); $searchresults = $pages->find("has_parent!=2, $language"); header("Content-type: application/json"); // Set header to JSON echo $searchresults->toJSON(); // Output the results as JSON via the toJSON function } else { ... Link to comment Share on other sites More sharing options...
kkalgidim Posted September 3, 2019 Author Share Posted September 3, 2019 2 hours ago, 3fingers said: Does this works? if($config->ajax) { $language = $languages->getLanguage(); $searchresults = $pages->find("has_parent!=2, $language"); header("Content-type: application/json"); // Set header to JSON echo $searchresults->toJSON(); // Output the results as JSON via the toJSON function } else { ... no ? i also tried Quote $currentLanguage = $user->language; $searchresults = $pages->find("has_parent!=2, $currentlanguage"); but its not working. i think find function does not accept variable parameter. maybe if i know the value of the laguage param it can be something like that Quote $searchresults = $pages->find("has_parent!=2, langparam={$currentlanguage}"); Link to comment Share on other sites More sharing options...
3fingers Posted September 3, 2019 Share Posted September 3, 2019 Worth a try: if($config->ajax) { $searchresults = $pages->find("has_parent!=2, title.data{$user->language}"); // See below for the forum link about *.data header("Content-type: application/json"); // Set header to JSON echo $searchresults->toJSON(); // Output the results as JSON via the toJSON function } else { ... More on that here Link to comment Share on other sites More sharing options...
kkalgidim Posted September 3, 2019 Author Share Posted September 3, 2019 i tried it but its not working. thanks for your reply @3fingers Link to comment Share on other sites More sharing options...
Mats Posted September 3, 2019 Share Posted September 3, 2019 Try sending the current language with the Ajax request. And set the language on the response. (On mobile). 1 Link to comment Share on other sites More sharing options...
kkalgidim Posted September 5, 2019 Author Share Posted September 5, 2019 On 9/3/2019 at 11:22 PM, Mats said: Try sending the current language with the Ajax request. And set the language on the response. (On mobile). @Mats can you give an example for it. i think that there must be a simple way to filter page array to current language but i coundnt figure it out Link to comment Share on other sites More sharing options...
kkalgidim Posted October 24, 2019 Author Share Posted October 24, 2019 Hi, i found solution and i would like to post it here. Maybe it helps someone. i removed echo $searchresults->toJSON(); i used the code below instead of toJSON function $myPages = $pages->find('template=clinic|doctor|treatment|location|resource'); $data =$myPages->explode(['title', 'url']); echo wireEncodeJSON($data); now it shows the current language results You have to set the $.typeahead({ cache: false, if you dont set cache : false it still show the cached language result even you change the language. 1 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