Jump to content

Listing Records Sorted By Alphabetic Dividers Problem


DannGrant

Recommended Posts

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

 

[attachment deleted by admin]

Your query needs to have an ORDER BY term that will cause the rows to be returned in the order you want them. Assuming you have columns named last_name and first_name, you would need something like this -

 

"SELECT * FROM members ORDER BY  last_name, first_name"

 

Each call to a mysql_fetch_xxxxx() function only returns one row from the result set, so your existing mysql_fetch_array() statement would need to be moved down and put into a while(){} loop, which would replace the existing foreach(){} loop.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.