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... Link to comment https://forums.phpfreaks.com/topic/28907-i-need-some-help/ 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 Link to comment https://forums.phpfreaks.com/topic/28907-i-need-some-help/#findComment-132389 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! Link to comment https://forums.phpfreaks.com/topic/28907-i-need-some-help/#findComment-133638 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> Link to comment https://forums.phpfreaks.com/topic/28907-i-need-some-help/#findComment-133660 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 Link to comment https://forums.phpfreaks.com/topic/28907-i-need-some-help/#findComment-133665 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!!! Link to comment https://forums.phpfreaks.com/topic/28907-i-need-some-help/#findComment-133724 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] Link to comment https://forums.phpfreaks.com/topic/28907-i-need-some-help/#findComment-134287 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.... Link to comment https://forums.phpfreaks.com/topic/28907-i-need-some-help/#findComment-134289 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] Link to comment https://forums.phpfreaks.com/topic/28907-i-need-some-help/#findComment-134295 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 Link to comment https://forums.phpfreaks.com/topic/28907-i-need-some-help/#findComment-134323 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 Link to comment https://forums.phpfreaks.com/topic/28907-i-need-some-help/#findComment-134435 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!! Link to comment https://forums.phpfreaks.com/topic/28907-i-need-some-help/#findComment-134840 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.