rbragg Posted March 14, 2007 Share Posted March 14, 2007 Say I have a droplist (drop11) with 2 sticky options (open & closed): <select name="drop11" id="11" class="style1"> <option value="open" <?php if (isset($_SESSION['drop11']) && $_SESSION['drop11'] == open) { echo 'selected="selected"';} ?> >open <option value="closed" <?php if (isset($_SESSION['drop11']) && $_SESSION['drop11'] == closed) { echo 'selected="selected"';} ?> >closed </select> I have a column in my db named Lot11 with the value of either "open" or "closed". I want to also have the above droplist to display the contents of Lot11 upon page load. How could I do that? Thanks in advance! Link to comment https://forums.phpfreaks.com/topic/42742-populate-droplist-upon-page-load/ Share on other sites More sharing options...
rbragg Posted March 15, 2007 Author Share Posted March 15, 2007 I assume I would need something like: <?php $query_getStatus = mysql_query("SELECT * FROM db") or die(mysql_error()); while($getStatus = mysql_fetch_array( $query_getStatus )) { ?> <select name="drop11" id="11" class="style1"> <option value='<?php echo $getStatus['Lot11'];?>' <?php if (isset($_SESSION['drop11']) && $_SESSION['drop11'] == open) { echo 'selected="selected"';} ?> >open </select> <?php } mysql_close; ?> But since there is an option of 2 values (open or closed), I don't know how to go about this. Link to comment https://forums.phpfreaks.com/topic/42742-populate-droplist-upon-page-load/#findComment-207919 Share on other sites More sharing options...
boo_lolly Posted March 15, 2007 Share Posted March 15, 2007 it's more like this: <?php $sql = "SELECT Lot11 FROM your_table"; $query = mysql_query($sql); echo "<select name=\"drop11\" id=\"11\" class=\"style1\">\n"; while($row = mysql_fetch_array($query)){ echo "<option value=\"{$row['Lot11']}\"". (($_POST['drop11'] == $row['drop11']) ? (" SELECTED") : ("")) .">\n"; } echo "</select>\n"; ?> get the idea? Link to comment https://forums.phpfreaks.com/topic/42742-populate-droplist-upon-page-load/#findComment-207992 Share on other sites More sharing options...
rbragg Posted March 15, 2007 Author Share Posted March 15, 2007 Ah ha ... I see. Thanks for replying. So, for the sql statement, what if I wanted to select from multiple columns ... not just Lot11? Link to comment https://forums.phpfreaks.com/topic/42742-populate-droplist-upon-page-load/#findComment-208026 Share on other sites More sharing options...
boo_lolly Posted March 15, 2007 Share Posted March 15, 2007 this will select every column in the specified table: $sql = "SELECT * FROM your_table"; Link to comment https://forums.phpfreaks.com/topic/42742-populate-droplist-upon-page-load/#findComment-208039 Share on other sites More sharing options...
rbragg Posted March 15, 2007 Author Share Posted March 15, 2007 I have: <?php include 'db_connect.php'; # connect to the db $query_getStatus = mysql_query("SELECT * FROM park_lots") or die(mysql_error()); ?> <?php echo " <select name='drop11' id='11' class='style1'> "; while($get_status = mysql_fetch_array( $query_getStatus )) { echo "<option value=\"{$get_status['Lot11']}\"". (($_SESSION['drop11'] == $get_status['drop11']) ? (" SELECTED") : ("")) .">\n"; } echo "</select>"; ?> My droplist is empty. Link to comment https://forums.phpfreaks.com/topic/42742-populate-droplist-upon-page-load/#findComment-208052 Share on other sites More sharing options...
boo_lolly Posted March 15, 2007 Share Posted March 15, 2007 your droplist isn't empty... you just haven't printed a description for your option tags. partially my fault. i guess i forgot =\ echo "<option value=\"{$get_status['Lot11']}\"". (($_SESSION['drop11'] == $get_status['drop11']) ? (" SELECTED") : ("")) .">print something here\n"; Link to comment https://forums.phpfreaks.com/topic/42742-populate-droplist-upon-page-load/#findComment-208115 Share on other sites More sharing options...
rbragg Posted March 15, 2007 Author Share Posted March 15, 2007 I wanted the droplist to display the description for the options on its own. So, if "closed" is stored in the db, the droplist should be defaulted to "closed" with the option to change to open. Hope I'm making sense. Link to comment https://forums.phpfreaks.com/topic/42742-populate-droplist-upon-page-load/#findComment-208222 Share on other sites More sharing options...
boo_lolly Posted March 15, 2007 Share Posted March 15, 2007 i've been confused this whole time as to what you want to do. <select name="drop11" id="11" class="style1"> <option value="open"<?php ((isset($_SESSION['drop11']) && $_SESSION['drop11'] == "open") ? (" SELECTED") : ("")); ?>>open <option value="closed"<?php ((isset($_SESSION['drop11']) && $_SESSION['drop11'] == "closed") ? (" SELECTED") : ("")); ?>>closed </select> i'm pretty sure that's what you want. Link to comment https://forums.phpfreaks.com/topic/42742-populate-droplist-upon-page-load/#findComment-208329 Share on other sites More sharing options...
rbragg Posted March 15, 2007 Author Share Posted March 15, 2007 Well, that's exactly what I had at the beginning. That will give me sticky values in case the user presses submit, fails validation and returns to the page. This is what I want along with: Upon the first visit to the page, I want the user to see, as the default value, the value that is currently stored in the database. That value will either be "open" or "closed". So, if in the database a lot is closed, the droplist will say "closed" but they can use the drop-down to see "open" if they wish. Link to comment https://forums.phpfreaks.com/topic/42742-populate-droplist-upon-page-load/#findComment-208346 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.