My search results are not working

My search results are not working.

I wrote the code almost exactly like the kirby cookbook example.

The input search box works fine.
And when I do a search, the results page appears.
However, no search results are displayed.

What is the problem? Please help…!
Or is there an example search kit for reference?

http://sciencewalden.org/sciencewalden2/document


search.php

<form>
  <input type="search" name="q" value="<?= html($query) ?>">
  <input type="submit" value="Search">
</form>

<ul>
  <?php foreach ($results as $result): ?>
  <li>
    <a href="<?= $result->url() ?>">
      <?= $result->title() ?>
    </a>
  </li>
  <?php endforeach ?>
</ul>

search.php - controllers

<?php
return function ($site) {

  $query   = get('q');
  $results = $site->search($query, 'title|text');
  $results = $results->paginate(20);

  return [
'query'   => $query,
'results' => $results,
'pagination' => $results->pagination()
  ];

};

Is search.php(the one with the form) a template or a snippet?

Which page actually uses the search.php template? The document page?

When submitting the form, the page url changes, but I wonder why because there is no action attribute in the form element.

Oh, I’m so sorry.

Yes, I want the search box to work on the document page.

So I wrote the code on the document page as below.
Should the action’s page url change?

document.php (templete)

<form action="<?= page('search')->url() ?>" autocomplete="off" class="searchbox" method="get">
  <div class="form-group">
    <input class="search" id="search" name="q" onfocus="this.value=''" placeholder="Search..." type="search" value="<?= html($query) ?>">
  </div>
</form>

Hm, what is in your search.php template? Something is very weird here.

Also, I would expect your document page to throw an undefined variable error, because $query is probably not defined.

Actually I don’t need search.php.
it is a page that is not used and was created after following the kirby example.

How can I define the $query?

If you want to do everything via your document page:

  • Instead of search.php template and controller, put everything into the controller/template for the document page (/site/templates/document.php, /site/controllers/document.php)
  • change the action attribute to <?= $page->url() ?>
1 Like

Thank you, the problem is solved!
But is it impossible to search for structure fields?

Kirby should actully search in structure fields as well, but you can’t add fields inside structure fields as search targets.

Hm, I wonder, is there any way to work around this limit? On my page I need a fine tuned search and that means I need to exclude some columns of a structure field from a search.

No. Not with the built-in search method. You’d have to roll your own if you need more finetuning.

Any ideas how to approach this?

I’m not a PHP pro, so the only thing I can come up with right now is to create some hook that on page save or update grabs all the text from a certain structure field column, and merges it all together in a new hidden field. Are there any easier ways that I am overlooking?

That would probably be the easiest if you don’t want to write your own search function.

Ok, thanks. I think I’ll try the hook to field approach then. :+1: