MultiSearch

6 posts by 2 authors in: Forums > CMS Builder
Last Post: July 24, 2013   (RSS)

By Toledoh - July 16, 2013 - edited: July 16, 2013

Hi All,

In multisearch.php, you have the "'titleField'      => '????'," to act as the title of the found record.  Is there a way that I can join 2 fields here?

ie: 'titleField'      => 'surname','first_name',

Thanks!

Cheers,

Tim (toledoh.com.au)

By gregThomas - July 17, 2013

Hi Tim,

Unfortunately this isn't possible without modification of core CMSB code, and any changes made to it we wouldn't be able to provide support for. If you have a look at line 1178 of the viewer_functions.php in the lib directory of your CMS Builder install, you should see the line decides what fields are selected. If you modify it from this:

$subquery         = "SELECT '$tablename' as `tablename`, num, `{$tableOptions['titleField']}` as `_title`, ";

to this:

    if(is_array($tableOptions['titleField'])){
      $fieldArray = array();
      foreach($tableOptions['titleField'] as $option){
        $fieldArray[] = "`$option`";
      }
      $fieldString = implode(",", $fieldArray);
      $subquery         = "SELECT '$tablename' as `tablename`, num, CONCAT($fieldString) as `_title`, ";
    }else{
      $subquery         = "SELECT '$tablename' as `tablename`, num, `{$tableOptions['titleField']}` as `_title`, ";
    }

Once this code has been added, you can use an array of fields in your titleField value, for example like this:

$searchTables['blog'] = array(
  'viewerUrl' => 'test.php',
  'titleField' => array('title', 'subject'),
  'summaryField' => 'content',
  'searchFields' => array('title','subject','content'),
  'field1' =>  'createdDate',
);

Then the code you've added to the viewer_functions will combine the two fields into one using the MySQL concat function.

Let me know if you have any questions. 

Cheers

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By Toledoh - July 18, 2013

Perfect Greg!

Any way that I can add separators like a comma or space?

It would be great to be able to have first_name surname - position (ie. separate with a space and a "-" as required)

Cheers,

Tim (toledoh.com.au)

By gregThomas - July 19, 2013

Hi Tim,

I've found a way that you can add strings into the concat statement, you need to update line 1178 from this:

$subquery         = "SELECT '$tablename' as `tablename`, num, `{$tableOptions['titleField']}` as `_title`, "; 

to this:

    if(is_array($tableOptions['titleField'])){
      $fieldArray = array();
      foreach($tableOptions['titleField'] as $option){
        $fieldArray[] = (substr($option, -2) == ':s')? "'".substr($option, 0, -2)."'": "`$option`";
      }
      $fieldString = implode(",", $fieldArray);
      $subquery         = "SELECT '$tablename' as `tablename`, num, CONCAT($fieldString) as `_title`, ";
    }else{
      $subquery         = "SELECT '$tablename' as `tablename`, num, `{$tableOptions['titleField']}` as `_title`, ";
    }

The line I've modified is highlighted in green. So now an if statement checks to see if :s is applied to the end of a string, if it is it treats it as a string for the concat list as opposed to a table name, so if I wanted to add '  - ' in-between my title and subject fields, my searchTables array would look like this:

$searchTables['blog'] = array(
'viewerUrl' => 'test.php',
'titleField' => array('title'," - :s", 'subject'),
'summaryField' => 'content',
'searchFields' => array('title','subject','content'),
'field1' =>  'createdDate',
);

Let me know if you have any questions.

Cheers

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By gregThomas - July 24, 2013 - edited: July 24, 2013

Hi Tim,

At some point soon we'd like to rewrite the multi search functionality completely (it's still technically an experimental feature), so we'll leave this functionality out for now.

I've also just found a much easier method for getting multiple fields and displaying their content:

$searchTables['blog'] = array(
'viewerUrl'    => 'test.php',
'titleField'   => 'title',
'summaryField' => 'content',
'searchFields' => array('title','subject','content'),
'field1'       => 'createdDate',
'field2'       => 'subject'
);

So you can use field1 to field10 to retrieve as many additional fields for a section as you require, then you can display the extra content like this:

<a class="search-result-title" href="<?php echo $record['_link'] ?>"><?php echo $title_prefix ?> &raquo; <strong><?php echo $record['_title'] ?> - <?php echo $record['field2']; ?></strong></a>

This functionality is already built into CMS Builder, so there is not need to make changes to the core code either.

Cheers

Greg

Greg Thomas







PHP Programmer - interactivetools.com