编译错误无法在代码点火器的写上下文中使用方法返回值

时间:2016-04-05 14:47:39

标签: php codeigniter-3 hmvc

我有这个模型在数据库中插入数据,但问题是,其他字段不是必需的,所以我这样做了,

public function insert(){


    if($this->input->post('MNAME') = NULL){
        $mname = "n/a";
    }else{
        $mname = $this->input->post('MNAME');
    }

    if($this->input->post('EXTENSION') = NULL){
        $ext = "n/a";
    }else{
        $ext = $this->input->post('EXTENSION');
    }

    if($this->input->post('EMAIL') = NULL){
        $email = "n/a";
    }else{
        $email = $this->input->post('EMAIL');
    }

    if($this->input->post('CELLNO') = NULL){
        $cell = "n/a";
    }else{
        $cell = $this->input->post('CELLNO');
    }

    if($this->input->post('AGENCY_NO') = NULL){
        $agency = "n/a";
    }else{
        $agency = $this->input->post('AGENCY_NO');
    }

    $input = array(
        'ID_NUM'        => $this->uri->segment(3),
        'FNAME'         => $this->input->post('FNAME'       ),
        'SURNAME'       => $this->input->post('SURNAME'     ),
        'DO_BIRTH'      => $this->input->post('DO_BIRTH'    ),
        'POBIRTH'       => $this->input->post('POBIRTH'     ),
        'SEX'           => $this->input->post('SEX'         ),
        'CIVILSTAT'     => $this->input->post('CIVILSTAT'   ),
        'ID_NAT'        => $this->input->post('ID_NAT'      ),
        'HEIGHT'        => $this->input->post('HEIGHT'      ),
        'WEIGHT'        => $this->input->post('WEIGHT'      ),
        'BLOOD_TYPE'    => $this->input->post('BLOOD_TYPE'  ),
        'RES_ADD'       => $this->input->post('RES_ADD'     ),
        'RES_ZIP'       => $this->input->post('RES_ZIP'     ),
        'PERM_ADD'      => $this->input->post('PERM_ADD'    ),
        'PERM_ZIP'      => $this->input->post('PERM_ZIP'    ),
        'MNAME'         => $mname,
        'EXTENSION'     => $ext,
        'EMAIL'         => $email,
        'CELLNO'        => $cell,       
        'AGENCY_NO'     => $agency,
        'DATE_CREATED'  => date("Y-m-d")
        );

    $insert = $this->db->insert($this->table,$input);

    return $insert;
}

但问题是我得到了这个错误Can't use method return value in write context说它在第108行。其中,第108行是我模型中的第一个。我的错误是什么?是否有更短的代码?

1 个答案:

答案 0 :(得分:0)

  

您仅在 IF 语句中声明1 =,以备将来注意:匹配中应为2 =

我为您重新构建了这个代码,因此您的代码更具可读性和简短性。

// Checking if they're null and appending n/a if they're
$example = array('MNAME','EXTENSION','EMAIL', 'CELLNO', 'AGENCY_NO');
$new = array();
foreach ($example as $_ex)
{
    $check = $this->input->post($_ex);
    if ($check == null)? array_push($new, 'n/a') : array_push($new, $check);
}

然后替换为此(记住索引在数组中 0 的开头):

// Inside your DB insert query
'MNAME'         => $new[0],
'EXTENSION'     => $new[1],
'EMAIL'         => $new[2],
'CELLNO'        => $new[3],       
'AGENCY_NO'     => $new[4],

我希望这会有所帮助。