SEVIZ Posted March 20, 2009 Share Posted March 20, 2009 I am using the following code to fill a select box on my site. <select name='tech' id='tech' tabindex='2'> <?php mysql_connect("localhost", "*****", "******") or die(mysql_error()); mysql_select_db("*****") or die(mysql_error()); $result = mysql_query("select *,TEAM from tech order by TEAM"); $previous = ""; // initialize variable to detect change in month name while($row = mysql_fetch_array($result)) { $ID = $row['ID']; $TEAM = $row['TEAM']; if($row['TEAM'] != $previous){ echo "<option value=".select.">Team $TEAM</option>"; //echo $row['TEAM'] . '<br />'; // display the Team Name $previous = $row['TEAM']; } ?> <?php echo "<option value=".$ID.">- $ID</option>"; } ?> </select> The above all works great. It sorts the TEAM by TEAM name. But under each team name it lists the ID's on the team. How can I also sort the IDs in order under the TEAM category? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/150323-solved-sort-subcategory-as-well-as-main-category/ Share on other sites More sharing options...
lonewolf217 Posted March 20, 2009 Share Posted March 20, 2009 how are the IDs sorted in $row['ID'] .. is it just a text string of all the IDs ? how are they delimited ? edit: I am guessing that it is a 1:1 relation in your database between a team and an ID in this case you may want collect all the information before displaying it. <?php $idstring = "" while($row = mysql_fetch_array($result)) { $ID = $row['ID']; $TEAM = $row['TEAM']; if($row['TEAM'] != $previous){ echo "<option value=".select.">Team $TEAM</option>"; //echo $row['TEAM'] . '<br />'; // display the Team Name $previous = $row['TEAM']; $idstring = $idstring . "," . $row['ID']; } else { //if there are no more IDs for that team, sort the ID list and echo it $IDs = explode(",",$idstring); sort($IDs); $idstring = implode(",",$IDs); echo $idstring; $idstring = ""; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/150323-solved-sort-subcategory-as-well-as-main-category/#findComment-789449 Share on other sites More sharing options...
SEVIZ Posted March 20, 2009 Author Share Posted March 20, 2009 In the database the techs are all sorted by ID. 7001, 7002, 7003, etc. Quote Link to comment https://forums.phpfreaks.com/topic/150323-solved-sort-subcategory-as-well-as-main-category/#findComment-789452 Share on other sites More sharing options...
lonewolf217 Posted March 20, 2009 Share Posted March 20, 2009 see my above edit .. is that basically what you are looking for ? If not i think we need more details on exactly how you want these displayed / sorted Quote Link to comment https://forums.phpfreaks.com/topic/150323-solved-sort-subcategory-as-well-as-main-category/#findComment-789457 Share on other sites More sharing options...
SEVIZ Posted March 20, 2009 Author Share Posted March 20, 2009 Currently the code I posted brings up a drop down like this: It sorts the TEAM name in alphabetical order as it should. But under each team it lists their ID's and those are not in order. They should be in numerical order but they come up 7219, 7025, 7403, 7284, etc. That specifically is what I need to correct at this time. Thanks again for the help! Quote Link to comment https://forums.phpfreaks.com/topic/150323-solved-sort-subcategory-as-well-as-main-category/#findComment-789475 Share on other sites More sharing options...
lonewolf217 Posted March 20, 2009 Share Posted March 20, 2009 try this (untested) <?php $idstring = "" while($row = mysql_fetch_array($result)) { $ID = $row['ID']; $TEAM = $row['TEAM']; if($row['TEAM'] != $previous){ echo "<option value=".select.">Team $TEAM</option>"; //echo $row['TEAM'] . '<br />'; // display the Team Name $previous = $row['TEAM']; $idstring = $idstring . "," . $row['ID']; } else { //if there are no more IDs for that team, sort the ID list and echo it $IDs = explode(",",$idstring); sort($IDs); foreach($IDs as $teamID) { echo "<option> - " . $teamID . "</option>"; } $idstring = ""; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/150323-solved-sort-subcategory-as-well-as-main-category/#findComment-789484 Share on other sites More sharing options...
sasa Posted March 20, 2009 Share Posted March 20, 2009 $result = mysql_query("select *,TEAM from tech order by TEAM, `ID`"); Quote Link to comment https://forums.phpfreaks.com/topic/150323-solved-sort-subcategory-as-well-as-main-category/#findComment-789490 Share on other sites More sharing options...
lonewolf217 Posted March 20, 2009 Share Posted March 20, 2009 $result = mysql_query("select *,TEAM from tech order by TEAM, `ID`"); of course, there is always this simpler solution as well Quote Link to comment https://forums.phpfreaks.com/topic/150323-solved-sort-subcategory-as-well-as-main-category/#findComment-789500 Share on other sites More sharing options...
SEVIZ Posted March 20, 2009 Author Share Posted March 20, 2009 Awesome! I knew it should be something with adding ID to the query but could not figure out how. Thanks to you both for helping me. Even though I used sasa's solution I thank you lonewolf for taking the time to help me out! Much appreciated. Consider this one SOLVED. Quote Link to comment https://forums.phpfreaks.com/topic/150323-solved-sort-subcategory-as-well-as-main-category/#findComment-789513 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.