site search

6 posts by 3 authors in: Forums > CMS Builder: Plugins & Add-ons
Last Post: May 24, 2013   (RSS)

By meg - May 21, 2013

is there an easy way to set up a site search on your website referencing the CMS / database?

By meg - May 23, 2013

So I'm trying to wrap my head around creating this search page. Do you have to create a search record in the CMS? I'm trying to follow this example (http://www.interactivetools.com/forum/forum-posts.php?postNum=2230147#post2230147), but it doesn't have an example for a multi-listing page. 

I'm trying to search just one particular multi-listing section on the site:

list($articlesRecords, $articlesMetaData) = getRecords(array(
'tableName' => 'articles',
'perPage' => '1',
'loadUploads' => true,
'allowSearch' => true,
));
$articlesRecord = @$articlesRecords[0];

Here's the search page I'm working on. If someone could point me in the right direction that would be awesome!!

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

   $libraryPath = 'cms/lib/viewer_functions.php';
  $dirsToCheck = array('/home/content/85/8004985/html/themarrowmagazine/','','../','../../','../../../');
  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 'search'
  list($searchRecords, $searchMetaData) = getRecords(array(
    'tableName'   => 'search',
    'where'       => '', // load first record
    'loadUploads' => true,
    'allowSearch' => false,
    'limit'       => '1',
  ));
  $searchRecord = @$searchRecords[0]; // get first record
  if (!$searchRecord) { dieWith404("Record not found!"); } // show error message if no record found
?>

<?php
    // search viewer code begins
      $searchOptions = array();
      $searchOptions['keywords'] = @$FORM['q'];
      $searchOptions['perPage']  = "3";
      $searchOptions['debugSql'] = "0";

      $searchTables = array();   
      
      // articles as categories
      $searchTables['articles'] = array(
        'viewerUrl'       => 'articles.php',
        'titleField'      => 'title',
        'summaryField'    => 'content',
        'searchFields'    =>  array('title','content_column_1', 'content_column_2'),
      );

      list($searchRows, $searchDetails) = searchMultipleTables($searchTables, $searchOptions);
      // search viewer code ends
?>

<?php 
/////////////////// MAX Words /////////////////////
function maxWords($textOrHtml, $maxWords) {  
   
  $text  = strip_tags($textOrHtml, "<b></b><i></i>");  
  $words = preg_split("/\s+/", $text, $maxWords+1); 
   
  if (count($words) > $maxWords) { unset($words[$maxWords]); }  
  $output = join(' ', $words);  
   
  return $output;  

?>

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<title>CMS Builder Search Engine</title>
<meta name="description" content="Search our website for anything related to page titles, names or page content." />

</head>

<body>
<div id="wrapper">
    <div class="content-container">

<h1><?php echo htmlspecialchars($searchRecord['title']); ?></h1>
    <?php echo $searchRecord['content']; ?>

<!-- search form -->
  <form method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
  <table width="0" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td valign="top"><input name="q" type="text" class="text-field" value="<?php echo htmlspecialchars(@$FORM['q']); ?>" size="40" /></td>
    <td valign="top"><input name="input" type="submit" class="form-button" value="Search" /></td>
    <td valign="top"><input name="reset" type="button" class="form-button" onclick="parent.location='search.php'" value="reset" /></td>
  </tr>
</table>
    
    <h3> Search results for ( <?php echo htmlspecialchars(@$FORM['q']); ?> ) <br />
Results <b><?php echo htmlspecialchars(@$searchDetails['pageResultsStart']); ?></b> to <b><?php echo htmlspecialchars(@$searchDetails['pageResultsEnd']); ?></b> of <b><?php echo htmlspecialchars(@$searchDetails['totalRecords']); ?></b>.</h3><br />

     <!-- show errors -->
      <?php if ($searchDetails['invalidPageNum']): ?>
     <p> Results page '<?php echo $searchDetails['page']?>' not found, <a href="<?php echo $searchDetails['firstPageLink'] ?>">start over &gt;&gt;</a>.</p>
     <!-- /show errors -->
    
    <!-- no results -->  
    <?php elseif ($searchOptions['keywords'] && $searchDetails['noRecordsFound']): ?>
    <h5><strong>No records matched search query!</strong></h5>
    <?php elseif ($searchOptions['keywords'] == ""): ?>
    <p>Type a word into the text field to begin your search.</p>
    <?php endif ?>
    <!-- /no results -->
    
    <!-- display record list -->
    <?php foreach ($searchRows as $record): ?>
    <h3><a href="<?php echo $record['_link'] ?>"><?php echo htmlspecialchars($record['_title']); ?></a></h3>
    
    <?php if ($record['_summary']): ?>
    <p><?php echo maxWords($record['_summary'], 60); ?>...</p>
    <?php else: ?>
    <p>No description available.</p>
    <?php endif ?>
    <h4><a href="<?php echo $record['_link'] ?>">Read more</a></h4>
    
    <hr/><br />
    <?php endforeach ?>
    <!-- /display record list -->
    
  </form>
  <!-- /search form -->


<!-- previous next -->
<h4><strong><?php if ($searchDetails['prevPage']): ?>
      <a href="<?php echo $searchDetails['prevPageLink'] ?>">Previous</a>
    <?php else: ?>
    <?php endif ?> 

<?php if ($searchDetails['nextPage']): ?>
      <a href="<?php echo $searchDetails['nextPageLink'] ?>">Next</a>
    <?php else: ?>
    <?php endif ?>
    
<?php  
    if (@!$_GET['page']): $current_page = "1"; 
    else: $current_page = $_GET['page'];    
    endif;  ?> 

<?php foreach (range(1,$searchDetails['totalPages']) as $page): ?>  
  <?php if ($page == $current_page): ?> 
     <strong><?php echo $page; ?></strong> 
<?php else: ?> 
     <a href=""><?php echo $page; ?></a> 
<?php endif ?> 
<?php endforeach; ?>
</strong></h4>
<!-- /previous next -->

 <!-- /end all search features -->
 
    <!-- /content-wrapper --></div>
<!-- /wrapper --></div>

</body>
</html>

By gregThomas - May 24, 2013

Hi Meg,

You shouldn't have to create any extra fields in the CMS. The code that you've got looks like it should work, are you getting any particular errors or issues when you use the page?

Thanks

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By meg - May 24, 2013

Here is the error I'm getting:

Warning: Cannot modify header information - headers already sent by (output started at /home/content/85/8004985/html/themarrowmagazine/search.php:1) in/home/content/85/8004985/html/themarrowmagazine/search.php on line 1
getRecords(search): Couldn't load schema for 'search'!

By gregThomas - May 24, 2013

Hi,

Thanks, I think I've got solutions to both of the issues, here is an updated script:

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

  $libraryPath = 'cms/lib/viewer_functions.php';
  $dirsToCheck = array('/home/content/85/8004985/html/themarrowmagazine/','','../','../../','../../../');
  foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
  if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }


  // search viewer code begins
  $searchOptions = array();
  $searchOptions['keywords'] = @$FORM['q'];
  $searchOptions['perPage']  = "3";
  $searchOptions['debugSql'] = "0";

  $searchTables = array();   
  
  // articles as categories
  $searchTables['articles'] = array(
    'viewerUrl'       => 'articles.php',
    'titleField'      => 'title',
    'summaryField'    => 'content',
    'searchFields'    =>  array('title','content_column_1', 'content_column_2'),
  );

  list($searchRows, $searchDetails) = searchMultipleTables($searchTables, $searchOptions);
  // search viewer code ends

  /////////////////// MAX Words /////////////////////
  function maxWords($textOrHtml, $maxWords) {  
     
    $text  = strip_tags($textOrHtml, "<b></b><i></i>");  
    $words = preg_split("/\s+/", $text, $maxWords+1); 
     
    if (count($words) > $maxWords) { unset($words[$maxWords]); }  
    $output = join(' ', $words);  
     
    return $output;  
  } 
?>

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<title>CMS Builder Search Engine</title>
<meta name="description" content="Search our website for anything related to page titles, names or page content." />

</head>

<body>
<div id="wrapper">
    <div class="content-container">

<h1>Search Page</h1>
  This is the search page!

<!-- search form -->
  <form method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
  <table width="0" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td valign="top"><input name="q" type="text" class="text-field" value="<?php echo htmlspecialchars(@$FORM['q']); ?>" size="40" /></td>
    <td valign="top"><input name="input" type="submit" class="form-button" value="Search" /></td>
    <td valign="top"><input name="reset" type="button" class="form-button" onclick="parent.location='search.php'" value="reset" /></td>
  </tr>
</table>
    
    <h3> Search results for ( <?php echo htmlspecialchars(@$FORM['q']); ?> ) <br />
Results <b><?php echo htmlspecialchars(@$searchDetails['pageResultsStart']); ?></b> to <b><?php echo htmlspecialchars(@$searchDetails['pageResultsEnd']); ?></b> of <b><?php echo htmlspecialchars(@$searchDetails['totalRecords']); ?></b>.</h3><br />

     <!-- show errors -->
      <?php if ($searchDetails['invalidPageNum']): ?>
     <p> Results page '<?php echo $searchDetails['page']?>' not found, <a href="<?php echo $searchDetails['firstPageLink'] ?>">start over &gt;&gt;</a>.</p>
     <!-- /show errors -->
    
    <!-- no results -->  
    <?php elseif ($searchOptions['keywords'] && $searchDetails['noRecordsFound']): ?>
    <h5><strong>No records matched search query!</strong></h5>
    <?php elseif ($searchOptions['keywords'] == ""): ?>
    <p>Type a word into the text field to begin your search.</p>
    <?php endif ?>
    <!-- /no results -->
    
    <!-- display record list -->
    <?php foreach ($searchRows as $record): ?>
    <h3><a href="<?php echo $record['_link'] ?>"><?php echo htmlspecialchars($record['_title']); ?></a></h3>
    
    <?php if ($record['_summary']): ?>
    <p><?php echo maxWords($record['_summary'], 60); ?>...</p>
    <?php else: ?>
    <p>No description available.</p>
    <?php endif ?>
    <h4><a href="<?php echo $record['_link'] ?>">Read more</a></h4>
    
    <hr/><br />
    <?php endforeach ?>
    <!-- /display record list -->
    
  </form>
  <!-- /search form -->


<!-- previous next -->
<h4><strong><?php if ($searchDetails['prevPage']): ?>
      <a href="<?php echo $searchDetails['prevPageLink'] ?>">Previous</a>
    <?php else: ?>
    <?php endif ?> 

<?php if ($searchDetails['nextPage']): ?>
      <a href="<?php echo $searchDetails['nextPageLink'] ?>">Next</a>
    <?php else: ?>
    <?php endif ?>
    
<?php  
    if (@!$_GET['page']): $current_page = "1"; 
    else: $current_page = $_GET['page'];    
    endif;  ?> 

<?php foreach (range(1,$searchDetails['totalPages']) as $page): ?>  
  <?php if ($page == $current_page): ?> 
     <strong><?php echo $page; ?></strong> 
<?php else: ?> 
     <a href=""><?php echo $page; ?></a> 
<?php endif ?> 
<?php endforeach; ?>
</strong></h4>
<!-- /previous next -->

 <!-- /end all search features -->
 
    <!-- /content-wrapper --></div>
<!-- /wrapper --></div>

</body>
</html>

Cannot modify header information - headers already sent by (output started at /home/content/85/8004985/html/themarrowmagazine/search.php:1) in/home/content/85/8004985/html/themarrowmagazine/search.php on line 1

This error was being caused as you had a space at the very beginning of the script, for the header function to work it needs to run before anything is added to the page. If you remove the space the script should stop displaying this warning.

getRecords(search): Couldn't load schema for 'search'!

This error was being caused as the script you had based this on had a single record section called 'search' that was being used to display the page title and some content. I've removed getRecords section function from this script and any reference to it in the code.

Cheers!

Greg

Greg Thomas







PHP Programmer - interactivetools.com