How to add search feature on my site.

23 posts by 6 authors in: Forums > CMS Builder
Last Post: January 2, 2013   (RSS)

By jacgo - October 25, 2012

Hi,

I would like to add a search feature on my site.

Is there any search code or feature on cmsbuilder that I can add it on my site?

If yes. How to do ?? or any guide for this?

Thanks!!

jacgo

Re: [jacgo] How to add search feature on my site.

By gkornbluth - October 26, 2012

Hi jacgo,

Here's one of the recipes on seaching from my CMSB Cookbook http://www.thecmsbcookbook.com that might help:

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>


Best,

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

Re: [jacgo] How to add search feature on my site.

By gregThomas - October 26, 2012

Hi,

What do you want to the search feature to look for? Are you trying to search a particular section of CMS Builder?

Thanks!
Greg Thomas







PHP Programmer - interactivetools.com

Re: [greg] How to add search feature on my site.

By jacgo - October 26, 2012

Hi Jerry , Greg[/#000000]

Thanks!

I have no idea about a particular section of CMS Builder.

What I am looking for is a site search. The search can search all of my sites information.

That's all waht I need.

I am a web designer. not a programer.

Is it any simple and easy way to do it?

Thanks a lot!

Re: [jacgo] How to add search feature on my site.

By gregThomas - October 29, 2012

If you want to create a seach for the entire site I would use CMS Builder to search any sections that contain page data. CMS Builder does allow you to search multiple sections at once using the function searchMultipleTables.

You will need to have a basic understanding of PHP and CMS Builder to implement this code. You could try something like this:

<?php


// load viewer library
$libraryPath = 'cmsAdmin/lib/viewer_functions.php';
$dirsToCheck = array('W:/g/greg.com/htdocs/','','../','../../','../../../');
foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

$searchOptions = array();
$searchOptions['keywords'] = @$_REQUEST['q'];
$searchOptions['perPage'] = "10";
$searchOptions['debugSql'] = "0";

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

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

list($searchResults, $searchMeta) = searchMultipleTables($searchTables, $searchOptions);

if(!$searchResults){
$error = "I'm sorry, no results have been found. Please try a different search term.";
}
?>


<form method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<input type="text" name="q" value="<?php echo @$_REQUEST['q']; ?>">
<input type="submit" name="submit" value="Search">
</form>

<!-- show errors -->
<?php if ($searchMeta['invalidPageNum']): ?>
Results page '<?php echo $searchMeta['page']?>' not found, <a href="<?php echo $searchMeta['firstPageLink'] ?>">start over &gt;&gt;</a>.<br/>
<?php elseif ($searchOptions['keywords'] && $searchMeta['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 ($searchResults 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 ($searchMeta['prevPage']): ?>
<a href="<?php echo $searchMeta['prevPageLink'] ?>">&lt;&lt; prev</a>
<?php else: ?>
&lt;&lt; prev
<?php endif ?>

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

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


You will need to change the variables I've highlighted in red to match the variables in your tables. You will also need to adapt the $searchTables['quotes'] variable to match another section that is in CMS Builder, you can add as many sections as you would like to be returned in the search results by adding more rows to the searchTables array using the layout that is used in the quotes and news rows.

We can add this feature for you if you would like. If you send an e-mail to consulting@interactivetools.com we can go through what you require in more detail and give you an estimate.

Thanks!
Greg Thomas







PHP Programmer - interactivetools.com

Re: [greg] How to add search feature on my site.

By jacgo - October 29, 2012

hi Greg

Noted.

I will try it.

thanks!

Re: [greg] How to add search feature on my site.

By northernpenguin - November 16, 2012

Greg: How do I search for a fields 'info1' in an upload_files section?
--
northernpenguin
Northern Penguin Technologies

"Any sufficiently advanced technology
is indistinguishable from magic."
........Arthur C. Clarke

Re: [northernpenguin] How to add search feature on my site.

By gregThomas - November 16, 2012

Hi,

There isn't a straightforward way to do this as it's a meta field. But there are a couple of workarounds you can try.

First you could use PHP to filter a getRecords results array based on your search term:

// load records from 'news'
list($newsRecords, $newsMetaData) = getRecords(array(
'tableName' => 'news',
'loadUploads' => true,
'allowSearch' => false,
));

foreach($newsRecords as $story){
foreach($story['page_image'] as $image){
if(strstr('Search Term',$image['info1']) != false){
echo '<img src="'.$image['urlPath'].'" />';
}
}
}


The second option is to do a search on the uploads section directly.

$images = mysql_select('uploads', "info1 LIKE '%Search term%' ");

This will return results for every uploaded CMS Builder image unless you specify a recordNum in your search, and the urlPath will not be dynamic.

Thanks
Greg Thomas







PHP Programmer - interactivetools.com

Re: [greg] How to add search feature on my site.

By paulmac - December 6, 2012

Hi Greg

'viewerUrl' => 'detailsPage.php'

What code goes in to 'detailsPage.php'

Thanks

Paul

Re: [paulmac] How to add search feature on my site.

By gregThomas - December 6, 2012

Hi Paul,

The viewerURL is the page that the $record['_link'] will go to. So if you already have a page set up where a user can view the details of a particular record in a section then you could use that.

You can create a details page for a section using the code generator. You can read more on doing this here:

http://www.interactivetools.com/docs/cmsbuilder/code_generator.html

Below I've created an example of how I might create a search page and details page for a blog section.

Search page code:

<?php

// load viewer library
$libraryPath = 'cmsAdmin/lib/viewer_functions.php';
$dirsToCheck = array('W:/g/greg.com/htdocs/','','../','../../','../../../');
foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

$searchOptions = array();
$searchOptions['keywords'] = @$_REQUEST['q'];
$searchOptions['perPage'] = "10";
$searchOptions['debugSql'] = "0";

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

list($searchResults, $searchMeta) = searchMultipleTables($searchTables, $searchOptions);

if(!$searchResults){
$error = "I'm sorry, no results have been found. Please try a different search term.";
}
?>


<form method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<input type="text" name="q" value="<?php echo @$_REQUEST['q']; ?>">
<input type="submit" name="submit" value="Search">
</form>

<!-- show errors -->
<?php if ($searchMeta['invalidPageNum']): ?>
Results page '<?php echo $searchMeta['page']?>' not found, <a href="<?php echo $searchMeta['firstPageLink'] ?>">start over &gt;&gt;</a>.<br/>
<?php elseif ($searchOptions['keywords'] && $searchMeta['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 ($searchResults 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 ($searchMeta['prevPage']): ?>
<a href="<?php echo $searchMeta['prevPageLink'] ?>">&lt;&lt; prev</a>
<?php else: ?>
&lt;&lt; prev
<?php endif ?>

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

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



This is my blogDetails.php page code I created this using the code generator, and selecting the "Multi record sections: Get record # from end of url. eg: viewer.php?record_title-3" radio button for the which record option.

<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php


// load viewer library
$libraryPath = 'cmsAdmin/lib/viewer_functions.php';
$dirsToCheck = array('C:/wamp/www/','','../','../../','../../../');
foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

// load record from 'blog'
list($blogRecords, $blogMetaData) = getRecords(array(
'tableName' => 'blog',
'where' => whereRecordNumberInUrl(0),
'loadUploads' => true,
'allowSearch' => false,
'limit' => '1',
));
$blogRecord = @$blogRecords[0]; // get first record
if (!$blogRecord) { dieWith404("Record not found!"); } // show error message if no record found

?><!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; }
.instructions { border: 3px solid #000; background-color: #EEE; padding: 10px; text-align: left; margin: 25px}
</style>
</head>
<body>

<!-- INSTRUCTIONS -->
<div class="instructions">
<b>Sample Detail Viewer - Instructions:</b>
<ol>
<?php ?>
<li><b>Remove any fields you don't want displayed.</b></li>
<li>Rearrange remaining fields to suit your needs.</li>
<li>Copy and paste code into previously designed page (or add design to this page).</li>
</ol>
</div>
<!-- /INSTRUCTIONS -->

<!-- STEP2: Display Record (Paste this where you want your record to appear) -->
<h1>Blog - Detail Page Viewer</h1>
Record Number: <?php echo htmlencode($blogRecord['num']) ?><br/>
Title: <?php echo htmlencode($blogRecord['title']) ?><br/>
Date: <?php echo date("D, M jS, Y g:i:s a", strtotime($blogRecord['date'])) ?><br/><!-- For date formatting codes see: http://www.php.net/date -->
Content: <?php echo $blogRecord['content']; ?><br/>
jhgjhg (value): <?php echo $blogRecord['jhgjhgjhgjhg'] ?><br/>
jhgjhg (label): <?php echo $blogRecord['jhgjhgjhgjhg:label'] ?><br/>
_link : <a href="<?php echo $blogRecord['_link'] ?>"><?php echo $blogRecord['_link'] ?></a><br/>

<!-- STEP 2a: Display Uploads for field 'uploads' (Paste this anywhere inside STEP2 to display uploads) -->
<!-- Upload Fields: extension, thumbFilePath, isImage, hasThumbnail, urlPath, width, height, thumbUrlPath, thumbWidth, thumbHeight, info1, info2, info3, info4, info5 -->
Uploads:
<blockquote>
<?php foreach ($blogRecord['uploads'] as $index => $upload): ?>
Upload Url: <?php echo $upload['urlPath'] ?><br/>

<!-- Uploads: Copy the tags from below that you want to use, and erase the ones you don't need.

Thumb Url: <?php echo $upload['thumbUrlPath'] ?><br/><br/>
Download Link: <a href="<?php echo $upload['urlPath'] ?>">Download <?php echo $upload['filename'] ?></a><br/><br/>

Image Tags:<br/>
<img src="<?php echo $upload['urlPath'] ?>" width="<?php echo $upload['width'] ?>" height="<?php echo $upload['height'] ?>" alt="" />
<img src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" alt="" /><br/>

info1 (Title) : <?php echo htmlencode($upload['info1']) ?><br/>
info2 (Caption) : <?php echo htmlencode($upload['info2']) ?><br/><br/>

Extension: <?php echo $upload['extension'] ?><br/>
isImage: <?php if ($upload['isImage']): ?>Yes<?php else: ?>No<?php endif ?><br/>
hasThumbnail: <?php if ($upload['hasThumbnail']): ?>Yes<?php else: ?>No<?php endif ?><br/>
<hr/>
// end uploads comment tag -->

<?php endforeach ?>
</blockquote>
<!-- STEP2a: /Display Uploads -->

<!-- /STEP2: Display Record -->
<hr/>

<a href="<?php echo $blogMetaData['_listPage'] ?>">&lt;&lt; Back to list page</a>
<a href="mailto:?subject=<?php echo urlencode(thisPageUrl()) ?>">Email this Page</a>

</body>
</html>


Let me know if you need any help implementing this code.

Thanks!
Greg Thomas







PHP Programmer - interactivetools.com