Jump to content

simple site search without module


stanoliver
 Share

Recommended Posts

Hi! I tried to implement a simple site search like in the regular site profile by Ryan.

http://demo.processwire.com/regular/

I checked out the code on github but I could not make it work for my site. Probably because Ryan uses some advanced output strategy and uikit specific stuff which I don't use or understand.

For my site I just use:

- 1 template (name: templatetoparea.php) for the toparea of my site

- 1 template (name: templatemainare.php) for the main content of my site

- 1 template (name: templatefooterarea.php) for the footerarea of my site.

Also I have prepared two empty templates:

- 1 template (name: templatesearch.php) for the search itself

- 1 template (name: templatelistsearch.php) for listing the search results

May somebody could let me know how to implement a very basic site search like in the regular site profile without using any module or complicated output strategies.

Two things would be a must have:

- The search should work with standard repeaters (where most of my content lives)

- The search results/hits should be listed and the search word(s) should have a yellow colored background (inline css is fine) 

Link to comment
Share on other sites

Hey stanoliver!

First of all, it's difficult to say exactly how to set this up without knowing how your site has been set up etc. but if you're looking for a simple solution, you might want to take a look at the search template in the "classic" site profile: https://github.com/processwire/processwire/blob/master/site-classic/templates/search.php. This is a very bare-bones approach, though, and as such it doesn't handle things like those highlights – but it might be a sensible starting point for your own custom feature. Just modify the fields to search from etc.

I know you said that you'd like to achieve this without a module, but I do still think that SearchEngine would make sense here. It does quite a bit for you out of the box.

Hope this helps a bit.

Link to comment
Share on other sites

18 hours ago, stanoliver said:

For my site I just use:

- 1 template (name: templatetoparea.php) for the toparea of my site

- 1 template (name: templatemainare.php) for the main content of my site

- 1 template (name: templatefooterarea.php) for the footerarea of my site.

Not related to search, but: Are these really three templates? i.e. do you really use them as PW-templates, or just as partials that you include() in the template?

18 hours ago, stanoliver said:

The search should work with standard repeaters (where most of my content lives)

Perhaps this is useful: https://processwire.com/blog/posts/processwire-3.0.91-core-updates/

$pages->find("FieldtypeTextarea|FieldtypeText|FieldtypePageTitle%=coffee");

or this: 

 

18 hours ago, stanoliver said:

- The search results/hits should be listed and the search word(s) should have a yellow colored background (inline css is fine) 

Here's an example with PHP: https://stackoverflow.com/questions/9373023/how-can-i-get-a-summary-string-around-a-search-query-within-a-longer-string

Here's a little JS lib: https://markjs.io/

  • Like 1
Link to comment
Share on other sites

@dragan Your question made sense I am sorry for confusement.

Actually the real templates are like:

- templatehome.php

- templatebasicpage.php

The templates "templatetoparea.php" and "templatefooterarea.php" are included/are partials.

The template "mainarea.pp" made no sense at all. Sorry for confusing you.

