-1

I am using a table for my states and I am having a time making the return value match what was already recorder in the DB I am using the following to populate the options list:

              <label class="label" for="state"><em>State</em></label>
          <select class="select" maxlength="30" id="" name="state" ><option>
              <?php 
              $options = "";
               while ($row = mysql_fetch_array($resultstate))
                        {
                        $name=$row["name"];
                        $abbrev=$row["abbrev"]; 
                        $options.="<OPTION VALUE=\"$abbrev\">".$name;
                        } 
              echo $options;


             </option></select></div>

How can i loop through the array of states, or just tag the one in the DB as selected? I can do both seperatly, but not together...

Any help is greatly appreciated

With some tweaking:

      while ($row = mysql_fetch_array($resultstate))
                        {
                            $name = $row["name"];
                            $abbrev = $row["abbrev"]; 
                                if($state == $abbrev){
                                    echo '<OPTION VALUE="'.$abbrev.'" selected="selected">'. $name.' </OPTION>';
                                    } else {
                            echo ' <OPTION VALUE="'.$abbrev.'"> '.$name.' </OPTION>';
                                    }

                        }

Works correctly, thank you for pointing me in the right direction

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Jjames
  • 183
  • 1
  • 3
  • 14
  • how you get `one in the DB as selected` ? – GBD Dec 07 '12 at 17:34
  • Not sure why I get a point down for asking a perfectly legitimate question, but yes, I have tried the solution below, but the concatenation is not correct, and I am not sure where I am screwing it up...it's not building the option list correctly – Jjames Dec 07 '12 at 18:33

1 Answers1

1

If output is the problem...I don't quite follow what you are having problem with ?

<label class="label" for="state"><em>State</em></label>
      <select class="select" maxlength="30" id="" name="state" ><option>
          <?php 
          $options = "";
           while ($row = mysql_fetch_array($resultstate))
                    {
                    $name=$row["name"];
                    $abbrev=$row["abbrev"]; 
                    $options.="<OPTION VALUE=\"$abbrev\";
                    if($somevaluefromDb==$name){echo 'selected="selected"';}
                     echo ">".$name."</OPTION>";
                    } 
          echo $options;


         </select></div>
v0d1ch
  • 2,738
  • 1
  • 22
  • 27