1

I'm building a delivery website.

If you have an account,for delivery details you have 2 options:

  1. Use the account delivery details(you complete them when you register),
  2. Use different delivery details(you complete them in shopping cart)

I'm making these verifications:

The user is logged in? If yes, verify if he entered some new delivery details(i'm using POST to take the values and i'm making an array with them-$data_pos).If he entered, so empty($data_pos) will be false, make a new array with his name(from account) and the total price and insert the order in database.

If the user is not logged in, he will need to complete the details to proceed.

The problem is that,even if $data_pos(the delivery details that he can complete to overwrite the account delivery details) is empty or not, the function empty always goes on false so even if he enters nothing, it will say that he entered something.

Note: This it's a function i made to print_r an array and die.Just debugging purpose.

echo '<pre>';
      print_r($data);
die();

Model:

        if($this->session->userdata('logged_in')){
            $data_pos = array(
                'telephone' => $this->input->post('phone_pos'),
                'address' => $this->input->post('address_pos'),
                'details' => $this->input->post('details'),
            );
            if(empty($data_pos) == false){
                $data = array(
                    'client_name' => $this->session->userdata('email'),
                    'telephone' => $this->input->post('phone_pos'),
                    'address' => $this->input->post('address_pos'),
                    'details' => $this->input->post('details'),
                    'price' => $this->cart->total(),
                );
                echo '<pre>';
                    print_r($data_pos);
                    echo 'second array is not empty';
                die("\n".'die in '. __FILE__ . ' at line '. __LINE__);
            }else{
                $data = array(
                    'client_name' => $this->session->userdata('email'),
                    'telephone' => $this->session->userdata('phone'),
                    'address' => $this->session->userdata('address'),
                    'details' => $this->input->post('details'),
                    'price' => $this->cart->total(),
                );
                echo '<pre>';
                    print_r($data);
                    echo 'i take informations from account beacuse the second array is empty';
                die("\n".'die in '. __FILE__ . ' at line '. __LINE__);
            }
        }
1
  • 2
    And as always, read the manual first. The docs for empty explicitly state what is considered an empty array. Commented May 8, 2018 at 6:42

4 Answers 4

3

The problem is that you are filling the array with the details they have entered, even if they do not enter any data. The key will be in the array with a blank value, so there will be content in the array...

$data_pos = array(
    'telephone' => $this->input->post('phone_pos'),
    'address' => $this->input->post('address_pos'),
    'details' => $this->input->post('details'),
);
if(empty($data_pos) == false){

One way you can do it is you can do is to filter the inputs to remove blank values

$data_pos = array(
    'telephone' => $this->input->post('phone_pos'),
    'address' => $this->input->post('address_pos'),
    'details' => $this->input->post('details'),
);
$data_pos = array_filter($data_pos);
if(empty($data_pos) == false){
Sign up to request clarification or add additional context in comments.

4 Comments

I see...I didn't knew about array_filter.Rookie mistake i guess... Thank you! I will accept it as an answer as far as i can
A more common approach is to check each individual field to see if it has a value, this means you can also report errors like 'Please enter a name'. You could also make the field mandatory using HTML, so the form isn't submitted until all details are complete.
A minor point - client side validation should not be used instead of server side validation. It should be used as well as server side validation.
@fubar good point. People may think it's overkill to check at both levels but it's worth it.
3

use array_filter to check empty value or not

replace your code

if(empty($data_pos) == false){

to

if(array_filter($data_pos)){
    // true
}else{
    // false
}

if you want to check 3 fields shouldn't be empty value add like this,

if(count(array_filter($data_pos)) == 3){

Comments

2

That's because it won't never be empty if you just declare an array with keys defined in the previous lines.

        $data_pos = array(
            'telephone' => $this->input->post('phone_pos'),
            'address' => $this->input->post('address_pos'),
            'details' => $this->input->post('details'),
        );

This array is not empty even when the inputs are NULL. You just create the keys telephone, address and details so... is not empty.

Comments

-1

You are facing this issue because you initialize the array before checking for empty().

$data_pos = array(
    'telephone' => $this->input->post('phone_pos'),
    'address' => $this->input->post('address_pos'),
    'details' => $this->input->post('details')
);

Comments

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.