Search box in website

Permalink 3 users found helpful
Hello
How can I enable a site-wide search box on my website? Just like here we have a search box to search the forums...

 
ijessup replied on at Permalink Reply
ijessup
c5 comes with a Search block. There are parameters you can set, when you add a Search block, that will tell it where to search.

To have a persistent search block on all pages, you can do this a couple different ways. Without hard coding, you can save a Search block to your scrapbook, then copy that saved block to your Page defaults.

Hard coding is a more difficult approach if you don't understand PHP.
jordanlev replied on at Permalink Best Answer Reply
jordanlev
The way I do sitewide search is to create a "search results" page (which I usually give the name "Search Results" and the path "search"). On this page, place a Search block. In the block, I set the title to "Search Results", and set the button text to "Search Again" (this will make sense when you see it on the page). After you've added that block, exit edit mode and publish changes.

Now, I place this code in my theme template (usually in the elements/header.php file, but may be different for you depending on the theme you're using):
<form method="get" action="<?php echo $this->url('/search') ?>">
   <input type="text" name="query" />
   <input type="submit" value="Search" />
</form>

Those are the bare minimum of things you need in the search form in order for it to work, but if you want to style it via CSS you will need to add classes to the input fields (to differentiate between the text and submit fields), and probably a class or id on the form itself. Often I'll use an image button for the submit instead of the submit element, which is fine -- the important thing is that the textbox is named "query" and that the form method is "get" and the form action goes to the "search results" page you set up above.

Hope that helps!

-Jordan
lordrt replied on at Permalink Reply
thanks for the help guys
msglueck replied on at Permalink Reply
msglueck
if you don't want to add a search block (usually you have to change the HTML anyways via custom template) you can also take the doSearch-Method from the search-Controller and paste it into your single page.

<div class="content">
            <?php 
               if( !empty($_REQUEST['query']) || isset($_REQUEST['akID']))  {       
                  $q = $_REQUEST['query'];
                  $_q = preg_replace('/[^A-ZÄÜÖa-zäöüß\']/i', '', $_REQUEST['query']);
                  Loader::library('database_indexed_search');
                  $ipl = new IndexedPageList();
                  $aksearch = false;
                  if (is_array($_REQUEST['akID'])) {
                     Loader::model('attribute/categories/collection');
                     foreach($_REQUEST['akID'] as $akID => $req) {
                        $fak = CollectionAttributeKey::getByID($akID);
                        if (is_object($fak)) {
                           $type = $fak->getAttributeType();
                           $cnt = $type->getController();
6glcorp replied on at Permalink Reply
6glcorp
Hi, does any of your guys know what the variable $_REQUEST['akID'] is for? i'm not getting any results in the website i'm working on and i noticed that $_REQUEST['akID'] has a NULL value.
It's the first time i have a problem with the search block.
I've already tried to set up a custom area for search in the /jobs/index_search.php, update the settings in Dashboard > Sitemap > Page Search > Setup Index and running manually the Index Search Engine job.
Any suggestions?

Thanks!
6glcorp replied on at Permalink Reply
6glcorp
well i finally found what's going on...
in the controller.php of the search block, $aksearch is always NULL, so far, but the last part in this code, always evaluates to false. This because I selected the option "post results to another page" and the variable
... || $this->resultsURL != ''

forces the function to return false.

if (is_array($_REQUEST['akID'])) {
 Loader::model('attribute/categories/collection');
 foreach($_REQUEST['akID'] as $akID => $req) {
  $fak = CollectionAttributeKey::getByID($akID);
  if (is_object($fak)) {
   $type = $fak->getAttributeType();
   $cnt = $type->getController();
   $cnt->setAttributeKey($fak);
   $cnt->searchForm($ipl);
   $aksearch = true;
  }
 }
}
// STRANGE CONDITION
/*


Anyway, i temporary commented those lines until i figure out what to do with them. Got my search working now

If someone could explain me what's the logic on that piece of code i'll be very thankful
jordanlev replied on at Permalink Reply
jordanlev
I don't really know whether the logic of the code is correct or whatnot, but I believe the "akID" stuff is to facilitate searching by tags (e.g. for the built-in blog) -- if you look at the tag_cloud custom template it utilizes this feature.