How to add search feature on my site.

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

Hi Greg

There are 3 STEPs on your search code.

I need to searh whole site information.   Do I need to post all 3 steps code on each page?

It seems a long code for search feature.

Is there any simple code that for search feature for a whole site?

Thanks!

-Jax H.

Hi JH,

Take a look at Google site search. It might help.

There's a free (advertising) and a paid version (no ads)

http://www.google.com/cse/

There are also a number of stand alone PHP based search scripts that you can use, some free, some not free. Do a Google search for "PHP site search scripts".

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

Hi Jerry

I hope I can build a site search with cmsBuilder.

Thanks a lot!

Jac

-Jax H.

Hi JH,

Which three steps are you referring too? Is it these three:

http://www.interactivetools.com/forum/forum-posts.php?postNum=2218303#post2218303

That code only needs to appear on the page that displays the search results. If you want to add a search box to any other page that links to your search results page, you could add this form:

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

You will need to change the name of nameOfResultsPage.php to the name of your search results page.

Thanks

Greg

Greg Thomas







PHP Programmer - interactivetools.com

Hi Gerg

Happy new year!   Thanks so much for your reply!

Yes. I followed your code and changed the URL : nameOfResultsPage.php.

In the searchResult.php page.     Only the "Title" field is correct.

I would like to display images, title, brief, intro on my searchResults.php page.  

Above all are my products section fileds.

They are all incorrect.

Most of important.  when clieck the result item.  got wrong page.

I tried to study "http://www.interactivetools.com/forum/forum-posts.php?Search-Field-72696&page=2"

Here is my code:

<?php

// load viewer library

$libraryPath = 'cmsAdmin/lib/viewer_functions.php';
$dirsToCheck = array('/home/quanmax/public_html/','','../','../../','../../../');

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['products'] = array(
'viewerUrl' => 'products-detail.php',
'titleField' => 'title',
'summaryField' => 'brief',
'searchFields' => array('title','brief','intro'),
);

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

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

list($productsRecords, $productsMetaData) = getRecords(array(
'tableName' => 'products',
'where' => whereRecordNumberInUrl(1),
'orderBy' => mysql_escape(@$_REQUEST['orderBy']),
'limit' => '10',
));

$productsRecord = @$productsRecords[0]; // get first record
?>

Form code

<form method="post" action="searchResults.php">
<input type="text" name="q" value="<?php echo @$_REQUEST['q']; ?>" size="15" />
<input class="btn" type="submit" name="submit" value="Search" />
</form>

Result code

<?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 ?>
<?php foreach ($searchResults as $record): ?>
<a href="<?php echo $record['_link'] ?>"> <?php echo $record['_title'] ?> </a> Record Number: <?php echo $record['num'] ?>
<br />
--------------------------------------------------------------
<br />
<?php foreach ($productsRecord['images'] as $index => $upload): ?>
<?php if ($upload['isImage']): ?>
<img src="<?php echo $upload['thumbUrlPath'] ?>" alt="" width="150" height="150" border="0" />
<?php endif ?>
<?php endforeach ?>
<br />
--------------------------------------------------------------
<br />
<?php if ($record['_summary']): ?>
<?php echo $record['_summary'] ?><br/>
<?php else: ?>
No description available for page.<br/>
<?php endif ?>
<br/>
<?php endforeach ?>
<?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 ?>

Here is my page : http://174.132.79.189/~quanmax/en/searchResults.php

Hope you can help.

Thank you a lot!

Jac

-Jax H.

By gregThomas - December 27, 2012 - edited: December 27, 2012

Hi Jac,

Looking at your current code your only searching the products section, do you want to search other sections as well? Just wanted to check before I suggest anything. 

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

Hi Gerg

yes only product section need to search.not whole site.

Thanks a lot!

jac

-Jax H.

Hi Jac,

You can read how to use CMS Builders search funtionality here:

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

If you need to search one section only, you can reduce your current code a fair bit, but you will have to modify your search box slightly. 

Search box html:

<form method="POST" action="nameOfSearchResultsFile.php">
  <input type="text" name="title_keyword" value="">
  <input type="submit" name="submit" value="Search">
