SIERRA Posted August 31, 2018 Share Posted August 31, 2018 Hai , Coding: <select name="eventstart" id="eventstart"> <option value=""><?php echo $lang['select_event_start']; ?></option> <?php foreach( $events_pages->events->find($filter.", sort=event_start") as $event) {?> <option value="<?php echo $event->event_start;?>"><?php echo $event->event_start;?></option> <?php } ?> </select> It results: But , i need result without repeated dates.how to 'groupby' event_start field in processwire ?could you please sugguest me Thank you Link to comment Share on other sites More sharing options...
dragan Posted August 31, 2018 Share Posted August 31, 2018 You could probably use unique(): http://cheatsheet.processwire.com/pagearray-wirearray/getting-items/a-unique/ Either way, I would avoid doing a find() in every foreach - that's inefficient. Define a variable for the results before your foreach. $results = $events_pages->events->find($filter.", sort=event_start"); $unique = $results->unique(); foreach($unique as $e) // etc. Link to comment Share on other sites More sharing options...
Soma Posted August 31, 2018 Share Posted August 31, 2018 There's no group by feature in PW. WireArray->unique() doesn't work for this purpose. Using the API you could Just build an array using WireArray->explode() and use array_unique(). Depending how large the result set is you could cache the array so it only builds the array when it expires or based on a selector. $uniquedates = $cache->get("uniquedates", "template=basic-page", function(){ return array_unique(wire("pages")->find("template=basic-page, sort=date")->explode("date")); }); print_r($uniquedates); If the amount of entries is very large and it would take several seconds to build the array, it could make sense to use something like direct SQL query. 2 Link to comment Share on other sites More sharing options...
bernhard Posted August 31, 2018 Share Posted August 31, 2018 11 minutes ago, Soma said: If the amount of entries is very large and it would take several seconds to build the array, it could make sense to use something like direct SQL query. It's easy using RockFinder. See the example in the docs: https://gitlab.com/baumrock/RockFinder#custom-sql-aggregations-groupings-distincts 3 Link to comment Share on other sites More sharing options...
d'Hinnisdaël Posted September 4, 2018 Share Posted September 4, 2018 For most group-by-property situations, I use Fun with hooks: PageArray::groupBy. 1 Link to comment Share on other sites More sharing options...
bernhard Posted September 4, 2018 Share Posted September 4, 2018 7 hours ago, d'Hinnisdaël said: For most group-by-property situations, I use Fun with hooks: PageArray::groupBy. Just have in mind that this method gets very slow with lots of pages. Link to comment Share on other sites More sharing options...
d'Hinnisdaël Posted September 4, 2018 Share Posted September 4, 2018 3 hours ago, bernhard said: Just have in mind that this method gets very slow with lots of pages. Good point. I've always used it conjunction with some light caching to store the results as JSON. 1 Link to comment Share on other sites More sharing options...
KarlvonKarton Posted December 15, 2018 Share Posted December 15, 2018 On 8/31/2018 at 2:17 PM, bernhard said: It's easy using RockFinder Great module! It is everything I needed for "group by" queries. 2 Link to comment Share on other sites More sharing options...
pwfans Posted July 17, 2020 Share Posted July 17, 2020 Maybe below code can help for simple group by date for example : $list = $pages->find("template=event"); $groups = array(); foreach ($list as $item) { $groups["$item->date"][] = $item->id; } foreach($groups as $key => $gr){ echo $key."<br>"; } Link to comment Share on other sites More sharing options...
bernhard Posted July 17, 2020 Share Posted July 17, 2020 RockFinder3 has a performant and versatile SQL groupby feature: https://github.com/baumrock/RockFinder3/tree/40da7c5f087d87bc33d883add5b78cf7c546bacd#predefined-methods 1 Link to comment Share on other sites More sharing options...
torf Posted May 1, 2023 Share Posted May 1, 2023 As i ran into the same Problem here is a very simple solution that only uses one loop for grouping (well it's not exactly grouping, but it looks like) $myPages = $pages->find("template=myTemplate, sort=FieldtoGroupby, sort=title"); //select all pages and sort by field you want to group, then by sorting field $lastgroup = "somevalueneverused"; foreach($myPages as $myPage) { $actualgroup = $myPage->FieldtoGroupby; if ($lastgroup != $actualgroup) { echo "<h3>".$myPage->FieldtoGroupby."</h3>"; } echo "<p>".$myPage->title."</p>"; $lastgroup = $actualgroup; } Downside: you only can sort your categories by the field to group by. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now