ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Sort Entries Alphabetically, Group By Letter

June 03, 2008 3:49pm

Subscribe [11]
  • #1 / Jun 03, 2008 3:49pm

    keystone

    21 posts

    I’d like to return a list of entries grouped by letter, with the ability to wrap the letter header (e.g. “A”) in a different tag than the entries. Example:

    A
    Apple
    Artichoke

    B
    Ball
    Bible
    Brian

    And so on…

    I found a plugin that can do this, but it is not well documented and some of the parameters have to be hard coded into the plugin. Is it possible to do this without a plugin?


    Brian Morykon

  • #2 / Jun 03, 2008 4:49pm

    Ingmar

    29245 posts

    There is no native solution that I am aware of, so the atozee plugin is probably your best choice. There is some more info in the wiki.

    Moving to Howto.

  • #3 / Jun 03, 2008 5:01pm

    grantmx

    1439 posts

    I want something similar, but I all I need to do is sort the entry title by alphabetically.  I’ll just make exp:weblog:entries for each letter.

    I dont see the atozee plug-in on the addon-page.  Can you direct us?

  • #4 / Jun 03, 2008 5:11pm

    Ingmar

    29245 posts

    grantmx, keystone linked to the relevant forum thread in his post.

  • #5 / Jun 03, 2008 5:15pm

    grantmx

    1439 posts

    Thanks Ingmar :cheese:

  • #6 / Jun 03, 2008 5:18pm

    Ingmar

    29245 posts

    No worries, and good luck 😊

  • #7 / Jun 03, 2008 5:37pm

    rokker

    179 posts

    i sort live music venues and bands by alphetical letter (like if you want to see all bands with names starting with the letter B) using a template that sorts alphabetically. then, i made a category group to share amongst all of my weblogs called “alphabetical” where a category is held for each letter in the alphabet (each letter has a category_id). it’s handy and can be used across multiple weblogs to sort in just about any template if the subject matter can be alphabetized meaningfully.

  • #8 / Jun 03, 2008 5:43pm

    grantmx

    1439 posts

    I don’t remember, but is it possible to use exp:query in conjunction with exp:weblog:entires to pull custom field data from each entry?

  • #9 / Jun 03, 2008 5:51pm

    grantmx

    1439 posts

    Never mind.  Yes.

  • #10 / Jul 10, 2008 4:19am

    stinhambo

    1268 posts

    I am trying to create groups of links broken down by the first letter.

    So -

    A
    Apple
    Amsterdam
    Aluminium
    Abracadabra

    B
    Burger
    Black
    Banter

    Etc.

    The second thing is not to show anything if there are no entries beginning with that letter.

    I tried to adapt Ray’s SQL but I am a bit lost -

    <?php
    global $DB;
    $gletter='_';
    $x = '';
        for ($i=65; $i<=90; $i++) { // For lowercase letters, use the numbers 97-122 instead
            $x = chr($i);
          if ($gletter<>substr($x,0,1)) {
                  $gletter=substr($x,0,1);
                        $results = $DB->query("SELECT COUNT(*) as number FROM exp_weblog_titles WHERE weblog_id = 2 AND title LIKE '$gletter%' LIMIT 1;");
                        $count = $results->row['number'];
                             if ($count > 0) {
                                    echo '{exp:weblog:entries weblog="artists" orderby="title|artists_first_names" sort="asc"}
                    <div class="ddmcc">
                        <ul>
                            '.$gletter.'
                            <ul>
                                <li><a href="http://{url_title_path=">{title}, {artists_first_names}</a></li>
                            </ul>
                        </ul>
                    </div> <!-- ddmcc -->
                    {/exp:weblog:entries}';
                            } else {
                                    echo '<li>'.$gletter.'</li>';
                            }
                                        
                }
                        
        }
    ?>

    But this just spits out all the entries each time a first letter is matched with any of the entries.

  • #11 / Jul 10, 2008 8:29am

    Cocoaholic

    445 posts

    Hi Steve,

    The simplest solution I came up with is this one:

    <?php $temp = ''; ?>
    
    {exp:weblog:entries weblog="your_weblog" orderby="title" sort="asc"}
    
    <?php
    $letter = strtoupper(substr('{title}', 0, 1));
    if ($temp != $letter) {
        echo "<h3>" . $letter . "</h3><p>";<br />
        $temp = $letter;<br />
    }<br />
    ?></p>
    
    <p>{title}</p>
    
    <p>{/exp:weblog:entries}

    PHP on output.

  • #12 / Dec 19, 2008 1:41pm

    salandra

    39 posts

    How can this still script be adapted to select a particular letter of alphabet or character?

  • #13 / Mar 27, 2009 5:04am

    wrightmight

    17 posts

    Thanks, Cocoaholic, your code works like a charm.

  • #14 / Apr 26, 2010 1:40pm

    sm9

    352 posts

    Another thanks to Cocoaholic, that code just worked great for me too!

    I was even able to adapt it slightly to output a list of anchors too:

    {exp:weblog:entries weblog="my_weblog" orderby="title" sort="asc" dynamic="off"}
    <?php
    $letter = strtoupper(substr('{title}', 0, 1));
    if ($temp != $letter) {
        echo "<a >".$letter."</a>";
        $temp = $letter;
    }
    ?>
    {/exp:weblog:entries}

    To perfect it, I might try and adapt this further so that letters of the alphabet with no entries on the page still appear, but without the anchor links, but I’ll see if my client is happy without this first. 😊

    Thanks.

  • #15 / Jun 29, 2010 7:34am

    Jonathan Schofield

    175 posts

    This works a treat. Thanks to Cocoaholic and sm9. And since I am building a glossary, my tweaked code incorporates definition lists…

    <?php $temp = ''; ?>
    
    {!-- output a linked list of the letters represented with each letter as an href --}
    
    Skip to terms beginning with: 
    {exp:weblog:entries weblog="glossary" orderby="title" sort="asc"}
    <?php
    $letter = strtoupper(substr('{title}', 0, 1));
    if ($temp != $letter) {
        echo '<a href="#%27.$letter.%27" rel="self">'.$letter.'</a></li>';
        $temp = $letter;
    }
    ?>
    {/exp:weblog:entries}
    
    
    {!-- output a linked list of the terms covered to show/hide with jQuery --}
    <div id="terms">
    We have definitions for: 
    <ol>
    {exp:weblog:entries weblog="glossary" orderby="title" sort="asc"}
    <li><a href="#{url_title}" rel="self">{title}</a></li>
    {/exp:weblog:entries}
    </ol>
    </div>
    
    {!-- output the definitions --}
    {exp:weblog:entries weblog="glossary" orderby="title" sort="asc"}
    <?php
    $letter = strtoupper(substr('{title}', 0, 1));
    if ($temp != $letter) {
        // set id to the current letter as destination for the a-z list links
        echo '<h2 id="'.$letter.'">'.$letter.'</h2>';
        $temp = $letter;
    }
    ?>
    {!-- set id to the url title as destination for the list of terms links --}
    <dl><dt id="{url_title}">{title}</dt>
    <dd>{body}</dd></dl>
    
    {/exp:weblog:entries}
.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases