Drupal 5: Grouping search results by node type in collapsible fieldsets

This code snippet will group your search results in collapsible fieldsets by node type.

<?php
function MYTHEME_search_page($results, $type) {
  $html = '<dl class="search-results">';

  // get a list of node types
  $nodeTypes = node_get_types();

  // loop through results, group by type
  $resultTypes = array();
  foreach ($results as $result) {
    $resultTypes[$result['node']->type][] = $result;
  }

  // create fieldsets for each type
  foreach ($resultTypes as $resultType => $resultTypeResults) {
    $value = "";
    // loop through entries
    foreach ($resultTypeResults as $entry) {
      $value .= theme('search_item', $entry, $type);
    }

    // add fieldset
    $html .= theme('fieldset',
      array(
        '#title' => $nodeTypes[$resultType]->name,
        '#collapsible' => TRUE,
        '#collapsed' => FALSE,
        '#value' => $value,
      )
    );

  }

  $html .= '</dl>';
  $html .= theme('pager', NULL, 10, 0);

  return $html;
}
?>

Updated: