Multiple Search Form

11 posts by 5 authors in: Forums > CMS Builder
Last Post: October 12, 2010   (RSS)

By (Deleted User) - September 8, 2009

Hi,

All my CMS Builder sites until now need no search FORMS. The site was so organized that each section had the search built into the URL to filter out the results.

I now need to create a search form to search more than one field. Or as some call it, an “Advance Search”. I already created one form to do a simple search. It searches the name field only and it works very good.

Can somebody post some basic code for a search form that will search a few fields? Right now the fields I need to search for are as follows:
  • name
  • city
  • state
  • country

Re: [RapidWeb] Multiple Search Form

By Kenny - September 8, 2009

Try this:

<?php echo "<?xml version='1.0'?>\n"; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<style type="text/css">
body { font-family: arial; font-size: 12px; }
</style>
</head>
<body>


<!-- STEP1: Load Record List (Paste this above other steps) -->
<?php
require_once "../lib/viewer_functions.php";
$searchOptions = array();
$searchOptions['keywords'] = @$FORM['q'];
$searchOptions['perPage'] = "10";
$searchOptions['debugSql'] = "0";

$searchTables = array();
$searchTables['news'] = array(
'viewerUrl' => 'newsPage.php',
'titleField' => 'title',
'summaryField' => 'summary',
'searchFields' => array('title','summary','content'),
);

$searchTables['quotes'] = array(
'viewerUrl' => 'quotesPage.php',
'titleField' => 'name',
'summaryField' => 'quote',
'searchFields' => array('name','pullQuote','quote'),
);

list($searchRows, $searchDetails) = searchMultipleTables($searchTables, $searchOptions);
?>
<!-- /STEP1: Load Record List -->

<h1>Search Viewer</h1>

<form method="GET" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
<input type="text" name="q" value="<?php echo htmlspecialchars(@$FORM['q']); ?>" size="50">
<input type="submit" name="" value="Search"><br/><br/>

<table border=0 cellpadding=2 cellspacing=0 width=100%>
<tr>
<td bgcolor="#EEEEEE"><font size=2 class="maintext">Search results for <b><?php echo htmlspecialchars(@$FORM['q']); ?></b></font></td>
<td bgcolor="#EEEEEE" align="right"><font size=2 class="maintext">Results
<b><?php echo htmlspecialchars(@$searchDetails['pageResultsStart']); ?></b> to
<b><?php echo htmlspecialchars(@$searchDetails['pageResultsEnd']); ?></b> of
<b><?php echo htmlspecialchars(@$searchDetails['totalRecords']); ?></b>.</font></td>
</tr>
</table><br/>

<!-- show errors -->
<?php if ($searchDetails['invalidPageNum']): ?>
Results page '<?php echo $searchDetails['page']?>' not found, <a href="<?php echo $searchDetails['firstPageLink'] ?>">start over &gt;&gt;</a>.<br/>
<?php elseif ($searchOptions['keywords'] && $searchDetails['noRecordsFound']): ?>
No records matched search query!<br/><br/>
<?php elseif ($searchOptions['keywords'] == ""): ?>
Enter a keyword to search.<br/><br/>
<?php endif ?>

<!-- STEP2: Display Record List -->
<?php foreach ($searchRows as $record): ?>
<a href="<?php echo $record['_link'] ?>"><?php echo $record['_title'] ?></a><br/>
<?php if ($record['_summary']): ?>
<?php echo $record['_summary'] ?><br/>
<?php else: ?>
No description available for page.<br/>
<?php endif ?>
<a href="<?php echo $record['_link'] ?>" style="color: #008000"><?php echo $record['_link'] ?></a><br/><br/>
<?php endforeach ?>
<!-- /STEP2: Display Record List -->




<!-- STEP3: Display Page Links (Paste anywhere below "Load Record List") -->
<?php if ($searchDetails['prevPage']): ?>
<a href="<?php echo $searchDetails['prevPageLink'] ?>">&lt;&lt; prev</a>
<?php else: ?>
&lt;&lt; prev
<?php endif ?>

- page <?php echo $searchDetails['page'] ?> of <?php echo $searchDetails['totalPages'] ?> -

<?php if ($searchDetails['nextPage']): ?>
<a href="<?php echo $searchDetails['nextPageLink'] ?>">next &gt;&gt;</a>
<?php else: ?>
next &gt;&gt;
<?php endif ?>
<!-- /STEP3: Display Page Links -->

</form>
</body>
</html>



Let us know if that is what you are looking for.

Kenny

Re: [RapidWeb] Multiple Search Form

By Chris - September 8, 2009

Hi RapidWeb,

Do you need to search multiple fields or multiple tables/sections?

If you only need to search multiple fields, here's a simple solution for you. Replace the STEP 1 code from a List page viewer with the following:

require_once "C:/wamp/www/cmsbuilder_1_34_build1/cmsAdmin/lib/viewer_functions.php";

