• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 188
  • Last Modified:

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>";
0
bdipasquale
Asked:
bdipasquale
  • 4
  • 2
1 Solution
 
hernst42Commented:
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();
0
 
bdipasqualeAuthor Commented:
NO ITS BECAUSE ITS ORDERING NUMBERS BEFORE LETTERS, COULD CHANGE THIS ?
0
 
fiboCommented:
Not sure I understand fully.
It seems that there are 2 different problems:
1 - the order in which SQL results are delivered
2 - the order in which the results are translated as HTML options.
If what really annoys you is the result of 2... this one is really easy to solve....

$the_strings = array();
while($getCBByearsResult = mysql_fetch_object($getCBByearsQuery))
{
  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) ;
for ($i=0; $i <=sizeof($the_strings_sorted); $i++)
       $startYearString .= $the_strings_sorted[i];
$startYearString .= "</select>";
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
bdipasqualeAuthor Commented:
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>
0
 
bdipasqualeAuthor Commented:
a small little mod, and it worked
0
 
bdipasqualeAuthor Commented:
$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>";
0
 
fiboCommented:
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];
...
0

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now