Link to home
Start Free TrialLog in
Avatar of bdipasquale
bdipasquale

asked on

numbers dont follow in select box when ordering sql statement

years do not follow in select box
data2 in my query contains letters or numbers which correspond to the year the car was built, thus the reason of my array, however my numbers dont come out right, I get

<select name='autoYear' class="textboxesAutoInfo"><option value=''></option><option value='2001'>2001</option>
<option value='2002'>2002</option>
<option value='2003'>2003</option>
<option value='2004'>2004</option>
<option value='2005'>2005</option>
<option value='2006'>2006</option>
<option value='2007'>2007</option>
<option value='1994'>1994</option>
<option value='1995'>1995</option>
<option value='1996'>1996</option>
<option value='1997'>1997</option>
<option value='1998'>1998</option>
<option value='1999'>1999</option>
<option value='2000'>2000</option>
</select>

here is my code


      $cbbYearArray = array("R","S","T","V","W","X","Y","1","2","3","4","5","6","7","8","9");
      $yearArray = array("1994","1995","1996","1997","1998","1999","2000","2001","2002","2003","2004","2005","2006","2007","2008","2009");
      
      $startYearString = "<select name='autoYear' class=\"textboxesAutoInfo\"><option value=''></option>";      
      $getCBByearsQuery = mysql_query("SELECT DISTINCT(data2) FROM cbb WHERE data7='" . $makeOne . "' ORDER BY data2") or die();
      while($getCBByearsResult = mysql_fetch_object($getCBByearsQuery))
      {
            for($i=0; $i <=15; $i++)
            {
                  if($getCBByearsResult->data2 == $cbbYearArray[$i])
                  {
                        $startYearString .= "<option value='" . $yearArray[$i] . "'>" . $yearArray[$i] . "</option>";
                  }
            }
            
      }
      $startYearString .= "</select>";
Avatar of hernst42
hernst42
Flag of Germany image

hm strange. maybe of the distinct. You can try:
$getCBByearsQuery = mysql_query("SELECT DISTINCT(data2) as ddata2 FROM cbb WHERE data7='" . $makeOne . "' ORDER BY ddata2") or die();
Then you also need to replace your accesses of data2 with ddata2
or

$getCBByearsQuery = mysql_query("SELECT DISTINCT(data2) FROM cbb WHERE data7='" . $makeOne . "' ORDER BY 1") or die();
Avatar of bdipasquale
bdipasquale

ASKER

NO ITS BECAUSE ITS ORDERING NUMBERS BEFORE LETTERS, COULD CHANGE THIS ?
ASKER CERTIFIED SOLUTION
Avatar of Bernard Savonet
Bernard Savonet
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
sorry but the order in which the result in my select box are the same as before

notice it does follow

<select name='autoYear' class="textboxesAutoInfo"><option value=''></option><option value='2001'>2001</option>    
<option value='2002'>2002</option>
<option value='2003'>2003</option>
<option value='2004'>2004</option>
<option value='2005'>2005</option>
<option value='2006'>2006</option>
<option value='2007'>2007</option>
<option value='1994'>1994</option>
<option value='1995'>1995</option>
<option value='1996'>1996</option>
<option value='1997'>1997</option>
<option value='1998'>1998</option>
<option value='1999'>1999</option>
<option value='2000'>2000</option>
</select>
a small little mod, and it worked
$the_strings = array();
while($getCBByearsResult = mysql_fetch_object($getCBByearsQuery))
{
  for($i=0; $i <=15; $i++)
  {
    if($getCBByearsResult->data2 == $cbbYearArray[$i])
            $the_strings[$i] = "<option value='" . $yearArray[$i] . "'>" . $yearArray[$i] . "</option>";
  }
}
$the_strings_sorted = array_unique ($the_strings) ;
for ($i=0; $i <=sizeof($the_strings_sorted); $i++)
       $startYearString .= $the_strings_sorted[$i];
$startYearString .= "</select>";
Glad it works now, thx for the grade.
Rereading I would gusess that a more elegant rewriting might be
...
{
  for($i=0; $i <=15; $i++)
  {
    if($getCBByearsResult->data2 == $cbbYearArray[$i])
            $the_strings[] = "<option value='" . $yearArray[$i] . "'>" . $yearArray[$i] . "</option>";
  }
}
$the_strings_sorted = array_unique ($the_strings) ;
foreach ({
  for($i=0; $i <=15; $i++)
  {
    if($getCBByearsResult->data2 == $cbbYearArray[$i])
            $the_strings[$i] = "<option value='" . $yearArray[$i] . "'>" . $yearArray[$i] . "</option>";
  }
}
$the_strings_sorted = array_unique ($the_strings) ;
foreach ($the_strings_sorted as $i)
       $startYearString .= $the_strings_sorted[$i];
)
       $startYearString .= $the_strings_sorted[$i];
...