J'ai ce script capable de lire le fichier csv qui s'exécute complètement, mais maintenant je veux d'abord le valider. Avant de télécharger, je veux vérifier toutes les données de la ligne. Si les données sont vides sur la ligne, une erreur s'affichera. J'ai essayé de nombreuses validations mais il semble que cela manque.

Ceci est mon script sur le téléchargement:

if(!empty($_FILES["charge_file"]["name"])){

        $file = explode('.', $_FILES['charge_file']['name']);
        $csvfile = end($file);
        $csvfile_ext = array('csv');

        if(in_array($csvfile, $csvfile_ext)){                       


        $handle = fopen($_FILES["charge_file"]["tmp_name"],"r");

        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {


                $chrg_empid = $data[0];
                $chrg_empname = $data[1];
                $chrgemp_name = utf8_encode($chrg_empname);
                $chrg_cvf = $data[2];
                $chrg_amount = $data[3];
                $chrg_store = $data[4];
                $chrg_date = $data[5];                          
                $chrg_installment = $data[6];
                $chrg_ded_type = $data[7];

                charges_individual_entry($chrg_empid, $chrg_empname, $chrg_cvf, $chrg_amount, $chrg_store, $ent_date, $chrg_ded_type, $chrg_installment);



            }

            // echo "success";

            fclose($handle);

        }
        else{   
            echo 'invalid';
        }
    }
    else{
        echo 'select';
    }

C'est ma fonction en insérant :

function charges_individual_entry($chrg_empid, $chrg_empname, $chrg_cvf, $chrg_amount, $chrg_store, $ent_date, $chrg_ded_type, $chrg_installment){

    $db = database2();
    $query = "INSERT INTO CFV_ENTRY (EMP_NO, EMP_NAME, REF_NO, AMOUNT, STORES, TRXN_DATE, DED_TYPE, NO_INSTALLMENT, DATE_INSERTED, ENTRY_TYPE, ENTRY_DESC) VALUES ('$chrg_empid', '$chrg_empname', '$chrg_cvf', '$chrg_amount', '$chrg_store', '$ent_date', '$chrg_ded_type', '$chrg_installment', SYSDATE, '1', 'CHARGE')";
    $cmd = $db->prepare($query);
    $cmd->execute();
    $db = null;

}

Voici ma validation :

function isEmptyFields($csv_file){

$handle = fopen($csv_file,"r");

    $counter = 0;

    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {

        $counter = $counter + 1;

        if ($data[0] != null) {

            if ($data[1] != null) {

                if($data[2] != null){

                    if ($data[3] != null) {

                        if($data[4] != null){

                            if($data[5] != null){

                                if($data[6] != null){

                                    if($data[7] != null){



                                        // echo $chrg_empid = $data[0];
                                        echo 'success';

                            //          echo $chrg_empid = $data[0]."\n";
                                        // $chrg_empname = $data[1]."\n";
                                        // echo $chrgemp_name = utf8_encode($chrg_empname)."\n";
                                        // echo $chrg_cvf = $data[2]."\n";
                                        // echo $chrg_amount = $data[3]."\n";
                                        // echo $chrg_store = $data[4]."\n";
                                        // echo $chrg_date = $data[5]."\n";
                                        // echo $chrg_installment = $data[6]."\n";
                                        // echo $chrg_ded_type = $data[7]."\n";


                                    }else{
                                        echo 'Empty field is in line: '.$counter; 
                                    }

                                }else{
                                    echo 'Empty field is in line: '.$counter;
                                }

                            }else{
                                echo 'Empty field is in line: '.$counter;
                            }

                        }else{
                            echo 'Empty field is in line: '.$counter;
                        }

                    }else{
                        echo 'Empty field is in line: '.$counter;
                    }

                }else{
                    echo 'Empty field is in line: '.$counter;
                }

            }else{

                echo 'Empty field is in line: '.$counter;
            }

        }else{
            echo 'Empty field is in line: '.$counter;
        }

    }


    // fclose($handle);

// return $handle;

}
0
rjjvillareal 18 mars 2019 à 08:51

2 réponses

Meilleure réponse

Voici une fonction qui parcourt la ligne et vérifie si l'un des éléments est vide.

function hasEmptyField(array $data)
{
    for ($i = 0; $i <= 7; $i++) {
        // Make sure that the key exists, isn't null or an empty string
        if (!isset($data[$i]) || $data[$i] === '') {
            return true;
        }
    }

    return false;
}

Vous pouvez utiliser cette fonction dans votre boucle while :

$counter = 0;

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {        

    $counter++;    

    if (hasEmptyField($data)) {
        // It has an empty field. Echo an error and skip to next row
         echo 'Empty field is in line: ' . $counter; 
         continue;
    }

    $chrg_empid       = $data[0];
    $chrg_empname     = $data[1];
    $chrgemp_name     = utf8_encode($chrg_empname);
    $chrg_cvf         = $data[2];
    $chrg_amount      = $data[3];
    $chrg_store       = $data[4];
    $chrg_date        = $data[5];                          
    $chrg_installment = $data[6];
    $chrg_ded_type    = $data[7];

    charges_individual_entry($chrg_empid, $chrg_empname, $chrg_cvf, $chrg_amount, $chrg_store, $ent_date, $chrg_ded_type, $chrg_installment);
}
0
Magnus Eriksson 18 mars 2019 à 09:35

Essayez ce qui suit -

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $counter++;
    foreach($data as $entry) {
        if(!$entry) {
            echo "Error on line $counter";
            break;
        }
    }
    //other code goes here
}
0
Tushar Walzade 18 mars 2019 à 07:51