Mukesh S
Mukesh S

Reputation: 377

issue with csv file row value remove white space

I'm importing data from CSV file to database by using php

$i = 0;
while(($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{  
if($i!=0) {
   if(trim($data[0])!=' ') { 
    $branddata['brand']         = $data[0];
    $branddata['create_date']   = date('Y-m-d H:i:s');
    print_r($branddata); die;
    $res = $this->mdl_brands->insertIntoBrand($branddata);
   }
}
$i++;
}

and My csv file has following data

enter image description here

After the Brand Name in the 2 row I have added more than one blank space which is inserting entry into the database.

However I have used the trim function to remove the blank spaces but it not working.

Please guide how to avoid this.

Upvotes: 0

Views: 1722

Answers (1)

Rizier123
Rizier123

Reputation: 59681

As you said it:

I have used the trim function to remove the blank spaces

You removed them! So you don't have to check for 1 space, just do:

 if(trim($data[0]) != '') {
                    //^^ See here

Upvotes: 1

Related Questions