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!

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

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!

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.