php - CodeIgniter - 在更新记录时将默认值添加到数据库中的 form_dropdown

标签 php mysql codeigniter

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

最佳答案

请考虑以下事项。从这里可以明显看出我不是 PHP 编码员,但它应该给你一个想法......

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

输出:

<select>
<option  >Blue
<option  >Green
<option  >Indigo
<option selected >Orange
<option  >Red
<option  >Violet
<option  >Yellow
</select>

关于php - CodeIgniter - 在更新记录时将默认值添加到数据库中的 form_dropdown,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30366902/

相关文章:

php - 找不到Codeigniter Controller

Codeigniter 发送多封电子邮件

javascript - 如何将 JQuery Mobile 数据角色 ="listview"样式应用到 AJAX php/MySQL 页面结果

php - 如何使表格列可排序

php - SQL查询根据年和月选择数据

mysql - 用于查找属于某个类别的一组数据的媒体的 SQL 查询

php - 类的 Print_R 作为数组

php - 将 time() 转换为特定的日期时间

mysql - where 子句中的列 'u_id' 不明确

php - 将日期传递给 where 子句时出错