suttercain Posted November 28, 2007 Share Posted November 28, 2007 Hi guys, The below code is used to edit a record. The select menu should automatically select the record that is in the MySQL table... so if 'Modern' is in the table... 'Modern' will already be selected. Here is the code for two select menu... one HTML version which just shows 'Select One' on page load, and then a PHP version THAT WORKS... <select name="ud_age"> <option value='0' selected>Select One</option> <option value='Golden'>Golden</option> <option value='Silver'>Silver</option> <option value='Bronze'>Bronze</option> <option value='Modern'>Modern</option> </select> <?php $TheList = array( "Select Twe" => 0, "Golden" => "Golden", "Silver" => "Silver", "Bronze" => "Bronze", "Modern" => "Modern"); echo "<select name='ud_age'>"; foreach($TheList as $K => $V) { if ($V == $age) { $sel = "selected"; } else { $sel = ""; } echo "<option value='$V' $sel>$K</option>"; } echo "</select>"; ?> So when I remove the HTML select menu, because I am trying to replace it with the dynamic one, it no longer works... it no longer selects the item in the MySQL cell, it now defaults to the 'Select Twe' What happening when I take out the HTML version? I can't get it.. Thanks Quote Link to comment Share on other sites More sharing options...
Daukan Posted November 28, 2007 Share Posted November 28, 2007 Try <?php $sel = 'selected="selected"'; ?> Quote Link to comment Share on other sites More sharing options...
suttercain Posted November 28, 2007 Author Share Posted November 28, 2007 Daiken, Thanks but it didn't solve the issue. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted November 28, 2007 Share Posted November 28, 2007 what is your testing variable defined as?? $age Quote Link to comment Share on other sites More sharing options...
suttercain Posted November 28, 2007 Author Share Posted November 28, 2007 $age = mysql_result($result, $i, 'age'); age is the name of the column. Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted November 28, 2007 Share Posted November 28, 2007 This works fine for me <?php $TheList = array( "Select Twe" => 0, "Golden" => "Golden", "Silver" => "Silver", "Bronze" => "Bronze", "Modern" => "Modern"); $age = 'Silver'; echo "<select name='ud_age'>"; foreach($TheList as $K => $V){ if ($V == $age) { $sel = "selected"; } else { $sel = ""; } echo "<option value='$V' $sel>$K - $V</option>"; } echo "</select>"; ?> Check your value from the database ($age). Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted November 28, 2007 Share Posted November 28, 2007 echo it out pre the loop to verify the value matches Quote Link to comment Share on other sites More sharing options...
suttercain Posted November 28, 2007 Author Share Posted November 28, 2007 Hi guys, Thanks for the replies. I echoed $age and it printed Modern. I checked the value in the specific column 'age' and it also states Modern. Like, I said works fine when I leave the HTML select menu in... but when I remove it, it stops working. Very weird Quote Link to comment Share on other sites More sharing options...
suttercain Posted November 28, 2007 Author Share Posted November 28, 2007 Hi Poco, I copied your code and tried it, leaving the variable $age = 'Silver' and it selected Select Twe on page load. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted November 28, 2007 Share Posted November 28, 2007 can we rewrite your array a bit, you should really use a Integer or ENum in mysql as its less storage, I used in this case an integer in mysql with the array acting like enum $TheList = array("Select Twe", "Golden", "Silver", "Bronze", "Modern"); echo "<select name='ud_age'>"; foreach($TheList as $key => $value){ echo "<option value=\"".$key."\" "; if ($value == $age) { echo "selected=\"selected\""; } echo ">".$value."</option>"; } echo "</select>"; ?> cuts down some storage space Quote Link to comment Share on other sites More sharing options...
suttercain Posted November 28, 2007 Author Share Posted November 28, 2007 Hi cooldude, thanks for taking sometime with to post that code. I tried this $TheList = array("Select Twe", "Golden", "Silver", "Bronze", "Modern"); $age = 2; echo "<select name='ud_age'>"; foreach($TheList as $key => $value){ echo "<option value=\"".$key."\" "; if ($value == $age) { echo "selected=\"selected\""; } echo ">".$value."</option>"; } echo "</select>"; and the select menu still went to Select Twe Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted November 28, 2007 Share Posted November 28, 2007 can i see the html output it isn't going anywhere its selected the xhtml dfeault of the first item if no selected is choosen meaning no matches Quote Link to comment Share on other sites More sharing options...
suttercain Posted November 28, 2007 Author Share Posted November 28, 2007 Sure, Also I have the doctype at HTML <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"><html lang="en"> Here is the source code in FireFox <select name="ud_age"><option value="0">Select Twe</option><option value="1">Golden</option><option value="2">Silver</option><option value="3">Bronze</option><option value="4">Modern</option></select> Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted November 28, 2007 Share Posted November 28, 2007 yeah its not reverting back to the first because its selected its cause its first that is all. my method isn't going to work unless you change it all to integers so use this, and don't convert age keep it text based for now to prove it works, then you should switch em all to integer or enum <?php $age = "Golden"; $TheList = array("Select Twe", "Golden", "Silver", "Bronze", "Modern"); echo "<select name='ud_age'>"; foreach($TheList as $key => $value){ echo "<option value=\"".$key."\" "; if ($value == $age) { echo "selected=\"selected\""; } echo ">".$value."</option>"; } echo "</select>"; ?> Quote Link to comment Share on other sites More sharing options...
suttercain Posted November 28, 2007 Author Share Posted November 28, 2007 Hi Cooldude, I used the above code and I still got the Select Twe. I am going nuts on this one... what I don't understand is why it works when I use the code I posted on my first post? Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted November 28, 2007 Share Posted November 28, 2007 worked for me http://pira00.worldispnetwork.com/see.php Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted November 28, 2007 Share Posted November 28, 2007 Your page is kinda messed up cooldude...but it worked for me also. I have no idea whats going on for you suttercain. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted November 28, 2007 Share Posted November 28, 2007 i justed pasted the php next to it to prove that is what is running Quote Link to comment Share on other sites More sharing options...
suttercain Posted November 28, 2007 Author Share Posted November 28, 2007 Here are the results from my first post... http://www.supermandatabase.com/select.php The second box works. I rmove the html... no longer works... Mystery Quote Link to comment Share on other sites More sharing options...
suttercain Posted November 28, 2007 Author Share Posted November 28, 2007 Thinking maybe the mornign group may have an answer as to why this works okay when I leave the HTML version in the php file, but when I remove it, it no longer works. Here is the full code and the URL: <?php require ('includes/get_connected.php'); $query = "SELECT * FROM comics WHERE comic_id= '45'"; $result = mysql_query($query); $num = mysql_num_rows($result); $i = 0; while ($i<$num) { $age = mysql_result($result, $i, 'age'); ?> <select name="ud_age"> <option value='0' selected>Select One</option> <option value='Golden'>Golden</option> <option value='Silver'>Silver</option> <option value='Bronze'>Bronze</option> <option value='Modern'>Modern</option> </select> <?php $TheList = array( "Select Twe" => 0, "Golden" => "Golden", "Silver" => "Silver", "Bronze" => "Bronze", "Modern" => "Modern"); echo "<select name='ud_age'>"; foreach($TheList as $K => $V) { if ($V == $age) { $sel = "selected"; } else { $sel = ""; } echo "<option value='$V' $sel>$K</option>"; } echo "</select>"; ++$i; } ?> Here is the results. Notice how the HTML precedes the Dynamic list which work, I remove the HTML it stops working. http://www.supermandatabase.com/select.php I wanted to add this in case it helps discover the problem... Source Code with HTML Select Box left in: <select name="ud_age"> <option value='0' selected>Select One</option> <option value='Golden'>Golden</option> <option value='Silver'>Silver</option> <option value='Bronze'>Bronze</option> <option value='Modern'>Modern</option> </select> <select name='ud_age'> <option value='0' selected>Select Twe</option> <option value='Golden' >Golden</option> <option value='Silver' >Silver</option> <option value='Bronze' >Bronze</option> <option value='Modern' selected>Modern</option> </select> Source code with only the dynamic select box left in: <select name='ud_age'> <option value='0'>Select Twe</option> <option value='Golden' >Golden</option> <option value='Silver' >Silver</option> <option value='Bronze' >Bronze</option> <option value='Modern' selected>Modern</option> </select> What is weird is how when I leave the html code in, the selected appears twice in the dynamic menu (see first source code). Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted November 28, 2007 Share Posted November 28, 2007 Okay, try this code and post us the link to where we can view it as well. <?php $TheList = array( "Select Twe" => 0, "Golden" => "Golden", "Silver" => "Silver", "Bronze" => "Bronze", "Modern" => "Modern"); echo "<select name='ud_age'>"; foreach($TheList as $K => $V) { if ($V == $age) { $sel = "selected"; } else { $sel = ""; } echo "<option value='$V' $sel>$K - $V - $age</option>"; } echo "</select>"; ++$i; } ?> Quote Link to comment Share on other sites More sharing options...
suttercain Posted November 28, 2007 Author Share Posted November 28, 2007 Hi poco, here is the result: http://www.supermandatabase.com/selectpoco.php Note: I removed ++$i; } from your code to get it to work. Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted November 28, 2007 Share Posted November 28, 2007 BAM! There is your problem. Notice how your $age variable is empty? So your probably not getting the results from the database properly. Can you post more of you code? Post the query and everything...I need to see what your doing with that variable. Quote Link to comment Share on other sites More sharing options...
suttercain Posted November 28, 2007 Author Share Posted November 28, 2007 Hi poco, This is the entire code <?php require ('includes/get_connected.php'); $query = "SELECT * FROM comics WHERE comic_id= '45'"; $result = mysql_query($query); $num = mysql_num_rows($result); $i = 0; while ($i<$num) { $age = mysql_result($result, $i, 'age'); ?> <select name="ud_age"> <option value='0' selected>Select One</option> <option value='Golden'>Golden</option> <option value='Silver'>Silver</option> <option value='Bronze'>Bronze</option> <option value='Modern'>Modern</option> </select> <?php $TheList = array( "Select Twe" => 0, "Golden" => "Golden", "Silver" => "Silver", "Bronze" => "Bronze", "Modern" => "Modern"); echo "<select name='ud_age'>"; foreach($TheList as $K => $V) { if ($V == $age) { $sel = "selected"; } else { $sel = ""; } echo "<option value='$V' $sel>$K</option>"; } echo "</select>"; ++$i; } ?> Notice $age = mysql_result($result, $i, 'age'); in the code. When I echo $age it echoes modern. In your code no variable to age was set. Quote Link to comment Share on other sites More sharing options...
suttercain Posted November 28, 2007 Author Share Posted November 28, 2007 Here is your code, but I added the $age variable to "Golden" <?php $TheList = array( "Select Twe" => 0, "Golden" => "Golden", "Silver" => "Silver", "Bronze" => "Bronze", "Modern" => "Modern"); $age = "Golden"; echo "<select name='ud_age'>"; foreach($TheList as $K => $V) { if ($V == $age) { $sel = "selected"; } else { $sel = ""; } echo "<option value='$V' $sel>$K - $V - $age</option>"; } echo "</select>"; ?> The output is here: http://www.supermandatabase.com/selectpoco.php 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.