Hello all.
In the code below, I have a script to fix a set of column numbers to 18 by adding or popping them off until they get to 18.
Now, my only problem is that when it inserts my data into my database, all of the columns are shifting by 1 to the right.
Now, I dont know where to use array_shift to get them right, or if that is even the right command to use.
Any assistance is great!
$csv_read = fopen($path.$file_name, "r");
while(($data = fgetcsv($csv_read, filesize($path.$file_name)+1, "\t")) !== FALSE){
$csv[] = $data;
}
$row_num = 0;
$err_no = 0;
foreach($csv as $row) {
// the id column
$arrCol = array('');
// add values to column array
foreach($row as $col) {
if ($col == "") {
$col = "NULL";
}
$arrCol[] = mysql_escape_string($col);
if(sizeof($arrCol) > 18){
while(sizeof($arrCol) > 18){ array_pop($arrCol); }
} elseif(sizeof($arrCol) < 18) {
for($cnt=sizeof($arrCol); $cnt<18; $cnt++){
$cnt++;
$row[$cnt] = "NULL";
$cnt--;
}
}
}
//echo "Row ".$row_num." has ".sizeof($arrCol)." columns.<br />";
// execute query (avoid header line of csv file)
if($row_num != 0){
mysql_query("INSERT INTO ".$prefix."store_wholesale VALUES ('".implode("','", $arrCol)."')");
}
// increment row counter
$row_num++;
// if unsuccessful
if (mysql_error()) {
$num = count($arrCol);
echo "<b>Columns in row: ".$num."</b>";
echo "<br /><br /><hr><b>Line: ".$row_num." was not inserted due to structure error.<br /><br />MySQL says: ".mysql_error()."</b><hr><br /><br />";
$err_no++;
}
}