Time for another round. I don’t know why these music companies have to make things so damn difficult on me. A couple months ago, we started submitting our clients’ music to spotify and now we’re getting the sales reports back, but instead of giving it to us in a way that’s easy to import, they do something like
Format version Start date End date Sender Recipient Label Aggregator Disclaimer
4 5/1/2012 5/31/2012 Spotify xxxxxxxxx #N/A #N/A Confidential
Country Product URI UPC EAN ISRC Track name Artist name Composer name Album name Quantity Label
So, now i’m left to figure out a way to completely skip the first two rows of the file so that i’m left with Country, Product, etc. Since this is part of another import file, i already have set up to read the file into an array using $fcontents = file($file, FILE_IGNORE_NEW_LINES);, then i loop through it with a for loop. Below is the entire php part of the script.[php]
if(isset($_POST[‘submit_ureport’])) {
$companyArr = array('venzo_app_sales', 'venzo_app_trends', 'venzo_itunes_sales', 'venzo_itunes_trends', 'spotify');
$company = mysql_real_escape_string(strtolower($_POST['company']));
if(in_array($company, $companyArr)) {
if($company == "venzo_app_sales") {
$path = "sales/app/";
} elseif($company == "venzo_app_trends") {
$path = "trends/app/";
} elseif($company == "venzo_itunes_sales") {
$path = "sales/itunes/";
} elseif($company == "venzo_itunes_trends") {
$path = "trends/itunes/";
} elseif($company == "venzo_spotify_sales") {
$path = "sales/spotify/";
$start = $_POST['start_date'];
$end = $_POST['end_date'];
}
$companyTbl = $company;
}
if($_FILES['file']['error'] > 0) {
echo "Return Code: {$_FILES['file']['error']}<br />";
} else {
if(file_exists($path . $_FILES['file']['name'])) {
echo "{$_FILES['file']['name']} already exists.";
} else {
if(move_uploaded_file($_FILES['file']['tmp_name'], $path . $_FILES['file']['name'])) {
//echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
$file = $path.$_FILES['file']['name'];
$fcontents = file($file, FILE_IGNORE_NEW_LINES);
/*check the table exists*/
$qry = current(mysql_fetch_row(mysql_query("SELECT DATABASE()")));
$qry = mysql_query("SHOW TABLES FROM `{$qry}` LIKE '{$companyTbl}'") or die(mysql_error());
if(mysql_num_rows($qry) < 1) {
unlink($file);
die("Table `{$companyTbl}` for '{$company}' does not exist!");
}
/*pull the field list out of the DB*/
$qry = mysql_query("SHOW FIELDS FROM `{$companyTbl}`") or die(mysql_error());
//echo $company."<br />";
if($company == "venzo_itunes_trends" || $company == "venzo_app_trends") {
$sqlFieldNum = mysql_num_rows($qry);
while($field = mysql_fetch_assoc($qry)) {
$sqlFields[] = strtolower($field['Field']);
}
} else {
$sqlFieldNum = mysql_num_rows($qry)-2;
while($field = mysql_fetch_assoc($qry)) {
if($field['Field'] != 'id' && $field['Field'] != 'report_paid')
$sqlFields[] = strtolower($field['Field']);
}
}
/*check the field list in the report and the field list in the database match*/
if($sqlFieldNum != count($fieldArr = explode("\t", $fcontents[0]))) {
foreach($fieldArr as $key => $value) {
$fields[0][$key] = trim(str_replace('/', '_', strtolower($value)));
$fields[1][$key] = trim(str_replace(array('/', ' '), '_', strtolower($value)));
$fields[2][$key] = trim(str_replace(array('/', ' '), array('_', ''), strtolower($value)));
}
$tfields = $fields[0];
/*check for fields in the DB that are not in the report*/
for($a = 0; $a < count($sqlFields); $a++) {
echo "SQL: {$sqlFields[$a]} - Raw report: {$fields[0][$a]} - Underline report: {$fields[1][$a]} - Strip-spaces report: {$fields[2][$a]}<br />";
if($sqlFields[$a] != $fields[0][$a] && $sqlFields[$a] != $fields[1][$a] && $sqlFields[$a] != $fields[2][$a]) {
$error = true;
echo "<b>Report fields do not match the table fields for `{$company}`! Field `{$sqlFields[$a]}` should not exist! Estimated expected field name: '" . $fields[0][$a] . "'.</b><br /><br />";
} else {
unset($tfields[$a]);
}
}
/*check for fields in the report that are not in the DB*/
if(count($tfields) > 0) {
$error = true;
echo "<b>Report fields do not match the table fields for `{$company}`! Field(s) `" . implode("`, `", $tfields) . "` are missing!</b><br /><br />";
}
}
/*if there is an error, stop here and delete the file*/
if($error == true) {
unlink($file);
die();
}
/*for each record in the report...*/
for($i = 1; $i < count($fcontents); $i++) {
$line = $fcontents[$i];
$arr = explode("\t", $line);
/*santitise the values*/
foreach($arr as $key => $value) {
$arr[$key] = trim(mysql_real_escape_string($value));
}
if(strtolower($arr[0]) != "total") {
/*number of fields in the DB exceeds number of fields in the record*/
if($sqlFieldNum > count($arr)) {
$error[$i] = true;
for($a = 0; $a < count($sqlFields); $a++) {
echo "{$a}) MYSQL: {$a} => {$sqlFields[$a]} - Report: {$a} => {$arr[$a]}<br />";
}
echo "<b># of fields in the table (" . $sqlFieldNum . ") is greater than the # of fields in the report (" . count($arr) . ")!</b><br /><br />";
/*number of fields in the record exceeds number of fields in the DB*/
} else if($sqlFieldNum < count($arr)) {
$error[$i] = true;
for($a = 0; $a < count($arr); $a++) {
echo "{$a}) MYSQL: {$a} => {$sqlFields[$a]} - Report: {$a} => {$arr[$a]}<br />";
}
echo "<b># of fields in the report (" . count($arr) . ") is greater than the # of fields in the table (" . $sqlFieldNum . ")!</b><br /><br />";
}
/*if there is no error insert the record into the table else continue onto the next record*/
if($error[$i] != true) {
$sql = "INSERT INTO {$companyTbl}(" . implode(', ', $sqlFields) . ") VALUES ('" . implode("', '", $arr) . "')";
mysql_query($sql);
if(mysql_errno()) {
unlink($file);
die(mysql_error());
}
} else {
echo "<b>Record {$i} not inserted!</b><br /><br />";
}
}
}
/*if there is an error, stop here and delete the file*/
if(count($error) > 0) {
unlink($file);
die();
}
if($sql) {
$msg = basename($_FILES['file']['name']) . ' has successfully been imported. The information is ready to be displayed<br />';
} else {
$msg = basename($_FILES['file']['name']) . ' has NOT been imported, please try again later!<br />';
}
}
}
}
}[/php]
Any help would be greatly appreciated. I’ve been racking what few brain cells i have left for a week now.