Pixel2Life Forum: PHP - Listing Records Sorted By Alphabetical Dividers Problem - Programming and General Web Design - Pixel2Life Forum

Jump to content

  • You cannot start a new topic
  • You cannot reply to this topic

PHP - Listing Records Sorted By Alphabetical Dividers Problem

Array

#1 User is offline   Daniel Grant 

  • Young Padawan
  • Pip
  • Group: Members
  • posts 2
  • Joined: 21-July 09

Posted 25 February 2010 - 07:31 AM

Hi,

Apologies for any lack of description as this is my first post on this forum.

I'm currently designing a website for a golf society and need to list all the members of that golf society sorting them alphabetically, i also want the list to have alphabetical dividers. For example a header saying 'A' containing all of the members who's last name starts with the letter 'A' and so on. Obviously if there is no member with a certain letter last name the header will not be shown.

The current code i am working with is shown below:

<?php

		$get_members = mysql_query("SELECT * FROM members");
		
		$result = mysql_fetch_array($get_members);

		$initial = '';

		foreach($result as $name)
		{

			if($name[0] != $initial)
			{

				$initial = $name[0];

				echo "<div class=\"item_header\">";
				echo "<p>$initial</p>";
				echo "</div>";

			}

			echo "$name";

		}

?>

I am aware that somewhere within this code i need to specify the last_name but wherever i put it i get errors thrown back at me.

This code may be completely wrong but if you could shed any light on the matter that would be greatly appreciated.

Thanks in advance

Dan

Attached Files


0

#2 User is offline   rc69 

  • PHP Master PD
  • PipPipPipPip
  • Group: P2L Staff
  • posts 3,827
  • Joined: 26-September 04
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 25 February 2010 - 04:28 PM

You might want to modify my change to the query a little bit (i.e. i don't know if you have a last_name field), but the rest should work:

$get_members = mysql_query("SELECT * FROM members ORDER BY last_name ASC");
		
		$initial = '';
		while($result = mysql_fetch_array($get_members)){
			if($result['name'][0] != $initial)
			{

				$initial = $result['name'][0];

				echo "<div class=\"item_header\">";
				echo "<p>$initial</p>";
				echo "</div>";

			}

			echo $result['name'];
		}

0

Share this topic:


  • You cannot start a new topic
  • You cannot reply to this topic

0 user(s) are reading this topic
members, guests, anonymous users