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: [NigelGordijk] Listing alphabetically under A to Z headings

By Jason - January 25, 2011

Hi Nigel,

First, we need to create an array and place each of our records in that array based on the first letter of it's service_desc field:

//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);


After that, it's just a matter of outputting the array we created:

<?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 ?>


This should get you started.

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

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: [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