$fieldsToSearch = array(
'name',
'city',
'state',
'country',

);

// if the visitor supplied a search string, construct a where clause to search all the fields we want to search
$search_string = @$_REQUEST['q'];
if ($search_string) {
$searchClauses = array();
foreach ($fieldsToSearch as $field) {
array_push($searchClauses, "$field LIKE '%" . mysql_escape($search_string) . "%'");
}
$where = join(' OR ', $searchClauses);
}
// if the visitor did not supply a search string, return all records
else {
$where = '1';
}


list($records,) = getRecords(array(
'tableName' => 'listings',
'where' => $where,
));


replacing the code in red with your own information.

I hope this helps. Please let us know if you have any questions or comments.
All the best,
Chris

Re: [chris] Multiple Search Form

By (Deleted User) - September 8, 2009

[font "Times New Roman"]Thanks for the reply. Not sure if I understand what I need to do.

[font "Times New Roman"]

[font "Times New Roman"]I have a search box inside a form on a regular HTML page. It is the simple search as described in your documentation here:

[url "http://www.interactivetools.com/docs/cmsbuilder/viewer_search.html"][font "Times New Roman"]http://www.interactivetools.com/docs/cmsbuilder/viewer_search.html[/#800080][/url]

Custom Search Forms

You can create search forms by naming the fields after what you want them to search. Here is a simple search form that searches the "title" field for a keyword.

<form method="POST" action="/path/to/your/listViewer.php">

<input type="text" name="title_keyword" value="">

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

</form>

[font "Times New Roman"]

[font "Times New Roman"]The test form is on my page here:

[url "http://www.koshertravelinfo.com/minyan/search_minyan.html"][font "Times New Roman"]http://www.koshertravelinfo.com/minyan/search_minyan.html[/#800080][/url]

[font "Times New Roman"]It searches a field called “minyan_name”.

[font "Times New Roman"]

[font "Times New Roman"]Here is what I am trying to do. I want the site visitor to be able to search more than one field. The form should have more than one text entry box. Like our old advance search form we used with “Listing Manager”. Sample here:

[url "http://www.koshertravelinfo.com/davendb/exec/search.cgi"][font "Times New Roman"]http://www.koshertravelinfo.com/davendb/exec/search.cgi[/#800080][/url]

[font "Times New Roman"]

Re: [RapidWeb] Multiple Search Form

By Chris - September 8, 2009

Ahh, it seems we both misunderstood you. You won't need to change minyan.php at all, just add some more fields to the form on your search_minyan.html page. You already have this one:

<input type="text" name="minyan_name_keyword" value="">

just add some more...

Minyan Name: <input type="text" name="minyan_name_keyword" /><br />
City: <input type="text" name="city_keyword" /><br />
State: <input type="text" name="state_keyword" /><br />
Country: <input type="text" name="country_keyword" /><br />
Content: <input type="text" name="content_keyword" /><br />


You can add as many as you want, just make sure to use your own field names (shown in red.)

Please let us know if you have any more questions. :)
All the best,
Chris

Re: [chris] Multiple Search Form

By Codee - October 11, 2010

so does this code go on the page with a search form or on the list page viewer? Does this page understand any form code posted to it?

Re: [equinox69] Multiple Search Form

By Chris - October 12, 2010

Hi equinox69,

The code in my last post would go in a search form on any page. The search form looks at all the form fields posted to it and, if they match up with field names, will use them to filter search results.

That is, unless you set disable 'allowSearch' in your getRecords() options.

Does that help? Please let me know if you have any questions.
All the best,
Chris

Re: [chris] Multiple Search Form

By Codee - October 12, 2010

Hi Chris,

I think I'm referring to the post before the last one with regards to this code:

require_once "C:/wamp/www/cmsbuilder_1_34_build1/cmsAdmin/lib/viewer_functions.php"; $fieldsToSearch = array( 'name', 'city', 'state', 'country', ); // if the visitor supplied a search string, construct a where clause to search all the fields we want to search $search_string = @$_REQUEST['q']; if ($search_string) { $searchClauses = array(); foreach ($fieldsToSearch as $field) { array_push($searchClauses, "$field LIKE '%" . mysql_escape($search_string) . "%'"); } $where = join(' OR ', $searchClauses); } // if the visitor did not supply a search string, return all records else { $where = '1'; } list($records,) = getRecords(array( 'tableName' => 'listings', 'where' => $where, ));

I guess what I would like to see with this post is the following, assuming something like the search form page is named "search.php" and the viewer list page is named "searchListing.php":

1. what code goes on the top of each page
2. what code goes in the body of each page.
So, the above code...I have no clue as to which page it would go into, other than the top of one of them.

Re: [equinox69] Multiple Search Form

By Jason - October 12, 2010

Hi,

That code performs the search, so it would go at the top of the file that is going to display the search results.

Hope this helps.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/