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

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

Ok, well to test I did accomplish this using jQuery and a pagination plugin.  It works.  Basically I put the per page option to 1000.....and have the plugin to the pagination.

I think if I go this way, I'll also array_slice the sorted array to artificially keep the overall size down to something manageable.

Does this sound like the best way to solve this?

-Kevin

BTW: The jQuery pagination code I'm using is from here: http://dl.dropboxusercontent.com/u/4151695/html/pajinate/examples/example1.html

Very simple to implement.

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 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