1

I have data that comes from a hard-coded array, which goes to a drop-down list, then added to an SQL database. I'm trying to write a function to update the record. On my update record page it populates the data into form fields. However I cannot figure out how to make the drop down lists populate with what is stored for that specific record. I am using the CodeIgniter framework. The dropdown list in question is for the Train Direction field.

Model:

public function edit_recrew()
{
    $this->db->where('train_id', $this->uri->segment(3));
    $query = $this->db->get('trains_road');
    return $query->result();
}

public function update_recrew($data)
{
    $this->db->where('train_id', $data['train_id']);
    $this->db->update('trains_road', $data);
}

View:

<?=
    form_open('atis/update_recrew');
    foreach ($record as $value) {
?>
        <input type="hidden" name="train_id" title="train_id" value="<?php echo $value->train_id; ?>">
        <tr>
            <td>Train Direction:</td>
            <?php
                $train_direction = array('.'$value->train_direction'.' => '.'$value->train_direction'.',
                    'North' => 'North',
                    'South' => 'South',
                    'East' => 'East',
                    'West' => 'West'
                );
            ?>
            <td><?= form_dropdown('txttrain_direction', $train_direction); ?></td>

Controller:

public function update_recrew()
{
    $data = array(
        'train_id' => $this->input->post('train_id'),
        'train_no' => $this->input->post('train_no'),
        'train_location' => $this->input->post('train_location'),
        'train_symbol' => $this->input->post('train_symbol'),
        'train_direction' => $this->input->post('train_direction'),
        'train_hpt' => $this->input->post('train_hpt'),
        'train_crew' => $this->input->post('train_crew'),
        'train_status' => $this->input->post('train_status')
    );
    $this->load->model('atisroad_model');
    $this->atisroad_model->update_recrew($data);
    if ($this->db->affected_rows() > 0)
    {
        redirect(base_url() . 'atis/recrews');
    }
    else
    {
        echo 'failure';
    }

1 Answer 1

1

You can pass the current value as the 3rd parameter when you call the form_dropdown() function.

$options = array(
    'small'  => 'Small Shirt',
    'med'    => 'Medium Shirt',
    'large'   => 'Large Shirt',
    'xlarge' => 'Extra Large Shirt'
);

echo form_dropdown('shirts', $options, 'large');

When the dropdown is rendered whichever option matches the 3rd parameter will be selected.

See the docs for the CodeIgniter form helper

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.