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
17
Other forums
Converting from one format to another
"I have got a date in DD/MM/YYYY but I need it in MM-DD-YYYY. Help!"
Sub-domains & calling unique content
Hello,
Is there a way use something similar to the $_GET function for a sub-domain? I to be a
Creating Images from images in PHP
Okay well I am trying to make a 'dynamic' calender image with PHP.
I have images like this:
for loop without same $i digits?
Hi,
Can some guru please advise how can I accomplish this with a simple for loop ?
I
MySQL Primary key gap
Alright, so I have a table with 26 entries in it (id, filename, caption) for my image randomiser (ht
modifying a property of an object from a different class
I have 2 classes:
- Math
- Distance
in my math class, I have this line:
$distance =
output_buffering so rewrite
I'm planning to rewrite some of my scripts so they don't have to use output buffering to accomplish
Pagination
Hi All,
I think I'm finally getting somewhere with pagination!
I can now submit a quer
Check Date/Time in PHP
What I need the code to check is that $reQuest is 24 hours in the past then return a time based on h
Doubles are giving me problems
Ok so, first of all i made a double = 0.05, but when running the debugger it shows up as 0.04999. I