</form> 

So any text entered into the input "title_keyword" will look be searched for in the title field. 

Your results page code will need to look something like this:

==================================================

<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php
/* STEP 1: LOAD RECORDS - Copy this PHP code block near the TOP of your page */

// 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."); }

list($productsRecords, $productsMetaData) = getRecords(array(
'tableName' => 'products',
'perPage' => '10',
'loadUploads' => true,
'allowSearch' => true,
));

?><!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 List Viewer - Instructions:</b>
<ol>
<?php /*><li style="color: red; font-weight: bold">Rename this file to have a .php extension!</li><x */ ?>
<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 Records (Paste this where you want your records to be listed) -->
<h1>Blog - List Page Viewer</h1>
<?php foreach ($productsRecords as $record): ?>
Record Number: <?php echo htmlencode($record['num']) ?><br/>
Title: <?php echo htmlencode($record['title']) ?><br/>
Date: <?php echo date("D, M jS, Y g:i:s a", strtotime($record['date'])) ?><br/><!-- For date formatting codes see: http://www.php.net/date -->
Content: <?php echo $record['content']; ?><br/>
jhgjhg (value): <?php echo $record['jhgjhgjhgjhg'] ?><br/>
jhgjhg (label): <?php echo $record['jhgjhgjhgjhg:label'] ?><br/>
_link : <a href="<?php echo $record['_link'] ?>"><?php echo $record['_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 ($record['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 -->

<hr/>
<?php endforeach ?>

<?php if (!$productsRecords): ?>
No records were found!<br/><br/>
<?php endif ?>
<!-- /STEP2: Display Records -->

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

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

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

=======================================

I generated this code using the CMS Builder code generator. I had the "Filter results based on search form input (disable for multiple viewers on one page)" box checked. 

Then you need to modify section two of the above to include the fields that you wish to display. 

Thanks

Greg

Greg Thomas







PHP Programmer - interactivetools.com

Hi Gerg

Thank you a lot for your reply.

Yes. The code is quite simple.

but I still can not display image and link correctly.

Here is my code:

Head 

<?php


// load viewer library

$libraryPath = 'cmsAdmin/lib/viewer_functions.php';
$dirsToCheck = array('/home/quanmax/public_html/','','../','../../','../../../');

foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

list($productsRecords, $productsMetaData) = getRecords(array(
'tableName' => 'products',
'perPage' => '10',
'loadUploads' => true,
'allowSearch' => true,
));

?>

Search field :


<form method="POST" action="search.php">
<input type="text" name="title_keyword" value="">
<input type="submit" name="submit" value="Search">
</form>

Search Result php page:

<?php foreach ($productsRecords as $record): ?>
Record Number: <?php echo htmlencode($record['num']) ?><br/>
Title: <?php echo htmlencode($record['title']) ?><br/>
intro: <?php echo $record['intro']; ?><br/>

<!--Try product image code 1-->
<?php foreach ($productsRecord['images'] as $index => $upload): ?>
<img src="<?php echo $upload['thumbUrlPath'] ?>" alt="<?php echo htmlencode($productsRecord['title']) ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" hspace="20" /><br/>

<?php endforeach ?>
<!--Try product image code 1 end--> 

<!--Try product image code 2-->


<?php foreach ($productsRecord['images'] as $index => $upload): ?>

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

<img src="<?php echo $upload['thumbUrlPath2'] ?>" width="<?php echo $upload['thumbWidth2'] ?>" height="<?php echo $upload['thumbHeight2'] ?>" alt="" /> <br/>

<?php endforeach ?>
<?php endforeach ?>

_link : <a href="<?php echo $productsRecord['_link'] ?>"><?php echo $productsRecord['_link'] ?></a><br/>

I tried 2 different image code.  I generated product page code using the CMS Builder code generator.

It seems simple way to do it.

but Image and Link code still in error.

Here is my page for testing:

http://174.132.79.189/~quanmax/en/search.php 

Yes I have study CmsAdmin search guide.   but dont' know how to use it.

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

My face got blue.

Please kindly help.

Thanks!

Jac 

-Jax H.