eightFX Posted December 1, 2006 Share Posted December 1, 2006 Good Evening,I am trying to sort a query of names alphabetically which I can do. The issue is that I need to group them by the first letter of the names. Basically I need my output to look like this:AAmbrose, MelanieAustin, JayBBailey, BrandonBeam, Jim... etc.What I tried doing is this:[code]$alphaQuery = mysql_query("SELECT lname FROM names ORDER BY lname"); while($alphaResult = mysql_fetch_array($alphaQuery)) {$limitAlpha = substr($alphaResult[lname],0,1);echo ' - '. $limitAlpha .' - '; $Alpha = $alphaResult[R_lname];$namesQuery = mysql_query("SELECT * FROM names WHERE lname LIKE '$Alpha%' ORDER BY lname");while($namesResult = mysql_fetch_array($namesQuery)) {echo '<br>'. $namesResult[lname] .', '. $namesResult[fname] .''; } // CLOSE namesResult WHILE } //CLOSE alphaResult WHILE[/code]The above code prints out what I want but duplicates it so the out put looks like:BBailey, BrandonBeam, JimBBailey, BrandonBeam, JimFor as many names that start with the same letter. Any help appreciated, thanks! Link to comment https://forums.phpfreaks.com/topic/29158-solved-sort-alphabetically-with-alpha-as-header/ Share on other sites More sharing options...
eightFX Posted December 1, 2006 Author Share Posted December 1, 2006 I think what I need to do is select distinct values in the first query for just the first letter of lname but I do not know if that is possible...Thanks. Link to comment https://forums.phpfreaks.com/topic/29158-solved-sort-alphabetically-with-alpha-as-header/#findComment-133672 Share on other sites More sharing options...
craygo Posted December 1, 2006 Share Posted December 1, 2006 [code]<?php// Set initial group values$letter = '';// Query database$sql = "SELECT SUBSTRING(lname, 1, 1) AS letter, names FROM items ORDER BY letter, names"; $res = mysql_query($sql); $num_rows = mysql_num_rows($res);if($num_rows > 1){// Set initial row color$bgcolor = "FFFFFF"; while ($rows = mysql_fetch_assoc($res)){// Print Group headersif ($rows['letter'] != $letter) {// Print Group Nameprint '<table width="500" align="center" cellspacing=0 cellpadding=0>';print '<tr bgcolor=83EA44>';print '<td><strong>'. strtoupper($rows['letter']).'</strong></td>';print '</tr>';print '</table>';}// Alternate row colorif ($bgcolor == "#E0E0E0"){ $bgcolor = "#FFFFFF";} else { $bgcolor = "#E0E0E0";}// Print Database Detailsprint '<table width=500 align=center cellspacing=0>';print '<tr bgcolor='.$bgcolor.'>';print '<td width=20 align=center> </td>';print '<td width=400 align=center>'.$rows[names].'</td>';print '</tr>';// Reset group values$letter = $rows['letter'];}} else {// Print No data messageprint '<table width=650 align=center>';print '<tr>';print '<td align=center><strong>NO ITEMS!!</strong></td>';print '</tr>';print '</table>';}?>[/code]Ray Link to comment https://forums.phpfreaks.com/topic/29158-solved-sort-alphabetically-with-alpha-as-header/#findComment-133675 Share on other sites More sharing options...
eightFX Posted December 1, 2006 Author Share Posted December 1, 2006 Thanks craygo! I guess I was not even close! Works like a champ! Link to comment https://forums.phpfreaks.com/topic/29158-solved-sort-alphabetically-with-alpha-as-header/#findComment-133684 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.