0

i got this model which inserts data on the database but the problem is, other fields are not required so i did this,

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;
}

but the problem is that i get this error Can't use method return value in write context says that its on line 108. which, the line 108 is the first if of my model. what is my error? and is there any codes which will be shorter?

kev_m
  • 325
  • 7
  • 30

1 Answers1

0

You're only stating 1 = in your IF statements, for future note: It should be 2 ='s in a match.

I restructured this for you so your code is more readable and short.

// 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);
}

Then replace with this (keeping in mind index's start at 0 in an array):

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

I hope this helped.

Jaquarh
  • 6,493
  • 7
  • 34
  • 86