Re: Options Available for $searchOptions Array in Multi-Search

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

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

In response to: [url "http://www.interactivetools.com/forum/forum-posts.php?postNum=2229396#post2229396"]Options Available for $searchOptions Array in Multi-Search[/url], ...

Greg,

I was wondering....is there ANY options available to take the results of a multisearch, and sort the resulting array by the createdDate field?  I really don't want the results being grouped by the table if possible.  

I'm able in my multisearch to add the folllowing to each $searchTables routine:

'field1'   => 'createdDate',

This gives me access to the date field.

I can then run that through a sort function to sort the array:

function date_sort2($a, $b) {
    $t1 = strtotime($a['field1']);
    $t2 = strtotime($b['field1']);
    return $t2 - $t1;


usort($searchRows, 'date_sort2');

This works great except it breaks with pagination.  I'm guessing the way pagination works is that is only displays the records based on perPage.....so each page it re-runs the query and redoes the sort.  Not want I want.

Any ideas?

I'm guessing at this point my only option might be to display ALL the search results at once, and use some jQuery pagination code to do faux pages?

-Kevin

LM

By gregThomas - July 25, 2013

Hi Kevin,

Another way you could do this is to use the multi search order by functionality. Here is an example of how to use it:

// site search stuff
$searchOptions = array();
$searchOptions['keywords'] = @$FORM['q'];
$searchOptions['perPage']  = "20";
$searchOptions['debugSql'] = "1";
$searchOptions['orderBy']  = 'field1 ASC ';


$searchTables = array();

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

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

So an orderBy key can be added to your searchOptions, which can contain a string that has a MySQL order by statement in it. You will need to add a space to the end of the string because of a bug in the function.

So in the above example I would always return the createdDate as field1, then I sort by field1 to ensure my results are returned in date order. 

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By weblm - July 25, 2013

Holy cow......that is way too easy.  Guess I should have waited for a response ;-)

Can I ask.....can this multi search functionality be used to grab all the articles from multiple tables, and just return them in date order?

Here's what I mean......I have multiple sections in the CMS that on the site are all grouped under the umbrella of Resources.  So each of these sections has it's own list page.  What I'd like to do is create a master Resources page that grabs ALL the records from all the sections, sorts them by createdDate, and lists them on a master resources page.  I know I can do this with multiple getRecords calls and then array_merge and sort, like what I was going to do on the search page.....but again my problem is pagination.  I can't use the CMS Builder pagination code on a merged array.

Can this multi-search functionally be used to just return ALL records sorted by createdDate?  Or can you think of another way?

-Kevin

LM

By gregThomas - July 25, 2013

Hi Kevin, 

I've just done a quick bit of testing, and this is possible, here is some example code:

// site search stuff
$searchOptions = array();
$searchOptions['keywords'] = ':';
$searchOptions['perPage']  = "20";
$searchOptions['debugSql'] = "1";
$searchOptions['orderBy']  = 'field1 DESC ';


$searchTables = array();

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

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

So I've changed it so that the keyword that is searched for is always ':', and then changed the searchTables arrays so that they always search the createdDate field. As a createdDate field will always have a colon in, all the contents of the tables will always be returned. The tables will also be ordered by the createdDate.

Cheers

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By weblm - July 25, 2013

Greg,

Let me know where I can send a Christmas gift this year!!!!  This is awesome.  It looks like it's working great.   I have a few more things to sort out and test and will let you know if I run into any issues....but so far this is doing exactly what I need it to.

Thank you so much.

-Kevin

LM