Kristoff1875 Posted May 24, 2013 Share Posted May 24, 2013 (edited) I have a form that has 2 select menus, I was hoping I could pre-populate the menus using the fields that exist in the table of the database, is this possible? I've created a dropdown, using the following: /* Get data. */ $sql = "SELECT DISTINCT Area FROM Users ORDER BY Area ASC"; $result = mysql_query($sql1); while($row = mysql_fetch_array($result)) { '<option value="'.$row['Area'].'"> '.$row['Area'].' </option>'; } With <select> tags wrapped around it works just how I wanted it to, apart from it not being involved with the second dropdown: $sql1 = "SELECT * FROM Users WHERE UserID = $UserID"; $result1 = mysql_query($sql1); while($row1 = mysql_fetch_array($result1)) { <select name="Area" id="Area" class="dropdown" style="width:260px;"> <option> Any </option> <option <?php if ($row1['Area']=='London') { echo 'selected="SELECTED"'; } ?> value="London"> London </option> <option <?php if ($row1['Area']=='Birmingham') { echo 'selected="SELECTED"'; } ?> value="Birmingham"> Birmingham </option> </select> } Is there a way of integrating the first result in to the second? Or even better, integrating them in to one query? I wasn't sure if that was possible as i'm searching for one row in the second query. Thanks in advance. Edited May 24, 2013 by Kristoff1875 Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted May 24, 2013 Share Posted May 24, 2013 your post doesn't identify what result you want (i'm guessing you want to preselect the area of the current user when producing the first drop-down select menu.) Quote Link to comment Share on other sites More sharing options...
Kristoff1875 Posted May 24, 2013 Author Share Posted May 24, 2013 Sorry, yes I'd like to end up with the select box in the second query: <select name="Area" id="Area" class="dropdown" style="width:260px;"> <option> Any </option> <option <?php if ($row1['Area']=='London') { echo 'selected="SELECTED"'; } ?> value="London"> London </option> <option <?php if ($row1['Area']=='Birmingham') { echo 'selected="SELECTED"'; } ?> value="Birmingham"> Birmingham </option> </select> With the results from the first, so: <option <?php if ($row1['Area']=='***RESULT FROM QUERY 1***') { echo 'selected="SELECTED"'; } ?> value="***RESULT FROM QUERY 1***"> ***RESULT FROM QUERY 1*** </option> To for the second if possible. Quote Link to comment Share on other sites More sharing options...
Solution DavidAM Posted May 24, 2013 Solution Share Posted May 24, 2013 Since you are only going to find ONE row in the second snippet, there is no need for a loop there. So, you can do the Area logic after retrieving that record: $sql1 = "SELECT * FROM Users WHERE UserID = $UserID"; $result1 = mysql_query($sql1); $row1 = mysql_fetch_array($result1); // Add area LOGIC HERE I usually have a function for this purpose. Something like: function getAreaOptions($psSelected = '') { $out = array(); $sql = "SELECT DISTINCT Area FROM Users ORDER BY Area ASC"; $result = mysql_query($sql); while($row = mysql_fetch_array($result)) { $out[] = sprintf('<OPTION %s value="%s">%s</OPTION>', ($row['Area'] == $psSelected ? 'SELECTED="selected"' : ''), $row['Area'], $row['Area']); } return implode(PHP_EOL, $out); }Then call it from your user code: $sql1 = "SELECT * FROM Users WHERE UserID = $UserID"; $result1 = mysql_query($sql1); $row1 = mysql_fetch_array($result1); echo '<select name="Area" id="Area" class="dropdown" style="width:260px;"> <option> Any </option>'; echo getAreaOptions($row1['Area']); echo '</select>'; Quote Link to comment Share on other sites More sharing options...
Kristoff1875 Posted May 24, 2013 Author Share Posted May 24, 2013 Absolutely brilliant. Thank you! Quote Link to comment Share on other sites More sharing options...
Kristoff1875 Posted May 24, 2013 Author Share Posted May 24, 2013 (edited) Scrap that. Sussed it. Cheers! Edited May 24, 2013 by Kristoff1875 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.