Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

As the title describes, I am trying to group the results of a view by their first letter. Googling revealed a way for me that half does the trick : display title twice, exclude from display, limit to 1(st) character, group by that. Clever I must admit but i need to reveal a grouping field even if there are no results in it such as:

A
  Alpha
  Anekin

B
  Bravo
  Borvo
C

D
  Delta
  Dooku

Any thoughts on how to go about doing that?

share|improve this question

4 Answers 4

A simple solution would be to add a field that only had the first letter and group by that.

Views also supports doing glossary, which is a but different, but allows you to group by the first letter and count the number of results and show fx

A(2) B(2) C(0), as an exposed filter, this is done through contexts in views 3.

share|improve this answer
    
"A simple solution would be to add a field that only had the first letter and group by that." I actually said that in my question but seeing as "C" doesn't give me any results and I still want it presented i can't see how that helps me. I'll look into Glossary. –  magtak Jan 26 '12 at 14:27
1  
@magtak You could use theming to add missing letters –  googletorp Jan 26 '12 at 14:28
1  
could you please elaborate on that? –  magtak Jan 26 '12 at 14:40
up vote 1 down vote accepted

It was really frustrating but in the end I figured it out:

In the views row output style tpl file:

<?php print $wrapper_prefix; ?>
  <?php if (!empty($title)) : ?>
    <h3><?php print $title; ?></h3>
  <?php endif; ?>

<?php $letters = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
foreach ($letters as $letter):?>
<div class="letter family">
  <div class="letter-header"><?php print $letter;?></div>
<?php print $list_type_prefix;  ?>
  <?php foreach ($rows as $id => $row):?>
    <?php $rest = substr($view->result[$id]->node_title,0,1);?>
  <?php if ($rest == $letter):?>
        <li class="<?php print $classes_array[$id]; ?>"><?php print $row; ?></li>
      <?php endif;?>
  <?php endforeach; ?>
<?php print $list_type_suffix; ?>
</div>
<?php endforeach;?>
<?php print $wrapper_suffix; ?>

This works for me since that view ouputs all it's results so I can safely assume $view->result will have the same elements in it as $rows. $rows afaik is the output of the current page. I am not sure if $view->result behaves in the same way.

share|improve this answer

Take a look at this article in Drupal.org. Hope it'll be useful for you, at least it helped me.

http://drupal.org/node/1078962

share|improve this answer
    
stumbled upon it during my google expedition - couldn't find anything useful for my particular issue but as i stated above i fixed it already :) –  magtak Jan 27 '12 at 10:21

We actually added code in the "Header" portion of our view (select input method to PHP code). Here's our php code that does what you're looking for (and it doesn't require a separate template file).

<?php
  $letters = array(
    'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','y','z'
  );  
?>  
<h2>Browse by Last Name</h2> 
<ul> 
  <li style="display:inline; padding-right:8px;"> 
    <a href="/Full_time_Faculty_Listing" >All</a>
  </li>
  <?php for ($i=0; $i<count($letters); $i++): ?> 
    <li style="display:inline; padding-right:8px;"> 
      <a href="/Full_time_Faculty_Listing/<?php print $letters[$i];?>"><?php print strtoupper($letters[$i]);?></a>
    </li> 
  <?php endfor; ?> 
</ul>
share|improve this answer
    
pretty similar to my theming solution :) –  magtak Jan 31 '12 at 11:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.