TransWikia.com

How to ignore fields if empty?

WordPress Development Asked by Israel Santiago on January 16, 2021

I’m new in php and just have a quick question, I’m developing a plugin, in metaboxes I have a function to save data into database and this is the code I use to save:

 $models_to_insert = array();
    for ($i = 1; $i <= 3; $i++) {
        if (isset($_POST["car_model_$i"]) != "") {
            $car_model = sanitize_text_field($_POST["car_model_$i"]);
        }
        if (isset($_POST["car_price_$i"]) != "") {
            $car_price = sanitize_text_field($_POST["car_price_$i"]);
        }
        if (isset($_POST["car_brand_$i"]) != "") {
            $car_brand = sanitize_text_field($_POST["car_brand_$i"]);
        }
        if (isset($_POST["car_year_$i"]) != "") {
            $car_year = sanitize_text_field($_POST["car_year_$i"]);
        }
        if (isset($_POST["car_type_$i"]) != "") {
            $car_type = sanitize_text_field($_POST["car_type_$i"]);
        }
        if (isset($_POST["car_basefee_$i"]) != "") {
            $car_basefee = sanitize_text_field($_POST["car_basefee_$i"]);
        }
        if (isset($_POST["car_basemodel_$i"]) != "") {
            $car_basemodel = sanitize_text_field($_POST["car_basemodel_$i"]);
        }
        if (isset($_POST["car_rcusa_$i"]) != "") {
            $car_rcusa = sanitize_text_field($_POST["car_rcusa_$i"]);
        }
        if (isset($_POST["car_bono_$i"]) != "") {
            $car_bono = 1;
        } else {
            $car_bono = 0;
        }

        $single_model = array(
            'post_id' => $post_id,
            'car_model' => $car_model,
            'car_price' => $car_price,
            'car_brand' => $car_brand,
            'car_year' => $car_year,
            'car_type' => $car_type,
            'car_basefee' => $car_basefee,
            'car_basemodel' => $car_basemodel,
            'car_rcusa' => $car_rcusa,
            'car_bono' => $car_bono
        );
        array_push($models_to_insert, $single_model);
    }

    if (cotizador_query_select($post_id) == null) {
        foreach($models_to_insert as $model){
            $wpdb->insert($table_name, $model);
        }
    } else {
        $wpdb->delete($table_name, array('post_id' => $post_id));
        foreach($models_to_insert as $model){
            $wpdb->insert($table_name, $model);
        }

    }

The for counts until 3 because I have 3 times the inputs for 3 car models:

enter image description here

But if I just fill the first model and leave the others inputs empty, I got 3 records inside database with empty fields instead just one because only one model is filled:

enter image description here

How can I just ignore the rest of inputs that are not filled??

One Answer

I found one solutions and it works, just in case someone could have the same question, I know that the response could be with an:

if(input==""){

}

But the problem was that I'm inserting every model in an object, so if the fields are empty, it insert in the object empty values, so I just did this:

if ($single_model['car_model'] != "") {
array_push($models_to_insert, $single_model);
} else {}

because if no model name is added in the first input and user clicks "update" or "publish" button, means that user don't want/need another model

Not sure if it's the best way but it works

Answered by Israel Santiago on January 16, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP