bluwe Posted November 29, 2006 Share Posted November 29, 2006 I want to retrieve a record from my database and display the record as the default choice in a html drop down menu - is this possible?Cheers... Quote Link to comment Share on other sites More sharing options...
gluck Posted November 29, 2006 Share Posted November 29, 2006 It is possible. You just need to add details of what specifics you want Quote Link to comment Share on other sites More sharing options...
bluwe Posted December 1, 2006 Author Share Posted December 1, 2006 Thanks for replying. I've got a title column in my customer details table with the following options: Mr, Mrs, Miss, Ms, Dr. When a user edits the customer details record it is obviously retireved from the db using a SELECT statement and displayed in a html table. I want to populate the first entry in the title drop down menu in the table with the value returned from the db, but still have the other options available. If you see what I mean!Cheers! Quote Link to comment Share on other sites More sharing options...
keeB Posted December 1, 2006 Share Posted December 1, 2006 [code=php:0]<select name="zomg"> <option name="ms">Ms.</option> <option name="dr" selected>Dr.</option> <option name="mr">Mr.</option></select>[/code]Notice the "selected" attribute in <option> Quote Link to comment Share on other sites More sharing options...
craygo Posted December 1, 2006 Share Posted December 1, 2006 this will do it[code]<select name=title><?php$title = array("Mr", "Mrs", "Miss", "Ms", "Dr.");foreach($title as $tname){ if($tname == $dbvariable){ echo "<option value=$title selected>$title</option>"; } else { echo "<option value=$title>$title</option>"; }}?></select>[/code]Ray Quote Link to comment Share on other sites More sharing options...
bluwe Posted December 1, 2006 Author Share Posted December 1, 2006 That's excellent! Thanks you all very much!!! Quote Link to comment Share on other sites More sharing options...
bluwe Posted December 3, 2006 Author Share Posted December 3, 2006 Thanks for the start Ray, needed a bit of debugging though, but finally got it working like so...[code]<select name=title><?php$dbvariable = "Miss";$title = array ("Mr", "Mrs", "Miss", "Ms", "Dr");foreach($title as $tname){ if($tname == $dbvariable){ echo "<option value=$dbvariable selected>$dbvariable</option>"; } else { echo "<option value=$title>$tname</option>"; }}echo "</select>";?>[/code] Quote Link to comment Share on other sites More sharing options...
bluwe Posted December 3, 2006 Author Share Posted December 3, 2006 Actually the above doesn't work, hmm.... Quote Link to comment Share on other sites More sharing options...
bluwe Posted December 3, 2006 Author Share Posted December 3, 2006 This deffo works though...[code]<select name=title><?php$dbvariable = "Dr";$title = array ("Mr", "Mrs", "Miss", "Ms", "Dr");foreach($title as $tname){ if($tname == $dbvariable){ echo "<option value=$tname selected>$dbvariable</option>"; }}foreach($title as $tname){ if($tname !=$dbvariable){ echo "<option value=$title>$tname</option>";}}echo "</select>";?>[/code] Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted December 3, 2006 Share Posted December 3, 2006 Actually your were closer with this:[code]<?php<select name=title><?php$dbvariable = "Miss";$title = array ("Mr", "Mrs", "Miss", "Ms", "Dr");foreach($title as $tname){ if($tname == $dbvariable){ echo "<option value=$dbvariable selected>$dbvariable</option>"; } else { echo "<option value=$title>$tname</option>"; }}echo "</select>";?>[/code]The problem is that you can't use the variable "$title" as a value, since all that will give you is the string "Array".Try this instead:[code]<?php<select name=title><?php$dbvariable = "Miss";$title = array ("Mr", "Mrs", "Miss", "Ms", "Dr");foreach($title as $tname){ $sel = ($tname == $dbvariable)?' selected':''; // sets $sel to either selected or nothing echo '<option value="' . $tmame . '"' . $sel . 'selected>' . $tname. "</option><br>"; // make sure all values of attributes are quoted }echo "</select>";?>[/code]Ken Quote Link to comment Share on other sites More sharing options...
craygo Posted December 3, 2006 Share Posted December 3, 2006 SHIT good catch ken. I buzzed though it so quick I put the wrong one in[code]<select name=title><?php$title = array("Mr", "Mrs", "Miss", "Ms", "Dr.");foreach($title as $tname){ if($tname == $dbvariable){ echo "<option value=$tname selected>$tname</option>"; } else { echo "<option value=$tname>$tname</option>"; }}?></select>[/code]Ray Quote Link to comment Share on other sites More sharing options...
bluwe Posted December 4, 2006 Author Share Posted December 4, 2006 Thnaks for all your help, much appreciated!! 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.