Sort search results by field

13 posts by 3 authors in: Forums > CMS Builder
Last Post: June 25, 2018   (RSS)

By dccreatives - June 12, 2018

I have a search results page that I have when you search a keyword it returns the product. I want to sort the page based on the 'lamping' which is a database field. I can use the sorting in the url:

http://www.axislighting.com/CMS/search-led.php?lamping=LED

But I want to sort all LED products on the top and the rest on the bottom with a <h1> separating them.

How do I do that? See attached files.

Attachments:

search-led.php 8K

led-search.jpg 58K

By leo - June 12, 2018

Hi,

It seems you want to order the results by the keyword rather than searching/filter it. Here is an example of how you can sort your records by keywords:

getRecords([
  ...
  'orderBy' => 'ORDER BY CASE WHEN lamping = ' . $keyword . ' THEN 1 ELSE 0 END'
  ...
]);

Note that you may also want to set allowSearch to false if you want to use url to pass sorting query like this ?lamping=LED.

Let me know if you have any questions.

Thanks,

Leo - PHP Programmer (in training)
interactivetools.com

By dccreatives - June 12, 2018

No. I want to search by keyword, but I want the results to be separated into sections based on the lamping...

By leo - June 12, 2018

Then you can use arrayGroupBy($records, $fieldName, true). This will return an array with the selected field to be the keys and the records to be the values.

Leo - PHP Programmer (in training)
interactivetools.com

By Damon - June 12, 2018

Hi,

It sounds like you want the products grouped by lamping type and also the search lamping keyword group (LED) to appear first on the page using this query:
http://www.axislighting.com/CMS/search-led.php?lamping=LED

This would then be followed by other lamping groups (Fluorescent) based on the image you attached?

So if the URL query was this:
http://www.axislighting.com/CMS/search-led.php?lamping=Fluorescentor
or another keyword, would that group appear first followed by other groups?

Any additional details you can provide will help us understand what the end result should be.

Thanks!

Cheers,
Damon Edis - interactivetools.com

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

By dccreatives - June 13, 2018

I only showed the url as an example of the results I want to achieve. I want the search results to be separated by lamping headers. How do I go about that. Forget the url string...

By leo - June 19, 2018 - edited: June 20, 2018

Hi,

You can use array_groupBy() to get the results separated by a field. Here is an example:

// For $itemsRecords:
$itemsGroupByLamping = array_groupBy($itemsRecords, 'lamping', true);

That will give you an array of records grouped by their "lamping" category. The next thing you need is to loop through them and display:

foreach($itemsGroupByLamping as $category => $records){
 // Display the category.
 foreach($records as $record){
  // Display the record.
 }
}

Hope this helps you to solve the problem. If you need further help feel free to send the details to consulting@interactivetools.com.

Thanks,

Leo - PHP Programmer (in training)
interactivetools.com

By dccreatives - June 20, 2018 - edited: June 20, 2018

ok. Seems like we might have something here. 

http://www.axislighting.com/CMS/search-led.php

However, 2 things. First, some products have more than one image and I only want it to show the first image, currently it is showing all. 

Second, I want to  separate the Lamping by either LED or other... So I need separate headers to show LED first and then FLUORESCENTbeneath it..

Can you help?

Attachments:

search-led.php 8K

By leo - June 22, 2018

Hi,

To show the first image of a record, you can directly use the first element of uploads field. For example: $record['images'][0]['urlPath'] will be the url path of the first element of "images" field uploads.

To organize the record array by let or others, you can manually set the if statement to pick them out first. For example:

foreach($itemsGroupByLamping['led'] as $record){
  // Display LED records
}

foreach($itemsGroupByLamping as $category => $records){
  if($category !== 'led'){
    // Display other records
  }
}

Or you can set order by to make led records the first of the array and use 1 foreach loop to display them all.

Let me know if you have any questions.

Thanks,

Leo - PHP Programmer (in training)
interactivetools.com