1

I wrote a code to insert input data to database using codeigniter but before inserting, data will go through an if statement to check the input then if condition is met data will be saved.

I was trying to check if the input data is equal to section then parent id must be 0. Else if not section then the $data['type'] entered must be inserted to database and parent id must not be 0. This is my code so far.

$data = array (
'title' => $this->input->post->('title'),
'type' => $this->input->post->('type'),
'description' => $this->input->post->('description')
); 


if ($data['type'] == 'section') {
         $data['parent_id'] = 0;
       } else {
         $data['parent_id']  = $this->input->post('parent_id');
         $data['type']  = $this->input->post('type');
       }
 $result = $this->checklist_item_model->put_item($data);

Model

public function put_item ($data) {
   $this->db->insert('items', $data);
 }
2
  • Where is your insert query or model code!! Commented Apr 15, 2016 at 4:55
  • I edited the code above pls have a look Commented Apr 15, 2016 at 4:56

2 Answers 2

1

You can create you condition as

if($this->input->post('type')=='section')
{
$data = array(
 'title' => $this->input->post('title'),
 'type' => $this->input->post('type'),
 'description' => $this->input->post('description'),
 'parent_id' => 0// if section==0 condition
);
} else {
    $data = array(
    'title' => $this->input->post('title'),
    'type' => $this->input->post('type'),
    'description' => $this->input->post('description'),
    'parent_id' => $this->input->post('parent_id')
    );
}

 $result = $this->checklist_item_model->put_item($data);
Sign up to request clarification or add additional context in comments.

7 Comments

got an error syntax error, unexpected '(', expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$'
AHHH!! my bad remove -> after post! Check updated answer!!
the code run but I got the same error. it only insert the section input
If you got same error then this code will never run!! I think you miss something!! Check your column name and array key
I get it now. It's because of my database field type which is enum which is case sensitive.
|
1

You can use ternary operator to simplify your code

$data = array (
    'title' => $this->input->post('title'),
    'type' => $this->input->post('type'),
    'description' => $this->input->post('description'),
    'parent_id' => ($this->input->post('parent_id') == 'section') ? 0 : $this->input->post('parent_id');
); 
$result = $this->checklist_item_model->put_item($data);

And, in the model you can return the last insert id like this

public function put_item ($data) {
    $this->db->insert('items', $data);
    return $this->db->insert_id();
}

2 Comments

This one is helpful too. Using only short type of if else condition.
Your solution is good and efficient just remove -> after post to make it syntax error free

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.