2

I'm wondering if someone can help me out. I have a form_dropdown which is filled with options from the database. I want to show the default value which the user selected while inserting a record, but can't figure out how to do it without adding that to the database This is Model

function get_type()
{
    $results = $this->db->select('t_id, t_name')->from('account_type')->get()->result();

    $t_id = array('-SELECT-');
    $t_name = array('-SELECT-');

    for ($i = 0; $i < count($results); $i++)
    {
        array_push($t_id, $results[$i]->t_id);
        array_push($t_name, $results[$i]->t_name);
    }
    return $type_result = array_combine($t_id, $t_name);
}
function get_account_record($a_id)
{
    $this->db->where('a_id', $a_id);
    $this->db->from('account_info');
    $query = $this->db->get();
    return $query->result();
}

This is the Controller

function update($a_id)
{
    $data['a_id'] = $a_id;

    $data['type'] = $this->sf_model->get_type();
    $data['view'] = $this->sf_model->get_account_record($a_id);

    $this->form_validation->set_rules('a_name', 'Account Name', 'trim|required|xss_clean|callback_alpha_only_space');
    $this->form_validation->set_rules('a_web', 'Website', 'trim|required|xss_clean');
    if ($this->form_validation->run() == FALSE)
    {
        $this->load->view('viewUpdate', $data);
    }
    else
    {
        $data = array(
            'a_id' => $this->input->post('a_id'),
            'a_name' => $this->input->post('a_name'),
            'a_website' => $this->input->post('a_web'),
            't_id' => $this->input->post('a_type'),
            'a_billingStreet' => $this->input->post('a_billingStreet'),
            'a_billingCountry' => $this->input->post('a_billingCountry'),
            'a_mobile' => $this->input->post('a_mobile')
        );

        $this->db->where('a_id',$_POST['a_id']);
        $this->db->update('account_info', $data); 
       redirect('salesforce' . $a_id);
    }

}

this is the view of dropdown function TYPE only

    <?php
    $attributes = 'class = "form-control" id = "a_type"';
    echo form_dropdown('a_type',$type,set_value('a_type'),$attributes);?>
    <span class="text-danger"><?php echo form_error('a_type'); ?></span>
?>
3
  • So you have $selected ='' . Then, while looping through the results, if the user's value is equal to the dropdown value $selected = 'selected' Commented May 21, 2015 at 7:19
  • Unable to get u. form_dropdown($type) contains all the values in a DB just wanted to show the value to the user which he has selected while inserting the record. Thanks in ADVANCE @Strawberry Commented May 21, 2015 at 11:56
  • I'm unfamiliar with that function Commented May 21, 2015 at 12:14

1 Answer 1

0

Consider the following. It should be obvious from this that I'm no PHP coder, but it should give you an idea...

    <?php

    /*  DROP TABLE IF EXISTS colours;

        CREATE TABLE colours(colour VARCHAR(12) NOT NULL PRIMARY KEY);

        INSERT INTO colours VALUES
        ('Red'),('Orange'),('Yellow'),('Green'),('Blue'),('Indigo'),('Violet');

        DROP TABLE IF EXISTS users;

        CREATE TABLE users
        (username VARCHAR(12) NOT NULL PRIMARY KEY
        ,colour VARCHAR(12) NOT NULL
        );

        INSERT INTO users VALUES
        ('Adam','Orange'),('Bob','Green'),('Charlie','Red'),('Dan','Yellow');


      */

include('path/to/connection/stateme.nts');
$query = "
        SELECT c.*
             , CASE WHEN u.colour = c.colour THEN 1 ELSE 0 END selected
          FROM colours c
          LEFT
          JOIN users u
            ON u.colour = c.colour
           AND u.username = 'Adam';";

$result = mysqli_query($db,$query);

$options = '';

 while($row = mysqli_fetch_assoc($result)){
 if ($row['selected'] == 1){
  $selected = 'selected';
  } else {
  $selected = '';
  }
    $options .= "<option $selected >{$row['colour']}\n";

 } // end of while loop
?>

<select><? echo "\n$options"; ?> </select>

Outputs:

<select>
<option  >Blue
<option  >Green
<option  >Indigo
<option selected >Orange
<option  >Red
<option  >Violet
<option  >Yellow
</select>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.