Kenny Pollock Posted March 11, 2008 Share Posted March 11, 2008 I have a value, let's say "4" retrieved from a row in the database. I want to have a drop down that's like this: <option value="4">statusname from the status table for the id of 4</option> <option value="1">another option that's NOT 4 from the status table</option> <option value="2">another option that's NOT 4 from the status table</option> <option value="3">another option that's NOT 4 from the status table</option> So that the current option is selected, and all others are in the drop down and can be selected Quote Link to comment Share on other sites More sharing options...
revraz Posted March 11, 2008 Share Posted March 11, 2008 You need to do a IF statement in the OPTION and if it == 4 then echo "selected" Quote Link to comment Share on other sites More sharing options...
cunoodle2 Posted March 11, 2008 Share Posted March 11, 2008 This is an example of some code I used for selecting the month. Pull their selection from the database and then assign it to the variable of "$month_of_year" <select size="1" name="billing_renew_month"> <option<? if ($month_of_year == 1) echo " SELECTED"; ?> value="1">January</option> <option<? if ($month_of_year == 2) echo " SELECTED"; ?> value="2">February</option> <option<? if ($month_of_year == 3) echo " SELECTED"; ?> value="3">March</option> <option<? if ($month_of_year == 4) echo " SELECTED"; ?> value="4">April</option> <option<? if ($month_of_year == 5) echo " SELECTED"; ?> value="5">May</option> <option<? if ($month_of_year == 6) echo " SELECTED"; ?> value="6">June</option> <option<? if ($month_of_year == 7) echo " SELECTED"; ?> value="7">July</option> <option<? if ($month_of_year == echo " SELECTED"; ?> value="8">August</option> <option<? if ($month_of_year == 9) echo " SELECTED"; ?> value="9">September</option> <option<? if ($month_of_year == 10) echo " SELECTED"; ?> value="10">Ocotober</option> <option<? if ($month_of_year == 11) echo " SELECTED"; ?> value="11">November</option> <option<? if ($month_of_year == 12) echo " SELECTED"; ?> value="12">December</option> </select> Quote Link to comment Share on other sites More sharing options...
soycharliente Posted March 11, 2008 Share Posted March 11, 2008 <?php //inside a while loop is where I'm guessing you're doing this { $selected = ($statusname == $id) ? "selected=\"selected\"" : ""; echo "<option value=\"$id\" $selected>$option</option>"; } ?> Proper HTML is selected="selected" Quote Link to comment Share on other sites More sharing options...
Kenny Pollock Posted March 11, 2008 Author Share Posted March 11, 2008 Another member of another forum helped me figure it out... function generate_options($option) { $sql = "SELECT options FROM option_table"; $result = mysql_query($sql) or die(mysql_error()); while ($row = mysql_fetch_array($result)) { print("<option value=\"$row[option]\" "); if($row[option] == $option) { print("selected "); } print(">OPTION $row[option]</option>\n"); } } Quote Link to comment Share on other sites More sharing options...
discomatt Posted March 11, 2008 Share Posted March 11, 2008 Use the ? notation to make things prettier... $cur = 3; echo '<select name="blah">' . "\n"; for ($i = 1; $i <= 4; $i++) { echo '<option value="'.$i.'"'.($i == $cur ? ' selected="1"' : '').'>'.$i.'</option>' . "\n"; } echo '</select>' . "\n"; Quote Link to comment Share on other sites More sharing options...
lemmin Posted March 11, 2008 Share Posted March 11, 2008 Proper HTML is selected="selected" Just to be clear, selected is a boolean value, so the proper way would be selected="true" However, since it is obviously false if the property isn't there, "The mere presence of the SELECTED attribute set its value to true." http://msdn2.microsoft.com/en-us/library/ms534623(VS.85).aspx Quote Link to comment Share on other sites More sharing options...
discomatt Posted March 11, 2008 Share Posted March 11, 2008 Never use MS for web-standard references... always W3. According to XHTML1.0/HTML4.01, all attributes must have a value, and all tags must have an escape (hence <br />) W3 specifically states selected="selected" here http://www.w3schools.com/tags/tag_option.asp But as long as you have a value (even 0 or false works) it will interpret. It simply checks for the existence of the attribute, not the value. Simply putting SELECTED will work, but it will not follow HTML4.01 standards. Quote Link to comment Share on other sites More sharing options...
lemmin Posted March 11, 2008 Share Posted March 11, 2008 One more reason why I don't follow the "standards." Quote Link to comment Share on other sites More sharing options...
discomatt Posted March 11, 2008 Share Posted March 11, 2008 I don't understand. What reason? Try parsing html using MS' 'standards' and you'll find you have a bloated pile of junk. Parsing W3 complaint HTML is very easy and efficient. When your exceptions to rules have exceptions themselves, you might as well just quit. Quote Link to comment Share on other sites More sharing options...
lemmin Posted March 11, 2008 Share Posted March 11, 2008 What exactly do you mean by "parsing?" Any web browser I've ever used parses my HTML just fine. Quote Link to comment Share on other sites More sharing options...
discomatt Posted March 11, 2008 Share Posted March 11, 2008 Yes, and those browsers have to parse code. If everyone could follow W3 html standards, our browsers would run leaps more efficient. I've had to program many php scripts that grabs the source from a webpage, and parses certain elements. If those pages aren't HTML4.01 compliant, I have to add an extreme amount of exceptions Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.