Listing alphabetically under A to Z headings

11 posts by 2 authors in: Forums > CMS Builder
Last Post: January 26, 2011   (RSS)

Is there a way of sorting and presenting records on a Web page under the relevant A to Z heading, like this?

http://app.kitchener.ca/services/default2.aspx

Many thanks!
Nigel Gordijk

Common Sense Design: User-focused Web design
Tel: 001 519 342 5348 | Web: www.commonsensedesign.net

Re: [NigelGordijk] Listing alphabetically under A to Z headings

By Jason - January 25, 2011

Hi Nigel,

Yes, you can sort your records into an array based on the first letter of a string. If you can give me some more details on the section you're trying to do this with, I can try to give you a more concrete example.

Hope this helps.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Listing alphabetically under A to Z headings

By NigelGordijk - January 25, 2011 - edited: January 25, 2011

Hi, Jason.

This is the overall look and effect I'm going for - http://app.kitchener.ca/services/default2.aspx - and this is my version - http://www.wilmot.ca/a-z-services.php. I've only added a few records via CMSB for now, but all of the services that start with "A", for example, should appear under the "A" heading (Agendas, Animal Control, etc); Contact Us and Council should be listed under the "C" heading; and so on.

This has been set up as a Multi Record section in CMSB, and this is the display code I'm using (from the listing page code):

<?php foreach ($a_z_of_servicesRecords as $record): ?>
<p class="pAZListing"><a href="<?php echo $record['url'] ?>"><?php echo $record['service_desc'] ?></a></p>
<?php endforeach ?>
<?php if (!$a_z_of_servicesRecords): ?><?php endif ?>


At he moment this snippet of code only appears in the "A" section of the page. It will need to be placed under each letter heading, but I don't know how to get it to display only the relevant records that start with the same letter.

Cheers,
Nigel Gordijk

Common Sense Design: User-focused Web design
Tel: 001 519 342 5348 | Web: www.commonsensedesign.net

Re: [Jason] Listing alphabetically under A to Z headings

Hi, Jason.

Is it just a matter of replacing my old code:

<?php foreach ($a_z_of_servicesRecords as $record): ?>
<p class="pAZListing"><a href="<?php echo $record['url'] ?>"><?php echo $record['service_desc'] ?></a></p>
<?php endforeach ?>
<?php if (!$a_z_of_servicesRecords): ?><?php endif ?>


with yours?

//create array
$recordsByFirstLetter = array();

//populate array
foreach($a_z_of_servicesRecords as $record){
//get the first letter of the service_desc
$firstLetter = strtoupper(substr(trim($record['service_desc']),0,1));

//add record to the array based on it's first letter
$recordsByFirstLetter[$firstLetter][]=$record;
}

//sort records
asort($recordsByFirstLetter);

<?php //Output sorted records by first letter ?>
<?php foreach($recordsByFirstLetter as $firstLetter => $records):?>
<h2><?php echo $firstLetter;?></h2>

<?php foreach($records as $record):?>
<p class="pAZListing"><a href="<?php echo $record['url'];?>"><?php echo $record['title'];?></a></p>
<?php endforeach ?>
<?php endforeach ?>


I'm getting an error coming up: http://www.wilmot.ca/a-z-services.php

Thanks,
Nigel Gordijk

Common Sense Design: User-focused Web design
Tel: 001 519 342 5348 | Web: www.commonsensedesign.net

Re: [NigelGordijk] Listing alphabetically under A to Z headings

By Jason - January 25, 2011

Hi Nigel,

The first part of the code where you create the array would go up at the top of your page, under where you're selecting $a_z_of_serviceRecords. The second part would go where you want to output your list.

Hope this helps.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Listing alphabetically under A to Z headings

By NigelGordijk - January 26, 2011 - edited: January 26, 2011

Thanks for the explanation, Jason.

Everything is displaying under the correct letter but the letters themselves are appearing in reverse alphabetical order. Is there a way to get them to go the other way around? The items listed under each letter are in the correct order.

http://www.wilmot.ca/a-z-services.php

Also, near the top of the page is the full alphabet on one line with each letter linked to an anchor below, so people can click on, say, T and go straight to everything that starts with the letter T. Is it possible to do something similar with your alphabetical technique?

Thanks,
Nigel Gordijk

Common Sense Design: User-focused Web design
Tel: 001 519 342 5348 | Web: www.commonsensedesign.net

Re: [NigelGordijk] Listing alphabetically under A to Z headings

By Jason - January 26, 2011

Hi Nigel,

Could you please attach a copy of a-z-services.php so I can see all of the code?

Thanks
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Listing alphabetically under A to Z headings

Here you go, Jason.
Nigel Gordijk

Common Sense Design: User-focused Web design
Tel: 001 519 342 5348 | Web: www.commonsensedesign.net
Attachments:

a-z-services.php 21K

Re: [NigelGordijk] Listing alphabetically under A to Z headings

By Jason - January 26, 2011

Hi Nigel,

I made a couple of quick changes to your file. I pre-populated your array will the entire alphabet, so that should take care of the sorting issue. I also have the alphabet list outputting dynamically and linking to the sections that have records.

Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/
Attachments:

a-z-services_001.php 18K