tell csv import script to ignore blank rows?


Posted on 16th Feb 2014 07:03 pm by admin

Hi i have the below script but i get an error if a row is blank ie a return in the csv file at the end i need to somehow tell it to skip the rows that have nothing on them:

Code: <?php
// define table name and csv file location and name

$supp_id = "15";
$pricecode = "6";
$csvupload = "../ccd/Catalogue_ZZMILGAT.csv";

// create DB connection
$host = "localhost";
$user = "user";
$pass = "pass";
$mydb = "db";
$table = "table";

$db = mssql_connect($host,$user,$pass);
mssql_select_db($mydb);

// delete all old data
$sql2 = "DELETE FROM ".$table." WHERE supp_id = ".$supp_id."";

mssql_query($sql2) or die("Failed to insert to db ");

// Define DB mapping (Fill in rest of the mapping in order of appearance in CSV)
$fields = array(
"manufacturer",
"ignore",
"manf_part_no",
"supp_part_no",
"description",
"rrp",
"cost_price",
"avail_qty");


// Open the CSV file
$handle = fopen($csvupload, "r") or die("Unable to open $csvupload for reading");

// Get each row's values to an array
$i = 1;
// define tab delimited or comma
while (($data = fgetcsv($handle, 512, ",")) !== FALSE) {
// First row's headers and not included, otherwise row field count must match wanted field count
if(count($data)==count($fields) && $i!=1){
$rows[] = $data;
}
elseif(count($data)!=count($fields)) {
die("Erroneus CSV file: incorrect field count on row $i");
}
$i++;
}

// Close file handle
fclose($handle);

// Create SQL
if(count($rows)==0) die("No rows found in user file");


// Remove last comma
foreach($rows as $i => $row){
$sql = "INSERT INTO ".$table." (";
foreach($fields as $field)
if($field!="ignore")
$sql .= "$field,";
$sql = substr($sql,0,-1).",supp_id, date_added, PriceCode) VALUES ";
$sql .= "(";
foreach($row as $j => $value){
if($fields[$j]!="ignore"){
if(empty($value))
$value = "0";
// Quote strings, try to remove existing quotes
elseif(!is_numeric($value))
$value = str_replace("'","",$value);
$value = str_replace(""","",$value);
$sql .= "'$value',";
}
}

$sql = substr($sql,0,-1).",'".$supp_id."', getdate(),'".$pricecode."')"; // Remove last comma
//echo "SQL became: $sql";

mssql_query($sql) or die("Failed to insert to db ");

}

?>

No comments posted yet

Your Answer:

Login to answer
110 Like 17 Dislike
Previous forums Next forums
Other forums

gmmktime or mktime
I live in the UK, i am within the GMT (with daylight saving time ie 1 hour difrence between summer a

Saving data from a form into a file
Hey everybody,

Sorry, I am really new to PHP coding and such but a project kind of got thrust

How to have a log of all the status a VIM document had been ?
Hi guys,

I’m using SAP Invoice Management plugin and the /OPT/VIM_ANALYTICS transa

Hyperlink is adding an extra gap to variable
Hi, here's my problem..

I have a php generated page with a hyperlink which opens in a new win

Writting a script to arrange images........ need some help
Ok so here is the link

http://hmtotc.com/dev/projects/vrassociates/jeweler_dev/admin/index.ph

php - xml what is the best way to do this.
IN essence I want to have a script calling the info from the database.. I have no problem thus far,

Help with looping
I have a comma separated list of colors taken from a database ($ICo) and a directory of images named

RTF fomatting to email content
Im trying to sen an email with content is picked up from a rtf-file (file_get_contents('*.rtf'). Mai

a very simple php header question (sorry!)
Firstly...I do apologise if this annoys anyone....a header error

I'm do not know php &

ALV List Display to point to another report on Double Click
Hi,

I want my ALV List Display to point to another report on Double Click on its line ite

Sign up to write
Sign up now if you have flare of writing..
Login   |   Register
Follow Us
Indyaspeak @ Facebook Indyaspeak @ Twitter Indyaspeak @ Pinterest RSS



Play Free Quiz and Win Cash