DannGrant Posted February 25, 2010 Share Posted February 25, 2010 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] Quote Link to comment https://forums.phpfreaks.com/topic/193337-listing-records-sorted-by-alphabetic-dividers-problem/ Share on other sites More sharing options...
PFMaBiSmAd Posted February 25, 2010 Share Posted February 25, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/193337-listing-records-sorted-by-alphabetic-dividers-problem/#findComment-1017999 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.