We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Help with sorting a list

Development and Programming

matthewatkins's avatar
matthewatkins
12 posts
13 years ago
matthewatkins's avatar matthewatkins

I have a list of cities and states that populates from a channel. My code looks like this:

<ul class="dropdown-menu">
            {exp:channel:entries channel="areas" dynamic="no" orderby="area_state|title" sort="asc|asc"}
                {area_state}
               <li><a href="http://{path=area/{url_title}}">{title}</a></li> 
              
            {/exp:channel:entries} 
</ul>

I am able to sort the list and display as folows:

  • City, ST
  • City, ST
  • City, ST

I need to display it like:

  • State
    • City
    • City
    • City

I have been able to display like:

  • State
    • City
  • State
    • City
  • State
    • City

Just not like I need. Could anyone help me with the proper way to write this List? Thanks in advance, this has been bugging me.

       
the3mus1can's avatar
the3mus1can
426 posts
13 years ago
the3mus1can's avatar the3mus1can

I don’t think you will be able to achieve that with just standard EE template code. You will probably have to use some PHP in the template. Try this (PHP parsed on output):

<?php $areas = array(); ?>

{exp:channel:entries channel="areas" dynamic="no" orderby="area_state|title" sort="asc|asc"}
 <?php $areas['{area_state}'][] = '<a href="http://{path=area/{url_title}}">{title}</a>'; ?>
{/exp:channel:entries} 

<ul class="dropdown-menu">
 <?php foreach($areas as $state => $state_areas): ?>
  <?php echo $state; ?>
  <?php foreach($state_areas as $area): ?>
   <li><?php echo $area; ?></li>
  <?php endforeach; ?>
 <?php endforeach; ?> 
</ul>
       
matthewatkins's avatar
matthewatkins
12 posts
13 years ago
matthewatkins's avatar matthewatkins

That did the trick perfectly. Thanks so much for your help!

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.