Jump to content

Splitting search results by template


a-ok
 Share

Recommended Posts

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

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; ?>

 

  • Like 3
Link to comment
Share on other sites

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; ?>

 

  • Like 4
Link to comment
Share on other sites

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");

 

  • Like 2
Link to comment
Share on other sites

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...