在ARRAY Codeigniter中使用SWITCH语句

时间:2014-03-03 01:22:09

标签: php codeigniter postgresql

我已经尝试在我的模型页面中将switch放入我的代码中,但它给了我服务器错误或者我的语法错误。我想要做的是,只要degree的值等于特定值,它就会将其发布或保存到具有相应名称的数据库中。

这是我在模型页面中所做的:

public function changeNow() {
    $data = array(
        'name' => $this->input->post('name'),
        'degree' => $this->input->post('degree'),

        switch($this->input->post('degree')){
            case 'POST-BACC':
                'high_degree_post_bacc'=>$this->input->post('high_degree_post_bacc');
                break;
            case 'LLB':
                'high_degree_llb'=>$this->input->post('high_degree_llb');
                break;
            case 'MD':
                'high_degree_md'=>$this->input->post('high_degree_md');
                break;
        }
    );

    $this->db->update('table_degree', $data); 
}

如果我删除了开关,它会运行。当我把它,它给出错误。

首先尝试:我做了@Marc所说的

 switch($this->input->post('degree')){

    case 'POST-BACC':
       $data['high_degree_post_bacc']=>$this->input->post('high_degree_post_bacc');
            break;                    }     //no luck: still got server error

1 个答案:

答案 0 :(得分:0)

switch('$this->input->post('degree'){

您的变量前面有一个'并且缺少)

应该是 switch($this->input->post('degree')){

你也试图在数组中使用switch语句。

尝试做类似

的事情

case 'POST-BACC': $data['high_degree_post_bacc'] = $this->input->post('high_degree_post_bacc'); break;

这是完整的答案:试试这个

public function changeNow() {
    $data = array(
        'name' => $this->input->post('name'),
        'degree' => $this->input->post('degree'),
        );

    switch($this->input->post('degree')){
        case 'POST-BACC':
            $data['high_degree_post_bacc'] = $this->input->post('high_degree_post_bacc');
            break;
        case 'LLB':
            $data['high_degree_llb'] = $this->input->post('high_degree_llb');
            break;
        case 'MD':
            $data['high_degree_md'] = $this->input->post('high_degree_md');
            break;
    }

    $this->db->update('table_degree', $data); 
}