Darkmatter5 Posted May 30, 2008 Share Posted May 30, 2008 If I have the following code: <?php include 'library/dbconfig.php'; include 'library/opendb.php'; $query="SELECT state_id, state, state_abbrev FROM byrnjobdb.states ORDER BY state ASC"; $result=mysql_query($query); echo "<select method='get'>"; echo "<option>---Select---</option>"; while ($row=mysql_fetch_array($result)) { $r1=$row['state_id']; $r2=$row['state']. ", " .$row['state_abbrev']; echo "<option value='$r1'>$r2</option>"; } echo "</select>"; include 'library/closedb.php'; ?> How can I use <option selected value="Texas, TX">Texas, TX</option> or something similar to have it by default select Texas. I can't figure out how to do this as the list is dynamically generated. Thanks in advance! Link to comment https://forums.phpfreaks.com/topic/108016-default-selected-value-in-dropdown-list-through-php/ Share on other sites More sharing options...
MadTechie Posted May 30, 2008 Share Posted May 30, 2008 you would do it something like this echo "<select name='get'>"; $sel = ($_GET['get'] == $r1)?"Selected":""; echo "<option $sel value='$r1'>$r2</option>"; Link to comment https://forums.phpfreaks.com/topic/108016-default-selected-value-in-dropdown-list-through-php/#findComment-553600 Share on other sites More sharing options...
.josh Posted May 30, 2008 Share Posted May 30, 2008 <?php include 'library/dbconfig.php'; include 'library/opendb.php'; $query="SELECT state_id, state, state_abbrev FROM byrnjobdb.states ORDER BY state ASC"; $result=mysql_query($query); echo "<select method='get'>"; echo "<option>---Select---</option>"; $sel = FALSE; while ($row=mysql_fetch_array($result)) { $r1=$row['state_id']; $r2=$row['state']. ", " .$row['state_abbrev']; echo "<option value='$r1'" . ($sel) ? " selected" : "" . ">$r2</option>"; $sel = TRUE; } echo "</select>"; include 'library/closedb.php'; ?> Link to comment https://forums.phpfreaks.com/topic/108016-default-selected-value-in-dropdown-list-through-php/#findComment-553607 Share on other sites More sharing options...
thebadbad Posted May 30, 2008 Share Posted May 30, 2008 selected="selected" if you're advocating the standards Link to comment https://forums.phpfreaks.com/topic/108016-default-selected-value-in-dropdown-list-through-php/#findComment-553612 Share on other sites More sharing options...
.josh Posted May 30, 2008 Share Posted May 30, 2008 oops i didn't think that through. this works. <?php include 'library/dbconfig.php'; include 'library/opendb.php'; $query="SELECT state_id, state, state_abbrev FROM byrnjobdb.states ORDER BY state ASC"; $result=mysql_query($query); echo "<select method='get'>"; echo "<option>---Select---</option>"; $sel = "somestate"; while ($row=mysql_fetch_array($result)) { $r1=$row['state_id']; $r2=$row['state']. ", " .$row['state_abbrev']; echo "<option value='$r1'"; echo ($row['state'] == $sel) ? " selected" : ""; echo ">$r2</option>"; } echo "</select>"; include 'library/closedb.php'; ?> Link to comment https://forums.phpfreaks.com/topic/108016-default-selected-value-in-dropdown-list-through-php/#findComment-553616 Share on other sites More sharing options...
.josh Posted May 30, 2008 Share Posted May 30, 2008 okay for real it works now. For some reason I thought you wanted the first one to be default. I reread it and changed it. Works for really real now. Link to comment https://forums.phpfreaks.com/topic/108016-default-selected-value-in-dropdown-list-through-php/#findComment-553627 Share on other sites More sharing options...
Darkmatter5 Posted May 30, 2008 Author Share Posted May 30, 2008 I copy and pasted the last code you wrote and changed "somestate" to "Texas", but it still doesn't work. Here's the code. <?php include 'library/dbconfig.php'; include 'library/opendb.php'; $query="SELECT state_id, state, state_abbrev FROM byrnjobdb.states ORDER BY state ASC"; $result=mysql_query($query); echo "<select method='get'>"; echo "<option>---Select---</option>"; $sel="Texas"; while ($row=mysql_fetch_array($result)) { $r1=$row['state_id']; $r2=$row['state']. ", " .$row['state_abbrev']; echo "<option value='$r1'"; echo ($row['state'] == $sel) ? " selected" : ""; echo ">$r2</option>"; } echo "</select>"; include 'library/closedb.php'; ?> Any thoughts? Link to comment https://forums.phpfreaks.com/topic/108016-default-selected-value-in-dropdown-list-through-php/#findComment-553807 Share on other sites More sharing options...
.josh Posted May 30, 2008 Share Posted May 30, 2008 // inside your loop, add the following... while ($row=mysql_fetch_array($result)) { $r1=$row['state_id']; $r2=$row['state']. ", " .$row['state_abbrev']; echo "<option value='$r1'"; echo ($row['state'] == $sel) ? " selected" : ""; echo ">$r2</option>"; echo "<color = 'red'> {$row['state']} : $sel <br />"; //add this line here } post output Link to comment https://forums.phpfreaks.com/topic/108016-default-selected-value-in-dropdown-list-through-php/#findComment-553894 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.