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