ryanC Posted August 21, 2017 Share Posted August 21, 2017 (edited) Hi, I've been working on a modified version of the default search template that comes with the "beginner" site profile. When someone searches a term, I would like to include the term the user searched for along with the results. For example, say someone searched the word "apples". Right now I have: if($matches->count) { // we found matches echo "<h2>Found $matches->count page(s) matching your query:</h2>"; // output navigation for them (see TIP below) echo "<ul class='nav'>"; Is there a way I can have their term echoed back to them along with results? Something like: Quote "Search results for apples" Thanks! Edited August 22, 2017 by ryanC issue was solved Link to comment Share on other sites More sharing options...
adrian Posted August 22, 2017 Share Posted August 22, 2017 $q contains the search term:https://github.com/processwire/processwire/blob/57b297fd1d828961b20ef29782012f75957d6886/site-beginner/templates/search.php#L20 2 Link to comment Share on other sites More sharing options...
ryanC Posted August 22, 2017 Author Share Posted August 22, 2017 Thanks Adrian, how would I enter that into my text? I am not very good at PHP yet. echo "<h2>Found $matches->count page(s) matching your query:</h2>"; echo "selectorValue"; Obviously it's not just "echo selectorValue", but I don't know what to put in there. Link to comment Share on other sites More sharing options...
adrian Posted August 22, 2017 Share Posted August 22, 2017 Just echo $q where you want it: echo "Search results for $q"; 3 Link to comment Share on other sites More sharing options...
kongondo Posted August 22, 2017 Share Posted August 22, 2017 What Adrian said. Just be sure to sanitise the values you echo back directly using htmlentities, for instance, like so: $results = $sanitizer->entities($q); echo "Search results for $results"; 3 Link to comment Share on other sites More sharing options...
szabesz Posted August 22, 2017 Share Posted August 22, 2017 21 minutes ago, kongondo said: Just be sure to sanitise the values you echo back directly using htmlentities +1 If you happen to "echo" it with JavaScript (let's say you cannot use PHP to echo it into the search input box because that one has already been rendered) then you can do something like: <?php if ($q) : ?> <script type="text/javascript"> $('#my-search-query-input').val(htmlentities.decode('<?= $q ?>')); </script> <?php endif; ?> where htmlentities.decode is found in https://github.com/bb87/htmlentities $q must be already sanitized of course as @kongondo pointed out, my JS example is about turning htmlentities back into regular characters which does not happen when injecting $q using JS. 3 Link to comment Share on other sites More sharing options...
ryanC Posted August 22, 2017 Author Share Posted August 22, 2017 Thanks for your help everyone! I have entered: $results = $sanitizer->entities($q); echo "Search results for $results"; in my template and everything is working perfectly. 2 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