Ugluth Posted November 4, 2008 Share Posted November 4, 2008 Hello there i have a page that edits some records on my database. Usually i'm using textfields to display the current value and just update it into the database and let the user decide which values he wants to change and which he doesn't. The problem is that one of the fields in this table is a foreign key to another table, so the textbox shows the user some primary key which doesn't really help him understand to what he can change it. Anyway here's the form as it is now with textboxes <form id="frmDirector" name="frmDirector" method="post" action="edit_director.php"> <input name="menuDirector" type="hidden" value="<?php echo $_POST['menuDirector']; ?>" /> <?php $query_director = "SELECT * FROM directors WHERE Director_ID = '$director'"; $result_director = mysql_query($query_director) or die("Query failed: " . mysql_error()); while ($line = mysql_fetch_array($result_director, MYSQL_NUM)) { ?> <p>Director ID <input name="txtID" type="text" id="txtID" value="<?php echo $line[0]; ?>" /> </p> <p>Director Name <input name="txtName" type="text" id="txtName" value="<?php echo $line[1]; ?>" /> </p> <p>Nationality [b]<input name="txtNation" type="text" id="txtNation" value="<?php echo $line[2]; ?>" />[/b] </p> <p> <input type="submit" name="Submit2" value="Submit" /> </p> <?php } ?> </form> What i want to do is replace this textbox <input name="txtNation" type="text" id="txtNation" value="<?php echo $line[2]; ?>" /> with a dropdown list that will take all of the records from that table and have as selected the value that this textbox would show. Here's the dropdown list i would use, just without having as selected value the above mentioned. <select name="menuNation"> <option value=" "> </option> <?php $query_menu = "SELECT * FROM nationalities"; $result_menu = mysql_query($query_menu) or die("Query failed: " . mysql_error()); while ($line_menu = mysql_fetch_array($result_menu, MYSQL_NUM)) { echo "<option value=\"$line_menu[0]\""; if ($_POST["menuNation"]==$line_menu[0]) echo " selected" ; echo ">".$line_menu[1]." </option>" ; } ?> </select> Hope this post made sense and thanks in advance! Link to comment https://forums.phpfreaks.com/topic/131369-solved-setting-the-selected-value-of-a-drop-down-list/ Share on other sites More sharing options...
rhodesa Posted November 4, 2008 Share Posted November 4, 2008 try this out: <form id="frmDirector" name="frmDirector" method="post" action="edit_director.php"> <input name="menuDirector" type="hidden" value="<?php echo $_POST['menuDirector']; ?>" /> <?php //Get nationalities $nationalities = array(); $query_menu = "SELECT * FROM nationalities"; $result_menu = mysql_query($query_menu) or die("Query failed: " . mysql_error()); while ($line_menu = mysql_fetch_array($result_menu, MYSQL_NUM)) { $nationalities[$line_menu[0]] = $line_menu[1]; } $query_director = "SELECT * FROM directors WHERE Director_ID = '$director'"; $result_director = mysql_query($query_director) or die("Query failed: " . mysql_error()); while ($line = mysql_fetch_array($result_director, MYSQL_NUM)) { ?> <p>Director ID <input name="txtID" type="text" id="txtID" value="<?php echo $line[0]; ?>" /> </p> <p>Director Name <input name="txtName" type="text" id="txtName" value="<?php echo $line[1]; ?>" /> </p> <p>Nationality <select name="txtNation" id="txtNation"> <option value=" "> </option> <?php foreach($nationalities as $natKey=>$natTitle){ echo "<option value=\"$natKey\""; if ($line[2]==$natKey) echo " selected" ; echo ">".$natTitle." </option>" ; } ?> </select> </p> <p> <input type="submit" name="Submit2" value="Submit" /> </p> <?php } ?> </form> Edit: I moved the Nationalities SELECT statement outside of the loop to prevent redundant queries Link to comment https://forums.phpfreaks.com/topic/131369-solved-setting-the-selected-value-of-a-drop-down-list/#findComment-682230 Share on other sites More sharing options...
Ugluth Posted November 4, 2008 Author Share Posted November 4, 2008 That works, thank you very much!! Link to comment https://forums.phpfreaks.com/topic/131369-solved-setting-the-selected-value-of-a-drop-down-list/#findComment-682231 Share on other sites More sharing options...
rhodesa Posted November 4, 2008 Share Posted November 4, 2008 see edit above... Link to comment https://forums.phpfreaks.com/topic/131369-solved-setting-the-selected-value-of-a-drop-down-list/#findComment-682234 Share on other sites More sharing options...
Ugluth Posted November 4, 2008 Author Share Posted November 4, 2008 Yes i did see it and you posted the solution to my problem and i thanked you i don't see something wrong with that Link to comment https://forums.phpfreaks.com/topic/131369-solved-setting-the-selected-value-of-a-drop-down-list/#findComment-682236 Share on other sites More sharing options...
rhodesa Posted November 4, 2008 Share Posted November 4, 2008 i just wanted to make sure you saw that i edited the post...i moved the nationalities SELECT statement to the top, outside of the main loop. Link to comment https://forums.phpfreaks.com/topic/131369-solved-setting-the-selected-value-of-a-drop-down-list/#findComment-682238 Share on other sites More sharing options...
Ugluth Posted November 4, 2008 Author Share Posted November 4, 2008 Hmm i didn't realise that you actually edited your post, sorry my bad, but the first one you posted works just fine, any specific reason on changing it and putting it outside of the main loop? Performance would be my guess but not that sure, and thank you again! Link to comment https://forums.phpfreaks.com/topic/131369-solved-setting-the-selected-value-of-a-drop-down-list/#findComment-682242 Share on other sites More sharing options...
rhodesa Posted November 4, 2008 Share Posted November 4, 2008 Edit: I moved the Nationalities SELECT statement outside of the loop to prevent redundant queries ...if it's inside, you are performing the same SELECT statement for every entry in the main loop. i just glanced at the code again though...is Director_ID the primary key? aka there is only ever one entry returned? Link to comment https://forums.phpfreaks.com/topic/131369-solved-setting-the-selected-value-of-a-drop-down-list/#findComment-682251 Share on other sites More sharing options...
Ugluth Posted November 4, 2008 Author Share Posted November 4, 2008 Yep actually only one record is supposed to return everytime, so the while is rather pointless, since there's only one result, but just gotten used to using that loop to get records Link to comment https://forums.phpfreaks.com/topic/131369-solved-setting-the-selected-value-of-a-drop-down-list/#findComment-682274 Share on other sites More sharing options...
rhodesa Posted November 4, 2008 Share Posted November 4, 2008 yeah...i would ditch the while loop, then move the SELECT back down: <form id="frmDirector" name="frmDirector" method="post" action="edit_director.php"> <input name="menuDirector" type="hidden" value="<?php echo $_POST['menuDirector']; ?>" /> <?php $query_director = "SELECT * FROM directors WHERE Director_ID = '$director'"; $result_director = mysql_query($query_director) or die("Query failed: " . mysql_error()); $line = mysql_fetch_array($result_director, MYSQL_NUM) or die("No record found"); ?> <p>Director ID <input name="txtID" type="text" id="txtID" value="<?php echo $line[0]; ?>" /> </p> <p>Director Name <input name="txtName" type="text" id="txtName" value="<?php echo $line[1]; ?>" /> </p> <p>Nationality <select name="txtNation" id="txtNation"> <option value=" "> </option> <?php $query_menu = "SELECT * FROM nationalities"; $result_menu = mysql_query($query_menu) or die("Query failed: " . mysql_error()); while ($line_menu = mysql_fetch_array($result_menu, MYSQL_NUM)) { echo "<option value=\"$line_menu[0]\""; if ($line[2]==$line_menu[0]) echo " selected" ; echo ">".$line_menu[1]." </option>" ; } ?> </select> </p> <p> <input type="submit" name="Submit2" value="Submit" /> </p> </form> Link to comment https://forums.phpfreaks.com/topic/131369-solved-setting-the-selected-value-of-a-drop-down-list/#findComment-682275 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.