Thanks for your help so far I will try out your guys hints.

Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By jacmaes
      I have a series of videos, and the following search form (translated into English here) that allows to filter these videos on the frontend:

      I've built a few of these search forms, but only with text fields, selects and radio buttons. Here I'm using an array with checkboxes ("Level" field above), and it's causing me grief when I try to paginate these results. I've done a lot of searching in the forum and spent too many hours to try to get it to work. Here's how I'm building the selector:
      <?php if(count($input->get)): // Level is an array. Code adapted from Ryan's snippet here: // https://processwire.com/talk/topic/3472-enable-pagination-for-search-results/?tab=comments#comment-38042 if($input->get->level) { $level = array(); foreach($input->get->level as $id) $level[] = (int) $id; // sanitize to INTs $level = implode('|', $level); // convert to 123|456|789 string, ready for selector } else { $level = ''; } $data = array( 'training_type' => array('=', (int) $input->get->training_type), 'duration' => array('=', (int) $input->get->duration), 'level' => array('=', $level), 'limit' => array('=', (int) $input->get->limit) ); $selector = ''; // iterate through the $data we made above to create a selector string foreach($data as $field => $a) { list($operator, $value) = $a; if(empty($value)) continue; // send value to the whitelist so that it can be used in pagination $input->whitelist($field, $value); // append to our selector string $selector .= "$field$operator$value, "; } $videos = $page->children("$selector"); When I hit search, I get the expected results. So far so good. The GET parameters are the following with the options selected in the screenshot above:
      videos/?level[]=1476&level[]=1477&training_type=1473&duration=1485&limit=10 $selector echoes the following as the "level" field is an array with a pipe character:
      level=1476|1477, training_type=1473, duration=1485, limit=10 Now, when paginating these results, the following page (page 2) shows these GET parameters:
      videos/page2/?level=1476|1477&training_type=1473&duration=1486&limit=10 And I think that's where the problem lies. The "level" field is "lost" and I'm getting more results than expected on subsequent pages. If I manually add "page2" to the initial results in the URL, just to test, everything works fine:
      videos/page2/?level[]=1476&level[]=1477&training_type=1473&duration=1486&limit=10 But how can I achieve this in code? Do I need to revert to "level[]=1475&level[]=1477" instead of "level=1476|1477" for the pagination to work correctly, and can you PHP gods illuminate me?
      Any help would be really appreciated, really.
    • By cboetens
      Hi guys
      So I'm building a little search index for a client with this recipe I've found on the ProcessWire Recipes website (more info: https://processwire-recipes.com/recipes/set-up-search-index-with-fieldtypecache/)
      I've followed the exact steps in the recipe and the script appears to be working, except for Profields: Repeater Matrix. 🙄
      This is the code I've got so far:
      <?php include('includes/header.php') ?> <?php $q = $sanitizer->selectorValue($input->get->q); if($q) { $results = $pages->find('search_cache%='. $q); } else { $results = new stdClass(); $results->count = 0; } ?> <section class="partners-block-consul mb-5"> <div class="container"> <div class="row"> <div class="col-sm-10 offset-sm-1"> <div class="partners-head sec-p-lg"> <?php if($page->headline) { ?> <h1 class="highlight"><?= $page->headline ?></h1> <?php } ?> <div class="cms text-left"> <p>Er werden <?= $results->count ?> resultaten gevonden.</p> <?php if($results->count > 0){ ?> <ul> <?php foreach($results as $result){ if($result->template->name == "faq_item") { $category = $pages->get("id=".$result->faq_category); $parent_template = $category->rootParent(); ?> <li><?= $result->title ?> - <a href="<?= $parent_template->url . $category->name . "/"; ?>" title="<?= $result->title ?>"><?= $labels->read_more ?></a></li> <?php } elseif($result->template->name == "faq_category") { $parent_template = $result->rootParent(); $faqCategoryUrl = $parent_template->url . $result->name . "/"; ?> <li><?= $result->title ?> - <a href="<?= $faqCategoryUrl; ?>" title="<?= $result->title ?>"><?= $labels->read_more ?></a></li> <?php } else { ?> <li><?= $result->title ?> - <a href="<?= $result->url; ?>" title="<?= $result->title ?>"><?= $labels->read_more ?></a></li> <?php } ?> <?php } ?> </ul> <?php } ?> </div> </div> </div> </div> </div> </section> <?php include('includes/cta.php'); ?> <?php include('includes/footer.php'); ?> In the ProcessWire admin the "search_cache" field is being build as follows (see image 1).


      Then, the field 'search_cache' is being used on the following templates (see image 2 and 3). So, let's say I'm searching the word 'kinderen', which should get a hit on the template 'Kind', the script isn't returning this page as one of the hits (the result should return 'migraine').
       

      If someone has any idea why the script is not looking for the data that is being put in the field "Profields: Repeater Matrix", he/ she would be my personal hero! 🤗
      Greetings
      Cédric
       


    • By slave
      I would like to create a search form with a category selection.
      I do not get the selector, I need help
      my selector
      $katId = $pages->find("template=leftnav_kat, h1_h6_1%=$kat"); its ok
      $matches = $pages->find("template=site, has_parent=$katId"); its ok
      myresult this pages (templates = site):
      MedLab ML +2 °C bis +8 °C 41 Liter
      MedLab ML +2 °C bis +8 °C 41 Liter
      MedLab ML +2 °C bis +8 °C 45 Liter
      MedLab ML +2 °C bis +8 °C 82 Liter
      MedLab ML +2 °C bis +8 °C 82 Liter
      LabStar Sanguis +2 °C bis +6 °C 100 Liter
      MedLab ML +2 °C bis +8 °C 107 Liter
      LabStar Sanguis +2 °C bis +6 °C 100 Liter
      MedLab ML +2 °C bis +8 °C 82 Liter
      but I'm looking for a text in the template = site, this template has a field search_cache

      $katId = $pages->find("template=leftnav_kat, h1_h6_1%=$kat"); its ok
      $matches = $pages->find("template=site, has_parent=$katId, search_cache%=Industrie"); not work
      I need help
       
       
       

    • By TLT
      Hello, and thanks again for PW. The more I use it, the more I like it.
      I have a silly problem, and since I have tested several (bad) solutions, I come and ask for help. I think I'm just being thick there...
      Is there a relatively simple way to add an allowed character to the default search form's sanitizer ?
      FYI, I'm working on a dictionary in breton language (60k entries), and I must find a way to allow apostrophes in search input. (In breton language, "CH" and "C'H" are actually two different letters altogether).
      I must say I'm a bit confused by the sanitizer. On the regular search form, it appears three times. Once in the input field itself, and twice on the search template.
      I did read the sanitizer page and sanitizer class page, but I'm not sure to understand where to go from there. From what I understand, the
      $sanitizer->name methods would accept some extra allowed character, but I seem to have no luck in adding this to my template. Morevover, do I have to change any sanitizer call to a -> name one ?
      The sanitizer php core page includes an "allowedASCII" which seems very promising, but is there a way to override it ? Hooks ? (not used to them I'm affraid...)
      Morevover, the fact that i'm trying to implement the (') character worries my a bit. Can I escape it ? would something like '\'' do the trick ?
      Thank in advance to you, good men. The site building is reaching an end, and I am tired indeed. (you wouldn't believe the state of the initial DB I had to work from...)
    • By RyanJ
      Need some help with these this filter if anyone can point me in the right direction. I altered the below from the skyscraper profile to meet my needs, but can not seem to get it to work in my case. The actual drop down on the form works properly displaying all locations, but I cant get the output to search what is selected. What am I missing?
      I am trying to grab all the employees when a specific location is selected.
      My structure is
      root
      Location 1                     
        - employee                         
          -- jane doe
          -- jone doe
      Location 2
        -employee
         -- jane smith
      form drop down for child page
      <select name="location">            <option value="">any</option>             <?php          foreach($pages->get("/")->children("id!=10") as $location) {               $selected = $location->title == $input->whitelist->location ? " selected='selected' " : '';               echo "<option$selected value='{$location->id}'>{$location->title}</option>";             } ?> </select> output for child page
      if($input->get->location) { $location = $pages->get("template=location" . $sanitizer->pageName($input->get->location)); if($location->id) {   $selector .= "location={$location}, ";   $input->whitelist('location', $location->name); }}
×
×
  • Create New...