Selectable Search Option

November 11, 2007, 02:26:46 PM Posted by Thantos on November 11, 2007, 02:26:46 PM in Selectable Search Option | 5 Comments
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.


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

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


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) : ''));
}

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.


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);
?>

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:
<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>

This is what we'll be changing.

First change the action of the form from action="', $scripturl, '?action=search2" to action="URL/search.php" (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:

$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,
),
);


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

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

And change it to:

<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;" />


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

Comments


brianjw on November 11, 2007, 07:54:27 PM said
Thank you so much! :) I will be adding this to my website when it gets back online. :)

Frestorm on November 14, 2007, 01:22:46 PM said
thank you :)

shadow82x on November 15, 2007, 09:21:27 PM said
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 on March 31, 2009, 12:55:08 AM said
I am having the same problem and i cant find any error message in the error log.

Angelina Belle on September 04, 2012, 03:05:22 PM said
Now that our Onine Manual has moved, it would be a good idea to point to the new manual -- not the old.
If memory serves, this could be achieved with something along the lines of:
elseif ($_REQUEST['search_type'] == 'docs')
redirectexit('http://wiki.simplemachines.org/smf/index.php?title=Special%3ASearch&go=GO&search=' . urlencode(stripslashes($_REQUEST['query'])));
Advertisement: