a-ok Posted October 22, 2017 Share Posted October 22, 2017 I'm searching two different templates in my search results and would like to split these results into two results, one per template. I'm guessing I might have to loop through them and remove the articles with X template then store them and add them after but I wondered if there was a better way? <?php $results = ''; if ($q = $sanitizer->selectorValue($input->get->q)) : ?> <?php $input->whitelist('q', $q); $matches = $pages->find('title~=' . $q . ', template=speech-archive-detail|training-detail, sort=template'); $count = count($matches); ?> <?php if ($count) : ?> <?php foreach ($matches as $result) : ?> <?php endforeach; ?> <?php endif; ?> <?php endif; ?> Link to comment Share on other sites More sharing options...
lokomotivan Posted October 22, 2017 Share Posted October 22, 2017 Not sure how do you wanna display results. If u want to group them by the template, u can simply have two search results and loop true them separately: $matches1 = $pages->find("title~=$q, template=speech-archive-detail"); $matches2 = $pages->find("title~=$q, template=training-detail"); Or, if you wanna mix the results, u can check for the template in a loop: <?php if ($count) : ?> <?php foreach ($matches as $result) : ?> <?php if($result->template == "speach-archive-detail"):?> <!-- Display results this way --> <?php else: ?> <!-- its not speach-archive-detail, display it in a nother way --> <?php endif;?> <?php endforeach; ?> <?php endif; ?> 3 Link to comment Share on other sites More sharing options...
abdus Posted October 23, 2017 Share Posted October 23, 2017 You can iterate over matches pages and group them in an array: <?php $matches = $pages->find('title~=' . $q . ', template=speech-archive-detail|training-detail, sort=template'); $grouped = []; foreach ($matches as $m) { // you can use template->label, too $grouped[$m->template->name] = isset($grouped[$m->template->name]) ? $grouped[$m->template->name] : []; $grouped[$m->template->name][] = $m; } // sort by template name ksort($grouped); ?> <?php foreach ($grouped as $templateName => $matches): ?> <?= $templateName ?>: <br> <?php foreach ($matches as $match): ?> <?= $match->title ?> <?php endforeach; ?> <?php endforeach; ?> 4 Link to comment Share on other sites More sharing options...
Robin S Posted October 23, 2017 Share Posted October 23, 2017 One more option: $matches = $pages->find("title~=$q, template=speech-archive-detail|training-detail"); $speech_archive_matches = $matches->find("template=speech-archive-detail"); $training_matches = $matches->find("template=training-detail"); 2 Link to comment Share on other sites More sharing options...
a-ok Posted October 23, 2017 Author Share Posted October 23, 2017 22 minutes ago, Robin S said: One more option: $matches = $pages->find("title~=$q, template=speech-archive-detail|training-detail"); $speech_archive_matches = $matches->find("template=speech-archive-detail"); $training_matches = $matches->find("template=training-detail"); I wonder what is fastest? Two queries like @lokomotivan suggested (which I opted for) or @Robin S's suggestion? Thanks for all your help. 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