Jump to content

Populate an arrayed dropdown when clicked


shaung

Recommended Posts

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 by shaung
Link to comment
Share on other sites

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..

Link to comment
Share on other sites

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);
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.