Options Available for $searchOptions Array in Multi-Search

6 posts by 3 authors in: Forums > CMS Builder
Last Post: March 15, 2013   (RSS)

By JLynne77 - February 5, 2013 - edited: February 7, 2013

I have a, hopefully, quick question.

For a multi-search, there's this piece of code to put on the list page.

$searchOptions = array();
$searchOptions['keywords'] = @$_REQUEST['q'];
$searchOptions['perPage'] = "10";
$searchOptions['debugSql'] = "0";

I was wondering if there were any other options that could be put in this array and what they were, if they're pre-defined anywhere. Ideally, I was hoping to take 'keywords' and maybe use 'query'. Similar to how the difference between a _keyword search and a _query search work using the URL method for a single table list viewer. Right now, the search returns a massive list of results that I'm hoping to narrow down by allowing someone to add keywords to the search field, like using an 'AND' instead of an 'OR'.

I'm also wondering if there's an "ORDER BY" option. It feels like the results are being displayed in a strange order.

Thanks in advance! :)

-----
~Jessica Sullivan, Crystal Realm Designs.

By gregThomas - February 8, 2013

Hi Jessica,

Sorry for the delay in reply.

The searchMultipleTables function is currently an experimental feature with limited functionality, the only filter option it currently has is for keywords.

The searchMultipleTables function collects results in the order the table items are added to the array. For example:

$searchTables['news'] = array(
  'viewerUrl' => 'multiSearch.php',
  'titleField' => 'title',
  'summaryField' => 'content',
  'searchFields' => array('title','content'),
);

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


Would collect news results, then blog results. If you reordered them like this:

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

$searchTables['news'] = array(
  'viewerUrl' => 'multiSearch.php',
  'titleField' => 'title',
  'summaryField' => 'content',
  'searchFields' => array('title','content'),
);

Then blog results would be collected first. So I would put sections that people are most likely to want results from at the top of the list, and least likely at the bottom.

There are currently only 4 searchOptions you can use in the searchMultipleTables function, the final one being the orderBy option. You can use this to as you would the orderBy function in a getRecords function. For example:

//NOTE: Because of a bug you always need to have a space at the end of the string.
$searchOptions['orderBy'] = "_title DESC ";

Would return results from each section ordered by there title. 

There is a bug in the orderBy functionality that means you will need to add a space at the end of the string.

Let me know if you have any questions.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By JLynne77 - February 11, 2013

Thanks, Greg. This helps a lot. :)

-----
~Jessica Sullivan, Crystal Realm Designs.

By mdegive - March 15, 2013

I also only would like an additional where clause "where publish=1", is this possible?

By gregThomas - March 15, 2013

Hi,

This behavior isn't available by default, but you can make a quick modification to the CMS Builder code to add it. If you open cmsAdmin/lib/viewerFunctions.php and add this code around line 1179:

    $where            = _addWhereConditionsForSpecialFields($schema, '', $searchOptions);

    if(@$tableOptions['customWhere']){
      if($where){
        $where .= "AND ".$tableOptions['customWhere'];
      }else{
        $where = 'WHERE '.$tableOptions['customWhere'];
      }
    }

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

Now you have the option to add custom where statements for each table search, here is an example of how you might use it:

$searchOptions = array(); 
$searchOptions['keywords'] = @$_REQUEST['q']; 
$searchOptions['perPage'] = "15"; 
$searchOptions['debugSql'] = "0"; 

$searchTables = array(); 

$searchTables['blog'] = array( 
'viewerUrl'    => 'index.php', 
'titleField'   => 'title', 
'summaryField' => 'title', 
'searchFields' => array('title'), 
'customWhere'  => "published = '1'"
); 


$searchTables['test_1'] = array( 
'viewerUrl'    => 'history.php', 
'titleField'   => 'title', 
'summaryField' => 'title', 
'searchFields' => array('title','content'), 
); 


list($searchRows, $searchDetails) = searchMultipleTables($searchTables, $searchOptions);

So when a search is carried out on the blog table, only records with the published checkbox selected will be returned.

Note: This change requires the modification of core CMSB files, if you upgrade at a later date these changes will be lost.

Let me know if you have any questions.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com