harkly Posted November 30, 2009 Share Posted November 30, 2009 I have a form that has a drop down, it updates the database with a number and then when that user calls the form again it will have their option already selected. What I am wondering is if there is a more efficient way of doing this? another of my drop downs has more than 30 options. I will need to change to another format for time issues, at least I think. if ($education == 2){ echo " <select name='education' size='1' id='st_age' class='selectOne'> <option value='1'></option> <option value='2' selected='selected'>Some High school</option> <option value='3'>High school/GED</option> <option value='4'>Some college</option> <option value='5'>Associates</option> <option value='6'>Bachelors</option> <option value='7'>Masters</option> <option value='8'>PhD / Post Doctoral</option> </select> \n";} else if ($education == 3){ echo " <select name='education' size='1' id='st_age' class='selectOne'> <option value='1'></option> <option value='2'>Some High school</option> <option value='3' selected='selected'>High school/GED</option> <option value='4'>Some college</option> <option value='5'>Associates</option> <option value='6'>Bachelors</option> <option value='7'>Masters</option> <option value='8'>PhD / Post Doctoral</option> </select> \n";} else if ($education == 4){ echo " <select name='education' size='1' id='st_age' class='selectOne'> <option value='1'></option> <option value='2'>Some High school</option> <option value='3'>High school/GED</option> <option value='4' selected='selected'>Some college</option> <option value='5'>Associates</option> <option value='6'>Bachelors</option> <option value='7'>Masters</option> <option value='8'>PhD / Post Doctoral</option> </select> \n";} else if ($education == 5){ echo " <select name='education' size='1' id='st_age' class='selectOne'> <option value='1'></option> <option value='2'>Some High school</option> <option value='3'>High school/GED</option> <option value='4'>Some college</option> <option value='5' selected='selected'>Associates</option> <option value='6'>Bachelors</option> <option value='7'>Masters</option> <option value='8'>PhD / Post Doctoral</option> </select> \n";} else if ($education == 6){ echo " <select name='education' size='1' id='st_age' class='selectOne'> <option value='1'></option> <option value='2'>Some High school</option> <option value='3'>High school/GED</option> <option value='4'>Some college</option> <option value='5'>Associates</option> <option value='6' selected='selected'>Bachelors</option> <option value='7'>Masters</option> <option value='8'>PhD / Post Doctoral</option> </select> \n";} else if ($education == 7){ echo " <select name='education' size='1' id='st_age' class='selectOne'> <option value='1'></option> <option value='2'>Some High school</option> <option value='3'>High school/GED</option> <option value='4'>Some college</option> <option value='5'>Associates</option> <option value='6'>Bachelors</option> <option value='7' selected='selected'>Masters</option> <option value='8'>PhD / Post Doctoral</option> </select> \n";} else if ($education == { echo " <select name='education' size='1' id='st_age' class='selectOne'> <option value='1'></option> <option value='2'>Some High school</option> <option value='3'>High school/GED</option> <option value='4'>Some college</option> <option value='5'>Associates</option> <option value='6'>Bachelors</option> <option value='7'>Masters</option> <option value='8' selected='selected'>PhD / Post Doctoral</option> </select> \n";} else if ($education == 1){ echo " <select name='education' size='1' id='st_age' class='selectOne'> <option value='1' selected='selected'></option> <option value='2'>Some High school</option> <option value='3'>High school/GED</option> <option value='4'>Some college</option> <option value='5'>Associates</option> <option value='6'>Bachelors</option> <option value='7'>Masters</option> <option value='8'>PhD / Post Doctoral</option> </select> \n";} else { echo " <select name='education' size='1' id='st_age' class='selectOne'> <option value='1' selected='selected'></option> <option value='2'>Some High school</option> <option value='3'>High school/GED</option> <option value='4'>Some college</option> <option value='5'>Associates</option> <option value='6'>Bachelors</option> <option value='7'>Masters</option> <option value='8'>PhD / Post Doctoral</option> </select> \n";} echo " Link to comment https://forums.phpfreaks.com/topic/183467-form-with-options/ Share on other sites More sharing options...
mrMarcus Posted November 30, 2009 Share Posted November 30, 2009 something like: <?php $edu_arr = array ( 1 => 'Select One', 2 => 'Some High school', 3 => 'High school/GED', 4 => 'Some college', 5 => 'Associates', 6 => 'Bachelors', 7 => 'Masters', 8 => 'PhD / Post Doctoral' ); echo '<select name="education" size="1" id="st_age" class="selectOne">'; foreach ($edu_arr as $key => $value) { echo '<option value="'.$key.'"'.(($key == $education) ? ' selected="selected"' : '').'>'.$value.'</option>'; } } echo '</select>'; ?> EDIT: you can swap out the value 'Select One' and have an empty value '', or you can change it to whatever you like. Link to comment https://forums.phpfreaks.com/topic/183467-form-with-options/#findComment-968442 Share on other sites More sharing options...
harkly Posted December 1, 2009 Author Share Posted December 1, 2009 Thanks! I will give this a try tonight! Link to comment https://forums.phpfreaks.com/topic/183467-form-with-options/#findComment-969087 Share on other sites More sharing options...
harkly Posted December 2, 2009 Author Share Posted December 2, 2009 I can't seem to get anything but a blank page. Right now my $education = 3 <?php include('library/login.php'); login(); mysql_select_db('bariatric'); $search=$_GET['username']; $result = mysql_query("SELECT * FROM about_me WHERE username = 'kelly'"); while ($r=mysql_fetch_array($result)) { $username=$r["username"]; $education=$r["education"]; } $edu_arr = array ( 1 => 'Select One', 2 => 'Some High school', 3 => 'High school/GED', 4 => 'Some college', 5 => 'Associates', 6 => 'Bachelors', 7 => 'Masters', 8 => 'PhD / Post Doctoral' ); echo " <select name='education' size='1' id='st_age' class='selectOne'>"; foreach ($edu_arr as $key => $value) { echo " <option value="'.$key.'"'.(($key == $education) ? ' selected="selected"' : '').'>'.$value.'</option>'"; } echo "</select>"; ?> Am I missing something outside of the array?? Link to comment https://forums.phpfreaks.com/topic/183467-form-with-options/#findComment-969398 Share on other sites More sharing options...
harkly Posted December 2, 2009 Author Share Posted December 2, 2009 Whew! I was able to get it to work so that it will display what is currently in the database but it wont update the database when something else is choosen. Can anyone help me on that? <?php include('library/login.php'); login(); mysql_select_db('database'); $search=$_GET['username']; $result = mysql_query("SELECT * FROM about_me WHERE username = 'test'"); while ($r=mysql_fetch_array($result)) { $username=$r["username"]; $education=$r["education"]; } $states = array( 1 => '', 2 => 'Some High school', 3 => 'High school/GED', 4 => 'Some college', 5 => 'Associates', 6 => 'Bachelors', 7 => 'Masters', 8 => 'PhD / Post Doctoral' ); $select = '<select name="state" id="state" size="1">'."\r\n"; foreach($states as $key => $value){ $select .= "\t".'<option value="'.$key.'"'.(($key == $education) ? ' selected="selected"' : '').'>'. $value.'</option>'."\r\n"; } $select .= '</select>'; echo $select; ?> Link to comment https://forums.phpfreaks.com/topic/183467-form-with-options/#findComment-969410 Share on other sites More sharing options...
mrMarcus Posted December 2, 2009 Share Posted December 2, 2009 is your <select> within a form? is it being processed? Link to comment https://forums.phpfreaks.com/topic/183467-form-with-options/#findComment-969412 Share on other sites More sharing options...
harkly Posted December 2, 2009 Author Share Posted December 2, 2009 Yes, its within the form. Here is a full version. <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'> <html xmlns='http://www.w3.org/1999/xhtml'> <head> <link rel='stylesheet' type='text/css' href='app.css'> <link href='wforms-jsonly.css' type='text/css' rel='alternate stylesheet' title='this stylesheet is activated by javascript' /> <script type='text/javascript' src='wforms.js' ></script> </head> <body> <?php include('library/login.php'); login(); ?> <div id='wrapperLifestyle'> <div id='header'><a href='index.html'><img src='img/redLogo.gif' border='0'></a></div> <div id='aboutLifestyle'> <span class='right'> <?php mysql_select_db('bariatric'); $search=$_GET['username']; $result = mysql_query("SELECT * FROM about_me WHERE username = 'kelly'"); while ($r=mysql_fetch_array($result)) { $username=$r["username"]; $education=$r["education"]; } echo " <form method='post' action='updateAboutMe.php' name='test' id='test'> <fieldset> <div id='aboutForm'> <span class='headerbox'><b>Education level:</b></span> <span class='textbox'> \n"; $states = array( 1 => '', 2 => 'Some High school', 3 => 'High school/GED', 4 => 'Some college', 5 => 'Associates', 6 => 'Bachelors', 7 => 'Masters', 8 => 'PhD / Post Doctoral' ); $select = '<select name="state" id="state" size="1">'."\r\n"; foreach($states as $key => $value){ $select .= "\t".'<option value="'.$key.'"'.(($key == $education) ? ' selected="selected"' : '').'>'. $value.'</option>'."\r\n"; } $select .= '</select>'; echo $select; echo " </span> </div> </fieldset> <fieldset> <div id='aboutForm'> <span class='button'> <input type='image' value='Update' src='img/button_update.gif' width='75' height='25' onmouseover='javascript:this.src='img/button_update2.gif';' onmouseout='javascript:this.src='img/button_update.gif';'> </span> </div> <fieldset> </form> </div> </span> </form> \n"; ?> </body></html> Is this line passing the key into the value? because I think that is what I need $select .= "\t".'<option value="'.$key.'"'.(($key == $education) ? ' selected="selected"' : '').'>'. $value.'</option>'."\r\n"; Link to comment https://forums.phpfreaks.com/topic/183467-form-with-options/#findComment-969431 Share on other sites More sharing options...
harkly Posted December 2, 2009 Author Share Posted December 2, 2009 Got it to work!! $states = array( 1 => '', 2 => 'Some High school', 3 => 'High school/GED', 4 => 'Some college', 5 => 'Associates', 6 => 'Bachelors', 7 => 'Masters', 8 => 'PhD / Post Doctoral' ); $select = '<select name="education" id="st_age" size="1" class="selectOne">'."\r\n"; foreach($states as $key => $value){ $select .= "\t".'<option value="'.$key.'"'.(($key == $education) ? ' selected="selected"' : '').'>'. $value.'</option>'."\r\n"; } $select .= '</select>'; echo $select; Link to comment https://forums.phpfreaks.com/topic/183467-form-with-options/#findComment-969450 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.