Searching 2 tables based on keyword, but also pull in image on search results

3 posts by 2 authors in: Forums > CMS Builder
Last Post: November 18, 2015   (RSS)

By zaba - November 12, 2015

I am trying to pull information from 2 tables based on a search keyword, this much I have achieved. But the results also have an associated thumbnail image which I want to also pull in. How can I achieve this. I am guessing it would require modifications to the /cms/lib/viewer_functions.php file but I am stuck as how to achieve this. Heres the working code that pulls the right result (but without any images).

in the products table the image field is called "image_gallery', i need to display the first thumbnail image

in the case_studies table the image field is called "images", again i need to display the first thumbnail image

 <!-- search form-->

<form id="search" action="/search.php" method="get">
<div id="label"><label for="search-terms" id="search-label">search</label></div>
<div id="input"><input type="text" name="q" id="search-terms" placeholder="Enter search terms..." value="<?php echo htmlspecialchars(@$FORM['q']); ?>"></div>
</form>


 <!-- search script-->
<?php 
/*  search tables */  
       $searchOptions = array();
      $searchOptions['keywords'] = @$FORM['q'];
      $searchOptions['perPage']  = "10";
      $searchOptions['debugSql'] = "1";

      $searchTables = array();
      $searchTables['products'] = array(
        'viewerUrl'       => 'products.php',
        'titleField'      => 'title',
        'summaryField'    => 'product_details',
        'searchFields'    =>  array('title','product_details'),
      );

      $searchTables['case_studies'] = array(
        'viewerUrl'       => 'case-study.php',
        'titleField'      => 'title',
        'summaryField'    => 'details',
        'searchFields'    =>  array('client','title','details'),
      );
      list($searchRows, $searchDetails) = searchMultipleTables($searchTables, $searchOptions);
 
/*  limit number of characters from summary field */     
function custom_echo($x){
  if(strlen($x)<=240) { echo $x; } else { $y=substr($x,0,240) . ' ...'; echo $y; }
}

?>

 <!-- search results-->
  <?php foreach ($searchRows as $record): ?>
  <!-- trim code from end of _link and replace with custom code -->
     <h1><a href="<?php $link=$record['_link']; $new_link= rtrim($link, '?1234567890'); echo $new_link; ?>?num=<?php echo $record['num'] ?>"><?php echo $record['_title'] ?></a></h1>
     <?php if ($record['_summary']): ?>
       <p><?php echo custom_echo($record['_summary']); ?></p>
      <?php endif ?>
    <p><a href="<?php $link=$record['_link']; $new_link= rtrim($link, '?1234567890'); echo $new_link; ?>?num=<?php echo $record['num'] ?>"><?php $link=$record['_link']; $new_link= rtrim($link, '?1234567890'); echo $new_link; ?>?num=<?php echo $record['num'] ?></a></p>
      </p>
<?php endforeach ?>

  <!-- paging -->
  <?php if (($searchOptions['keywords'] !== "")OR(!$searchOptions['keywords'] && !$searchDetails['noRecordsFound'])) : ?>
<p>
    <?php if ($searchDetails['prevPage']): ?>
      <a href="<?php echo $searchDetails['prevPageLink'] ?>">&lt;&lt; Previous</a>
    - page <?php echo $searchDetails['page'] ?> of <?php echo $searchDetails['totalPages'] ?> -
    <?php endif ?>
    <?php if ($searchDetails['nextPage']): ?>
      <a href="<?php echo $searchDetails['nextPageLink'] ?>">Next <strong>&gt;&gt;</strong></a>
    <?php endif ?>
</p>
    <?php endif ?>

By Daryl - November 17, 2015 - edited: November 17, 2015

Hi zaba,

It's not recommended to update the lib functions as the changes will get overwritten when we upgrade the CMS.
To get the thumbnail images, you can use getUploads() function:

getUploads($tableName, $fieldName, $recordNum);

For example:

foreach ($searchRows as $record){
  if     ($record['tablename'] == 'products')     { $uploadFieldName = 'image_gallery'; }
  elseif ($record['tablename'] == 'case_studies') { $uploadFieldName = 'images'; }
 
  if     (@$uploadFieldName){
    $uploadRecords = getUploads($record['tablename'], $uploadFieldName, $record['num']);
  }
  $uploadRecord  = @$uploadRecords[0]; // get the first record
}

Let me know if you have any question.

Cheers,

Daryl Maximo
PHP Programmer - interactivetools.com