orderBy Search Results

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

By s2smedia - May 7, 2013

I have a search function on my site that works great.. however, not sure how to order the results by Alphabetical order:

ex:   'orderBy'     => 'item ASC',

<?php
require_once "../../admin/lib/viewer_functions.php";

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

$searchTables = array();
$searchTables['products'] = array(
'viewerUrl' => 'productdetails.php',
'titleField' => 'item',
'summaryField' => 'item_description',
'field1' => 'pattern',
'field2' => 'size',
'searchFields' => array('item','item_description','pattern'),

);

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


?>

any help would be appreciated! thanks!

By Steve99 - May 8, 2013

Hi,

The php function asort should do the trick: http://php.net/manual/en/function.asort.php

Hope this helps.

- Steve

By s2smedia - May 8, 2013

ok great.. problem is Im not sure exactly how to implement that into my code. :(

By Steve99 - May 8, 2013

Actually, this might work best for you. Try this:

          function compare_title_alpha($a, $b) { 
            return strnatcmp($a['yourField'], $b['yourField']); 
          } 
         
          usort($yourRecords, 'compare_title_alpha');

- Steve

By s2smedia - May 8, 2013

I added this (towards bottom), doesnt seem to be working:

<?php
require_once "../../admin/lib/viewer_functions.php";

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

$searchTables = array();
$searchTables['products'] = array(
'viewerUrl' => 'productdetails.php',
'titleField' => 'item',
'summaryField' => 'item_description',
'field1' => 'pattern',
'field2' => 'size',
'searchFields' => array('item','item_description','pattern'),

);

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


function compare_title_alpha($a, $b) {
return strnatcmp($a['item'], $b['item']);
}

usort($searchTables, 'compare_title_alpha');

?>

By Steve99 - May 8, 2013

It looks like you should change this part:

This:
usort($searchTables, 'compare_title_alpha');

Change to this:
usort($searchRows, 'compare_title_alpha');


It works great for me. This is the basic main setup from the code output:

list($yourRecords, $yourMetaData) = getRecords(array(
          'tableName'   => 'yourTable',
));

function compare_title_alpha($a, $b) { 
 return strnatcmp($a['item'], $b['item']); 


usort($yourRecords, 'compare_title_alpha');

Let me know how you make out.

- Steve

By s2smedia - May 8, 2013

UHHGG.. now I get Undefined index: item in /home/roshotel/public_html/test/Products/search.php on line 28 Notice: Undefined index: item in /home/roshotel/public_html/test/Products/search.php on line 28 Notice:

line 28 is:    return strnatcmp($a['item'], $b['item']); 

full code:

<?php
require_once "../../admin/lib/viewer_functions.php";




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


$searchTables = array();
$searchTables['products'] = array(
'viewerUrl' => 'productdetails.php',
'titleField' => 'item',
'summaryField' => 'item_description',
'field1' => 'pattern',
'field2' => 'size',
'searchFields' => array('item','item_description','pattern'),

);





list($searchRows, $searchDetails) = searchMultipleTables($searchTables, $searchOptions);
function compare_title_alpha($a, $b) {
return strnatcmp($a['item'], $b['item']);
}

usort($searchRows, 'compare_title_alpha');


?>

By Steve99 - May 8, 2013

Yikes. Well "item" isn't part of $searchRows then. Is it in $searchDetails?

By Jason - May 8, 2013

Hi,

One things I noticed, is that in your code, you  are only searching against 1 table (products).  In this case, it might be easier to use a simple getRecords call instead of using searchMultipleTables.  However, if you want to use the current set up, you can use "orderBy" in the search options.  For example, if you want to sort by "item":

$searchOptions['orderBy'] = "item";

Hope this helps

---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/