swatisonee Posted September 4, 2011 Share Posted September 4, 2011 I have this code below but the output on screen doesnt get sorted alphabetically. Any pointers please ? Many thanks ! $resultxx = mysql_query(" SELECT DISTINCT CITYID FROM Address ORDER BY `CITYID` asc "); //ORDER here is immaterial if ($myrowxx = mysql_fetch_array($resultxx)) { do { $cityid = $myrowxx["CITYID"]; $sqlcity1 =" SELECT * FROM Cities WHERE CITYID = $cityid ORDER BY `City` asc "; //ORDER here is irrelevant $resultcity1 = mysql_query( $sqlcity1 ) or die (mysql_error()); $myrowcity1 = mysql_fetch_array($resultcity1); printf("<option value=%d> %s", $myrowcity1["CITYID"], $myrowcity1["City"]); } while ($myrowxx = mysql_fetch_array($resultxx)); echo "</select>\n"; } else { echo "Sorry, no records were found!"; echo "</select>\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/246397-troubling-orders/ Share on other sites More sharing options...
joel24 Posted September 4, 2011 Share Posted September 4, 2011 they're not alphabetical because you're ordering by a numeric value; CITY ID, order by city rather than city id. Quote Link to comment https://forums.phpfreaks.com/topic/246397-troubling-orders/#findComment-1265300 Share on other sites More sharing options...
swatisonee Posted September 5, 2011 Author Share Posted September 5, 2011 Exactly and therein lies my problem. Because the 1st table has only the IDs it can sort numerically which is nullified when the city names are output thru the 2nd selection . So i wish to know how i can get the city selection sorted alphabetically. they're not alphabetical because you're ordering by a numeric value; CITY ID, order by city rather than city id. Quote Link to comment https://forums.phpfreaks.com/topic/246397-troubling-orders/#findComment-1265523 Share on other sites More sharing options...
jcbones Posted September 5, 2011 Share Posted September 5, 2011 I would suggest writing one query to do the work you have two doing. It eliminates the headaches. Un-tested: SELECT a.CITYID, b.City FROM Address AS a JOIN Cities AS b ON a.CITYID = b.CITYID GROUP BY a.CITYID ORDER BY b.City ASC It can be tricky (sometimes) getting a group by and an order by running on the same query. So let us know if that doesn't work, we may have to go with a sub-query to get it working correctly. Quote Link to comment https://forums.phpfreaks.com/topic/246397-troubling-orders/#findComment-1265530 Share on other sites More sharing options...
swatisonee Posted September 6, 2011 Author Share Posted September 6, 2011 It works perfectly !! Thank you so much. I ought to have thought about using JOIN. I would suggest writing one query to do the work you have two doing. It eliminates the headaches. Un-tested: SELECT a.CITYID, b.City FROM Address AS a JOIN Cities AS b ON a.CITYID = b.CITYID GROUP BY a.CITYID ORDER BY b.City ASC It can be tricky (sometimes) getting a group by and an order by running on the same query. So let us know if that doesn't work, we may have to go with a sub-query to get it working correctly. Quote Link to comment https://forums.phpfreaks.com/topic/246397-troubling-orders/#findComment-1266019 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.