Jump to content

Recommended Posts

Posted (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 by ryanC
issue was solved
Posted

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.

Posted

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";

 

  • Like 3
Posted
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.

  • Like 3
Posted

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.

  • Like 2

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...