1

This is my following code:

$update = array(
                    'fullname' => $this->input->post('fullname'),
                    'phone' => $this->input->post('phone'),
                    'sex' => $this->input->post('sex'),
                    'city' => $this->input->post('city'),
                    if($_POST['bday']){ -->THIS FROM SUBMIT FORM
                        'bday' => $this->input->post('birthday')
                    }
);

Is there is a way to do that if conditional?

3 Answers 3

2

Just need to append it in existing array like:

$update = array(
 'fullname' => $this->input->post('fullname'),
 'phone' => $this->input->post('phone'),
 'sex' => $this->input->post('sex'),
 'city' => $this->input->post('city')                   
);

if($_POST['bday']){ 
    $update['bday'] = $this->input->post('birthday');
}

Check Live Demo: Click Here

Sign up to request clarification or add additional context in comments.

1 Comment

array_push() will append value without key so its not useful in your case. Please use above code. It will append key and value both. @ReynaldHenryleo
1

@Reynald Henryleo you can do it like below:

<?php
    $update = array(
                    'fullname' => $this->input->post('fullname'),
                    'phone' => $this->input->post('phone'),
                    'sex' => $this->input->post('sex'),
                    'city' => $this->input->post('city'),
                    'bday' => !empty($_POST['bday']) ? $this->input->post('birthday') : null
            );

    // if you don't want bday with null value then try below one

    $update = array(
                    'fullname' => $this->input->post('fullname'),
                    'phone' => $this->input->post('phone'),
                    'sex' => $this->input->post('sex'),
                    'city' => $this->input->post('city')
            );
    if(!empty($_POST['bday'])){
        $update["bday"] = $this->input->post('birthday');
    }

3 Comments

hmm it will set 'null' as the value
yeah, don't you want null value ?
i want if conditional to add extra index 'bday'. So if $_POST['bday'] == FALSE then 'bday' index will not be set
0
'city' => $this->input->post('city'),
'bday' => ($_POST['bday']) ? $this->input->post('birthday') : ''

PHP docs reference.

1 Comment

i mean it will declare 'bday' index

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.