Select option box help (pulling from db)?

Discussion in 'PHP' started by mokimofiki, Jul 10, 2009.

  1. #1
    I would like for the drop down box to have a list of the states within my database but without duplicating them.

    My code:
    <h1>Step1: Choose Your State</h1>
    <center>
    <form action=\"builders.php\" method=\"post\">
    State: 
    <select name=\"state\" onChange=\"this.form.submit();\">
    <option>Choose State</option>
    <option>----------------------------</option>
    ";
    
    $result = mysql_query("SELECT * FROM builders ORDER BY builderstate ASC");
    
    while($row = mysql_fetch_array($result))
      {
      echo "<option value=\"" . $row['builderstate'] . "\">" . $row['builderstate'] . "</option>";
      echo "<br />";
      }
    
    echo "
    </select>
    <input type=\"hidden\" name=\"stateselected\" value=\"1\">
    </form>
    </center>
    Code (markup):
    If there are 2 builders with the same state the state will show up as 2 options. I need it to only show up once.

    Thank you in advance.
     
    mokimofiki, Jul 10, 2009 IP
  2. Bogdanel

    Bogdanel Active Member

    Messages:
    77
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    65
    #2
    then why u put the br in while ? :) why u select all table if you need just the "builderstate" column :)
     
    Bogdanel, Jul 10, 2009 IP
  3. mokimofiki

    mokimofiki Well-Known Member

    Messages:
    444
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    130
    #3
    I got it figured out you just have to add DISTINCT to the SELECT line:

    $result = mysql_query("SELECT DISTINCT builderstate FROM builders ORDER BY builderstate ASC");
     
    mokimofiki, Jul 10, 2009 IP