shaung Posted December 10, 2013 Share Posted December 10, 2013 (edited) Hi, I have a 2d array of form elements in a table, as seen here. Among those elements are dropdown lists. One shows college names and the other shows what degree that person has. These are initially populated from a database with the single value pertaining to that particular record. If they click the dropdown, I wish to populate it with all the options from that table, from which they can choose one. Example: User logs in and his/her record shows as an editable table row that has two dropdowns. One dropdown shows the college they attended, and the other shows the degree they earned. If they click the 'degree' dropdown, it populates with all the degree options contained in that particular database table - from which they can choose whatever they want. I understand the database part and how to fill the dropdown with options, but I am unsure of how to know which dropdown was clicked, in that particular row, so I can fill it with options. When a faculty member uses the page, he/she will only see their single record BUT when an administrator uses it they will see all the records, which they may have to edit as necessary. How do I know which dropdown has been clicked so I can populate it? Here is the code where I have the dropdowns: foreach ($arrValues as $row){ $id = $row['employee_id']; echo <<<FORM_FIELDS <tr> <td><input type="text" name="record[$id][firstName]" value="{$row['first_name']}" /></td> <td><input type="text" name="record[$id][lastName]" value="{$row['last_name']}" /></td> <td><input type="text" name="record[$id][height]" value="{$row['height']}" /></td> <td><input type="text" name="record[$id][cap]" value="{$row['cap']}" /></ td> <td><input type="text" name="record[$id][color]" value="{$row['colors']}" /></td> //shows degree type <td><select name="record[$id][degree]" </td> <option>{$row['type']}</option> </select> //shows school attended <td><select name="record[$id][school]" </td> <option>{$row['name']}</option> </select>' <td><input type="submit" name="update" value="Update" /></td> </tr> FORM_FIELDS; } Thanks Edited December 10, 2013 by shaung Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted December 10, 2013 Share Posted December 10, 2013 (edited) you would make the select menu with all the choices, but pre-select the option entry that matches the value from each the current record. Edited December 10, 2013 by mac_gyver Quote Link to comment Share on other sites More sharing options...
shaung Posted December 10, 2013 Author Share Posted December 10, 2013 you would make the select menu with all the choices, but pre-select the option entry that matches the value from each the current record. So populate every dropdown when loading the table? I was just worried that doing that would take a long time load. That certainly would be easier to implement, Will post code here when I get it working. Thanks.. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted December 10, 2013 Share Posted December 10, 2013 using php array functions, you can very quickly produce a number of option menus with a dynamic preselected option - $array[1] = array('id'=>1,'name' => 'namea'); $array[2] = array('id'=>2,'name' => 'nameb'); $array[3] = array('id'=>3,'name' => 'namec'); $array[4] = array('id'=>4,'name' => 'named'); function _options($arr){ $selected = isset($arr['selected']) ? " selected='selected'" : ''; return "<option value='{$arr['id']}'$selected>{$arr['name']}</option>"; } $current_options = $array; // get a copy of the list of options // preselect an option(s) - works with any quantity of preselected options in one menu $current_options[2]['selected'] = true; // set an element for a selected option $options = array_map('_options',$current_options); echo implode("\n",$options); 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.