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

Hi Jac,

I think the problem is this line:

 <!--Try product image code 1-->
<?php foreach ($record['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-->  

The images are stored under each record in the section as opposed to in the section array, so the variable you need is $record and not $productRecord

If this doesn't work, could you attach your search.php file to a post so I can see which lines are causing problems?

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com