Simple Machines Blogs > Developers' Blog

Selectable Search Option

(1/2) > >>

Thantos:
For those of you that use the default site theme you have no doubt noticed that we have a selectable search near the top of the page.  This topic will hopefully explain how to create a similar one for yourself.

The first step is to do some preparation work and figure out which search options you want, where they should go, and to figure out what URL parameters you need for each.

Our opinions are as follows:
'This topic': Appears only when you are in a topic.  Uses the SMF search.
'This board':  Appears only when in a board.  Since topics are in boards then being in a topic is being in a board.  Uses the SMF search.
'Entire Site':  Uses Google with a restriction to our site.
'Community':  Searches the community forum using the SMF search.
'Modifications':  Uses the mod site search.
'Themes':  Uses the theme site search
'Online Manual':  Uses the SMF search at the Online Manual forum.

Now that we have the opinions specified we need to figure out what the parameters need to be.
For SMF searches it is: ?action=search2;search=SEARCH STRING;other parameters
For the mod and theme site: ?action=search;basic_search=SEARCH STRING
For Google: /search?q=SEARCH STRING&domains=YOUR DOMAIN&sitesearch=YOUR SITE

Ok now that we have the prep work out of the way we can start coding things up.  We need to create a separate script page that will receive the information from the form.  This script will direct the search to its proper location.


--- Code: ---<?php
require_once('PATH TO FORUM/SSI.php');

--- End code ---
Using SSI will give us access to some functions and other information.


--- Code: ---if (in_array($_REQUEST['search_type'], array('community', 'board', 'topic')))
{
$params = array();
if (!empty($_REQUEST['brd']) && $_REQUEST['search_type'] == 'board')
{
if (!is_array($_REQUEST['brd']))
$_REQUEST['brd'] = explode(',', $_REQUEST['brd']);

foreach($_REQUEST['brd'] AS $b)
{
if (empty($b))
continue;

$params[] = 'brd[]='.urlencode($b);
}
}

if (!empty($_REQUEST['topic']) && $_REQUEST['search_type'] == 'topic')
$params[] = 'topic=' . (int)$_REQUEST['topic'];

redirectexit('action=search2;search=' . urlencode(stripslashes($_REQUEST['query'])) . (!empty($params) ? ';' . implode(';',$params) : ''));
}

--- End code ---
The community, board, and topic types all point back to this forum.  What this block does is to check for other parameters, like the board and topic.  However it also makes sure to add them only if the search type matches.


--- Code: ---elseif ($_REQUEST['search_type'] == 'docs')
redirectexit('http://docs.simplemachines.org/?action=search2;search=' . urlencode(stripslashes($_REQUEST['query'])));
elseif ($_REQUEST['search_type'] == 'mods')
redirectexit('http://custom.simplemachines.org/mods/index.php?action=search;basic_search=' . urlencode(stripslashes($_REQUEST['query'])));
elseif ($_REQUEST['search_type'] == 'themes')
redirectexit('http://custom.simplemachines.org/themes/index.php?action=search;basic_search=' . urlencode(stripslashes($_REQUEST['query'])));
else
redirectexit('http://www.google.com/search?q=' . urlencode(stripslashes($_REQUEST['query'])) . '&domains=simplemachines.org&sitesearch=simplemachines.org', false);
?>
--- End code ---
Here we go through the rest of the possible sites and if it matches then it sends the request off.  We allow it to fall through to the Google search engine that way if they specified an invalid site the search will still work.


Now we just need to upload the file somewhere were we can get to it.  Here we call it search.php and put it in the root of the web directories.

Now we need to modify index.template.php and change the form around.  After opening the file you should look for:

--- Code: --- <form action="', $scripturl, '?action=search2" method="post" accept-charset="', $context['character_set'], '" style="margin: 0;">
<a href="', $scripturl, '?action=search;advanced"><img src="'.$settings['images_url'].'/filter.gif" align="middle" style="margin: 0 1ex;" alt="" /></a>
<input type="text" name="search" value="" style="width: 190px;" />&nbsp;
<input type="submit" name="submit" value="', $txt[182], '" style="width: 11ex;" />
<input type="hidden" name="advanced" value="0" />';

// Search within current topic?
if (!empty($context['current_topic']))
echo '
<input type="hidden" name="topic" value="', $context['current_topic'], '" />';

// If we're on a certain board, limit it to this board ;).
if (!empty($context['current_board']))
echo '
<input type="hidden" name="brd[', $context['current_board'], ']" value="', $context['current_board'], '" />';

echo '
</form>
--- End code ---
This is what we'll be changing.

First change the action of the form from
--- Code: ---action="', $scripturl, '?action=search2"
--- End code ---
to
--- Code: ---action="URL/search.php"
--- End code ---
(where URL is the actual URL).

We now need to add the ability to select where to search.  A select box works wonders for that.  Since you have at least two options that appear only under certain conditions it may be easier to follow what we did:

--- Code: --- $search_locations = array(
array(
'name' => 'This topic',
'value' => 'topic',
'selected' => !empty($context['current_topic']),
'hide' => empty($context['current_topic']),
),
array(
'name' => 'This board',
'value' => 'board',
'selected' => !empty($context['current_board']) && empty($context['current_topic']),
'hide' => empty($context['current_board']),
),
array (
'name' => $searchboxtxt['entire'],
'value' => 'entire',
'selected' => false,
),
array (
'name' => $searchboxtxt['community'],
'value' => 'community',
'selected' => empty($context['outside_forum']) && empty($context['current_board']) && empty($context['current_topic']),
),
array (
'name' => $searchboxtxt['mods'],
'value' => 'mods',
'selected' => !empty($context['mod_site']),
),
array(
'name' => $searchboxtxt['themes'],
'value' => 'themes',
'selected' => !empty($context['theme_site']),
),
array(
'name' => $searchboxtxt['docs'],
'value' => 'docs',
'selected' => false,
),
);

--- End code ---

This allows us to easily add more options later and it makes the code for adding the select box a lot cleaner.

Now find

--- Code: --- <input type="text" name="search" value="" style="width: 190px;" />&nbsp;
<input type="submit" name="submit" value="', $txt[182], '" style="width: 11ex;" />

--- End code ---
And change it to:

--- Code: --- <input type="text" name="query" value="" style="width: 190px;" />&nbsp;
<select name="search_type">';

foreach($search_locations AS $loc)
{
if (!empty($loc['hide']))
continue;

echo '
<option value="', $loc['value'], '"', !empty($loc['selected']) ? 'selected="selected"' : '', '>', $loc['name'], '</option>';
}

echo '
</select>
<input type="submit" name="submit" value="', $txt[182], '" style="width: 11ex;" />

--- End code ---

And there you have it.  A nice selectable search option.

brianjw:
Thank you so much! :) I will be adding this to my website when it gets back online. :)

Frestorm:
thank you :)

mcblaber:
I am having some problems with this. the selectable part with the dropdown that says "this topic" "this board" "entire site" is blank after i upload both scripts to my ftp after editing. can you help me with this?

Stevie777:
I am having the same problem and i cant find any error message in the error log.

Navigation

[0] Message Index

[#] Next page

Go to full version