Jump to content

Recommended Posts

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";
  }

 

Link to comment
https://forums.phpfreaks.com/topic/246397-troubling-orders/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/246397-troubling-orders/#findComment-1265523
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/246397-troubling-orders/#findComment-1265530
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/246397-troubling-orders/#findComment-1266019
Share on other sites

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.