Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.