jwmiller Posted May 7, 2011 Share Posted May 7, 2011 I have a simple form that edits student information. I have several fields that I have a drop down list pulled as an array from a separate table. I would like the information that is already entered on the student to be the default but still have the drop down list available if the item was entered in error. For example: Drop down list of all the states of America, OH was entered for the student, when the edit form comes up for State in Address I would like OH to be the default but still have the list to choose if OH was entered in error. I have tried selected = $selected but then the entire drop down changed to OH. I am very, very much a beginner in this <?php // list of possible modules comes from database $state_array=get_states(); //display as a drop down list for end user to choose if (!is_array($state_array)) { echo "<p>No States are currently available<p>"; return; } foreach ($state_array as $row) { echo "<option value=\"".($row['stateid'])."\""; // if existing state, put in current catgory echo "<strong> ".($row['stateid'])."</strong><br />"; echo "<option select=\"selected\">OH"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/235746-how-do-i-set-a-default-variable-from-an-edit-form-that-is-using-php-array-list/ Share on other sites More sharing options...
xyph Posted May 7, 2011 Share Posted May 7, 2011 <?php $c = 3; $a = array( 1 => 'Option 1', 2 => 'Option 2', 3 => 'Option 3', 4 => 'Option 4', ); echo '<select name="name">'; foreach ($a as $k => $v ) echo '<option value="'.$k.'"'.($k == $c ? ' selected="1"':'').'>'.$v.'</option>'; echo '</select>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/235746-how-do-i-set-a-default-variable-from-an-edit-form-that-is-using-php-array-list/#findComment-1211779 Share on other sites More sharing options...
jwmiller Posted May 7, 2011 Author Share Posted May 7, 2011 Thank you for your reply, I am still not real clear but I will copy your code and see if I can make it work - so i can understand your logic. I was not real clear in MY coding - the OH is really coming over as a variable $studstate - I just hard coded it in the loop to see if I could get anything to default to the information that is already stored in the students database. Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/235746-how-do-i-set-a-default-variable-from-an-edit-form-that-is-using-php-array-list/#findComment-1211899 Share on other sites More sharing options...
jwmiller Posted May 7, 2011 Author Share Posted May 7, 2011 I ran your code and that is exactly what I need to happen, however I am stuck trying to translate the way you wrote your code to my way So if I have the variable returned $studstate (this has OH stored in this field in the students table and I returned that text and stored it in $studstate) I am also getting the table from the states table and returning as an array See below How do I translate your option set up : foreach ($a as $k => $v ) echo '<option value="'.$k.'"'.($k == $c ? ' selected="1"':'').'>'.$v.'</option>'; to minic my set up below. That is now where I am stuck. I am getting confused between my [] and your => as well as where exactly I can place -- as well as create the ($row==$studstate ? selected ) statement $studstate='OH' foreach ($state_array as $row) { echo "<option value=\"".($row['stateid'])."\""; // if existing state, put in current catgory echo "<strong> ".($row['stateid'])."</strong><br />"; } Quote Link to comment https://forums.phpfreaks.com/topic/235746-how-do-i-set-a-default-variable-from-an-edit-form-that-is-using-php-array-list/#findComment-1211909 Share on other sites More sharing options...
jwmiller Posted May 7, 2011 Author Share Posted May 7, 2011 HA, got it THANK YOU SO MUCH. This was the piece of the puzzle I needed. This was my final echo option statement echo '<option value="'.($row['stateid']).'"'.(($row['stateid'])==$studstate ? 'selected="1"':'').'>'; and it is exactly what I needed. So How do I mark this as solved? Quote Link to comment https://forums.phpfreaks.com/topic/235746-how-do-i-set-a-default-variable-from-an-edit-form-that-is-using-php-array-list/#findComment-1211916 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.