Sort records by the sort order in another table
3 posts by 3 authors in: Forums > CMS Builder
Last Post: August 13, 2013 (RSS)
By benedict - August 12, 2013
So here's a curly one. The CMS has a Wine Varieties (cms_varieties) and a Wines (cms_wines) section. The varieties are linked into the Wines section via a list field.
Now, I the customer wants the wines sorted by variety - these are not sorted alphabetically, rather by the strength of the grape from white through to red wines. So I'm using DragSortOrder in the varieties table - on the wines list viewer page how do I get the wines to order by the variety order from the variety table, i.e. not alphabetically. ?
By Steve99 - August 13, 2013
Hi Benedict,
Just to make sure I understand... You have a two multi record sections, one being the wine varieties and the other being the wines. The wine varieties populate a selectable list field in the wine listings. With this setup you would like to have one of the sort options to list the wines by grape strength from white up to red. Is this correct?
I think this should be the easiest way if you wanted to display a full list:
- Wine Varieties Table
Insert each type in order by strength with the value being the "num" field and label as "title" (assuming title is the variety name field). - Wine List Viewer Page
Have the getRecords call "orderBy" the "variety" field ASC or DESC based on how you want them ordered
That should do it if that's what you're looking for.
Aside from that, if you would like to take it a step further and only display a specific variety at a time based on the user selection - I do have a straight forward code snippet that I could provide for you that would do this. What it would do is filter your wine list for unique "varieties" and list them alphabetically. That would work well for grouping results, but if you're looking for an "ascending strength chart" that wouldn't be applicable. Let me know though.
Hope this helps.
Steve
Hi benedict,
Would it be possible to post or attach the code you've already created? I think you need to use the array_groupBy function:
$yourArray = ksort(array_groupBy($yourArray, 'dragSortOrder'));
This function would make the array key the dragSortOrder field, and you could then sort the array on this field using the ksort function.
Let me know if you have any questions.
Thanks!
Greg
PHP Programmer - interactivetools.com