johnnydoe Posted April 4, 2016 Posted April 4, 2016 Hi there!I want to implement a "simple" full text search (no ajax needed) into a webpage that's built with processwire. I couldn't find any threads, tutorials or modules helping with this. Can anyone help me with this or point me to a place where I find information how to implement this? Thanks in advance! Jonas
gebeer Posted April 4, 2016 Posted April 4, 2016 Hi, you can find some basic search logic in the search.php of the default site profile: https://github.com/ryancramerdesign/ProcessWire/blob/master/site-default/templates/search.php In https://github.com/ryancramerdesign/ProcessWire/blob/master/site-default/templates/_main.php#L61 you find the code for the search form. This should get you started. EDIT: If you should later decide to use AJAX, there is Soma's module: https://github.com/somatonic/AjaxSearch 4
elabx Posted April 4, 2016 Posted April 4, 2016 Hi johnnydoe, welcome to the forum! I think the easiest approach is to use the Processwire API. Give a look at the Skyscrapper profile template files. Specifically, the search.php template file. Take a look at how the Processwire API is used to check for HTTP variables and sanitize the data received. In the end of the search.php file you will find: $content .= renderSkyscraperList(findSkyscrapers($selector)); ..Which you can take a look here in the functions.php file, and it is the function that actually renders the markup from the PageArray retrieved by the selector queries. It is saved into $content, and rendered in _out.php. You might also take a look at this page of the documentation explaining the $input variable. https://processwire.com/api/variables/input/ 4
johnnydoe Posted April 12, 2016 Author Posted April 12, 2016 Thank you very much for your help! For now I took the search from the default site profile. But I still have 2 problems, for which I couldn't find a solution so far: As selector for the results I have now: "title~=$q" ($q stands for the term I entered). This gives me all the pages that have exactly this word in their title. If I enter "kanzler" I won't get a page with "bundeskanzler" -> this seems to be more important for german - where words are often put together - than for english. but it's a german page. So far I couldn't find a selector that just checks if a title contains a string (I looked at http://cheatsheet.processwire.com). Is there a possibility to achieve this? I would like to search fields like body and some others too for the search term. But if I write "title|body~=$q" as e selector, it returns an error ("Error: Exception: Field does not exist: body"), because not all my templates have a body element, I guess? Is there a possiblity to search fields that are not in all templates? Have a nice week! Jonas
LostKobrakai Posted April 12, 2016 Posted April 12, 2016 If you need partial matching you'd need to use the %= operator. It's using mysql's LIKE to get matches. 3
johnnydoe Posted April 14, 2016 Author Posted April 14, 2016 Thank you, I overlooked this one ^^ My other question (search several fields) is also finished... I turned out that I didn't have a field "body" at all. If it exists, there is no error. 1
uliverse Posted October 24, 2019 Posted October 24, 2019 I am trying to implement the search function from the demo into my website. I run into errors, though. My search.php template starts like this: Quote <?php namespace ProcessWire; // look for a GET variable named 'q' and sanitize it $q = input()->get('q'); // sanitize to text, which removes markup, newlines, too long, etc. $q = sanitizer()->text($q); Already at line 4 it throws an error: Quote Fatal Error: Uncaught Error: Call to undefined function ProcessWire\input()... I am lost... What am I doing wrong? It somehow must have to do with Processwire syntax, but the code is taken directly from the demo... so why wouldn't it work?
Gideon So Posted October 24, 2019 Posted October 24, 2019 Hi @uliverse, It is $input->get($q). https://processwire.com/docs/start/variables/input/ Gideon
uliverse Posted October 24, 2019 Posted October 24, 2019 Ah... thank you. That at least takes care of the errors... not quite there, but closer... Still confused about the PW syntax ?
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