Search labels

5 posts by 3 authors in: Forums > CMS Builder
Last Post: February 3, 2014   (RSS)

By samsam - January 28, 2014

Hi,

I know this topic has been already answered a while back but maybe someone figured out a solution or alternative for this problem.

We need to search Products by keywords. The keyword input field would search through basic textfields, textboxes but also list fields with data from another table.

We don't want to have a dropdown select field made of those external data lists, it really needs to be part of the same and unique keyword box.

Here is how the product table is made of:

<?php foreach ($productsRecords as $record): ?>
Record Number: <?php echo htmlencode($record['num']) ?><br/>
Stock #: <?php echo htmlencode($record['title']) ?><br/>
Gem Type (value): <?php echo $record['gem_type'] ?><br/>
Gem Type (label): <?php echo $record['gem_type:label'] ?><br/>
Featured? (value): <?php echo $record['featured'] ?><br/>
Featured? (text): <?php echo $record['featured:text'] ?><br/>
Weight (Carats): <?php echo htmlencode($record['weight_carats']) ?><br/>
# of pieces: <?php echo htmlencode($record['of_pieces']) ?><br/>
Gem Variety: <?php echo htmlencode($record['gem_variety']) ?><br/>
Shape/ Cut (value): <?php echo $record['shape_cut'] ?><br/>
Shape/ Cut (label): <?php echo $record['shape_cut:label'] ?><br/>
Measurements: <?php echo htmlencode($record['measurements']) ?><br/>
Origin: <?php echo htmlencode($record['origin']) ?><br/>
Description: <?php echo htmlencode($record['content']) ?><br/>
<?php endforeach ?>

Basically we need to search the title, gem_type:label, gem_variety, shape_cut:label and content fields.

If you have a solution, that would be greatly appreciated.

Thank you

By Daryl - February 3, 2014

Hi Samsam,

What I would do is I will build a where clause for all the fields and related list fields because I don't think that using the allowSearch => true will include the searching of list fields from another table.

Here's an example:

  $where = '';
  if (@$_REQUEST['searchKeyword']){
    $where               .= 'title LIKE "%'.mysql_escape($_REQUEST['searchKeyword']).'%" OR content LIKE "%'.mysql_escape($_REQUEST['searchKeyword']).'%"';
    
    $loadGemTypeRecords   = mysql_select('gem_type', 'title LIKE "%'.mysql_escape($_REQUEST['searchKeyword']).'%"');
    $loadShapeCutRecords  = mysql_select('shape_cut', 'title LIKE "%'.mysql_escape($_REQUEST['searchKeyword']).'%"');
    
    if (@$loadGemTypeRecords){
      $gemTypeRecordNums = array_pluck($loadGemTypeRecords, 'num');
      $where .= ' OR gem_type IN ('.mysql_escapeCSV($gemTypeRecordNums).')';
    }
    
    if (@$loadShapeCutRecords){
      $shapeCutRecordNums = array_pluck($loadShapeCutRecords, 'num');
      $where .= ' OR shape_cut IN ('.mysql_escapeCSV($shapeCutRecordNums).')';
    }
    
    // load records from 'products'
    list($productsRecords , $productsMetaData) = getRecords(array(
      'tableName'   => 'products',
      'loadUploads' => false,
      'allowSearch' => false,
      'where'       => $where,
    ));
  }

Hope this helps!

Cheers!

Daryl Maximo
PHP Programmer - interactivetools.com

By samsam - February 3, 2014

Hi Daryl

Thanks so much for taking the time. We'll give a shot and let you know!

By samsam - February 3, 2014

This is amazing, thanks Greg!