Jump to content

[SOLVED] Sort subcategory as well as main category


SEVIZ

Recommended Posts

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

Currently the code I posted brings up a drop down like this:

 

ex.jpg

 

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.  :)

Link to comment
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.