Search form

3 posts by 2 authors in: Forums > CMS Builder
Last Post: April 9, 2013   (RSS)

By Mikey - April 9, 2013

Hey Jesus,

Below is a stripped down version of my search form, that should get you started in the right direction. This form assumes that you have created the following Editor pages within the CMS, with the following menu names, table names and resulting pages ending in .php:

Single Record:
Menu Name: Search
Table name: search
Site Page: search.php

Single Record:
Menu Name: Home Page
Table name: home_page
Site Page: index.php

Single Record:
Menu Name: About Us
Table name: about_us
Site Page: about_us.php

Advanced Menus > Category Menu:
Menu Name: Services
Table name: services
Site Page: services.php

Save the code below as your "search.php" site page.

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

  // load viewer library
  $libraryPath = 'cmsBuilder/lib/viewer_functions.php';
  $dirsToCheck = array('/home/content/00/00000000/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."); }
  
  // 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();   
      
      // Home Page as single
      $searchTables['home_page'] = array(
        'viewerUrl'       => 'index.php',
        'titleField'      => 'title',
        'summaryField'    => 'content',
        'searchFields'    =>  array('title','content'),
      );
      // services as categories
      $searchTables['services'] = array(
        'viewerUrl'       => 'services.php',
        'titleField'      => 'name',
        'summaryField'    => 'content',
        'searchFields'    =>  array('name','content'),
      );
      // about_us as single
      $searchTables['about_us'] = array(
        'viewerUrl'       => 'about_us.php',
        'titleField'      => 'title',
        'summaryField'    => 'content',
        'searchFields'    =>  array('title','content'),
      );

      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 http_build_query(array_merge($_REQUEST, array( 'page' => $page ))) ?>"><?php echo $page; ?></a> 
<?php endif ?> 
<?php endforeach; ?>
</strong></h4>
<!-- /previous next -->

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

</body>
</html>

And here's a bit of code you can use on other site pages that will query the "search.php" site page and produce search results for your website.

<!-- search field for other site pages that queries the search.php site page -->
    <form method="get" action="http://yourwebsite.com/search.php?q=" >
    <table border="0" cellpadding="0" cellspacing="0">
     <tr>
    <td><input type="text" name="q" value="<?php echo htmlspecialchars(@$FORM['q']); ?>" class="text-field"/></td>
    <td><input type="image" src="images/magnify-glass.png" name="submit" value="Search" title="Search" onclick="submit" class="magnify-glass"/></td>
    </tr>
    </table>
    </form>
<!-- /search field for other site pages that queries the search.php site page -->

Hope this helps,

Zick

By Jesus - April 9, 2013

Thank you Zick, will give it a try!

Jesus