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...
How can I enable a site-wide search box on my website? Just like here we have a search box to search the forums...
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):
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
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
thanks for the help guys
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();
Viewing 15 lines of 89 lines. View entire code block.
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!
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!
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
forces the function to return false.
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
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 /*
Viewing 15 lines of 16 lines. View entire code block.
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
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.
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.