How do I create a Search Field?

4 posts by 3 authors in: Forums > CMS Builder
Last Post: January 21, 2010   (RSS)

By invisions - January 20, 2010

Can someone please tell me how I add a search field to my site that will search the database and list the results.

After searching, it would be nice to have the results listed like the following example:

http://litteraria2.ff.cuni.cz/?searchword=book&searchphrase=any&limit=&ordering=newest&view=search&Itemid=99999999&option=com_search
Daniel Pegues
invisionshosting.com

Re: [Donna] How do I create a Search Field?

By invisions - January 21, 2010

Hello Dona.
How do I go about making the search field in my template? I looked at the information page you sent me, but there are no examples.

Do you have a document with examples on how to do various things with CMS Builder? I work best by using examples to work from.

Thanks
Daniel Pegues
invisionshosting.com

Re: [invisions] How do I create a Search Field?

By gkornbluth - January 21, 2010

Hi Invisions,

Welcome to the CMSB Family. If you’ve been coding in HTML up to now, making the switch to PHP and to any CMS can feel a bit overwhelming.

Understanding how to implement even basic searches is one of those areas that has confused many of us.

Here’s an excerpt from my CMSB Cookbook http://www.thecmsbcookbook.com that demonstrates some different ways that you can set up a simple search on your page.

SETTING UP BASIC SEARCHES
Here’s some basic code that you can use to set up a simple search box on the list page of a multi-record editor to return only those records that match your search criteria.
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<input type="text" name="your_field_name_your-criteria" value="">
<input type="submit" name="submit" value="Search">
</form>

Notice the “name” entry on the sample form above. It starts with the field in which you want to preform the search (your_field_name) and is followed by a modifier that determines what type of search is to be performed (_your-criteria).

CMSB is set up to allow you to search by many criteria by changing ( _your-criteria) in the “name” entry to one of these:
_match - an exact match
_keyword - will look for specific words
_prefix - starts with keyword (or letter)
_query - allows google-style query searches such as: +dog -cat "multi word phrase". Only records matching EVERY word or quoted phrase are returned. Words or phrases that start with - mean "must not match". The + is optional and not required.
_min - A minimum value for numeric searches
_max - A maximum value for numeric searches
_year year number for date searches
_month - month number for date searches
_day - Day of month for date searches

So, let’s say you’ve set up a field called “fruit” that can contain one or more keywords, like Apple, Banana, Pear, Orange.

Here’s the basic code that you would use to search for one of those.
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<input type="text" name="fruit_keyword" value="">
<input type="submit" name="submit" value="Search">
</form>

If the visitor entered Apple, Banana, Pear, or Orange, only those records that contained the keyword would be listed.

You could also change the form to include a drop down menu of choices instead of the text box like this:
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>">

<select name="last_name_keyword">
<option value="">Please Choose a Fruit</option>
<option value="Apple">Apple</option>
<option value="Banana">Banana</option>
<option value="Pear">Pear</option>
<option value="Orange">Orange</option>
</select>

<input type="submit" name="submit" value="Search">
</form>

Hope that helps.

Jerry Kornbluth
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php