There has to be an easier way of getting rid to 2 lines in that file lol.
First you need to make sure the file has a line break to divide into the three lines or is all data separated by tabs and what you see is wrap around.
$lines = explode("\n",$mystring) // using line breaks (maybe (\r\n)
$mystring = $lines[2]; The string you want
or
$lines = explode("\t",$mystring) // using tabs
Well, its gotta have line breaks, hadn’t thought of that. ok, so if i explode it and use a for loop, i’d just start the loop at 2 instead of 0 (for $i = 2; $i < count($lines); $i++) ? The first 2 lines should be $line[0] and $line[1] right?
Actually, if i do it this way, i can skip the 3rd line too because i won’t need the headers on the 3rd line.
Well, i had a look at how it imports other files and it explodes it on the \t. Now i’m stuck trying to get the script to ignore all the field checks.
well if that info you provided (three lines) is a header, it’s most likely ALL tabs, you just said it’s a header. A header by nature can only be one line, unless items wrap within each element.
array_diff() might help…
$a1=array(“Cat”, “Dog”, “Snake”);
$a2=array(“Snake”, “Dog”, “Rabbit”);
print_r(array_diff($a1, $a2));
count($a1) = 1
$a1[0] = Cat
Any array element in $a1 that matches an element in $a2 will be removed, reducing the array to only non-matches.
Please explain ‘field checks’.
The middle bit of code in my first post (all that sqlfield stuff) looks at the first row and compares that against the column names in the db table. if one does’t exist or if something is out of place, it stops the script and tells me what’s going on.
if it’s just 2 lines u need to skip use a counter and continue
[php]
$fh=fopen(‘blah.txt’);
$lines=0;
while(!feof($fh))
{
$row=fgetcsv($fh,0,"\t");
if($lines++ < 2) continue;
// More processing code
}
fclose($fh);
[/php]
if you need to start reading after the country header than a quick ok flag to signal when $line[0] is country to begin processing. About the same as above
[php]
$fh=fopen(‘blah.txt’);
$ok=FALSE;
while(!feof($fh))
{
$row=fgetcsv($fh,0,"\t");
if($line[0] == 'Country) $ok=TRUE;
if(!$ok) continue;
// More processing code
}
fclose($fh);
[/php]
Ill give it a try asap, looks like it should work. I knew there had to be a way.
ok, i’m making progress here on this thing, but i have another question. Where is $line[0] coming from? is it supposed to be $row[0]? below is what i have so far.
[php]
if($companyTbl == “venzo_spotify_sales”) {
$fh=fopen($file, "r");
$ok=FALSE;
while(!feof($fh)) {
$row=fgetcsv($fh,0,"\t");
if($line[0] == 'Country') $ok=TRUE;
if($ok) continue; // More processing code
$qry = mysql_query("SHOW FIELDS FROM `{$companyTbl}`") or die(mysql_error());
while($field = mysql_fetch_assoc($qry)) {
$sqlFields[] = strtolower($field['Field']);
}
for($i = 3; $i < count($line); $i++) {
$sql = "INSERT INTO {$companyTbl}(start_date, end_date, " . implode(', ', $sqlFields) . ") VALUES ('$start_date, '$end_date', '" . implode("', '", $row) . "')";
mysql_query($sql) or die(mysql_error());
}
}
fclose($fh);
}[/php]
I’m sure he meant $row[0] and not $line[0]…
Well i see where $line came from, but i can’t get it to skip those first to lines, no matter what i change.
look back to his original post. He uses $line as a counter and that’s where you skip the first two lines.
yea, i found that. I ended up not using it though, i got it. i haven’t tried the sql insert yet, so its commented out. this is what i ended up with.
[php]
$file = $path.$_FILES[‘file’][‘name’];
$fcontents = file($file, FILE_IGNORE_NEW_LINES);
if($companyTbl == “venzo_spotify_sales”) {
for($i = 3; $i < count($fcontents); $i++) {
$line = $fcontents[$i];
$arr = explode("\t", $line);
//$sql = "INSERT INTO {$companyTbl}(start_date, end_date, " . implode(', ', $sqlFields) . ") VALUES ('$start_date, '$end_date', '" . implode("', '", $row) . "')";
//$ins = mysql_query($sql);
if(!ins) {
unlink($file);
//die(mysql_error());
}
}
$qry = mysql_query("SHOW FIELDS FROM `{$companyTbl}`") or die(mysql_error());
while($field = mysql_fetch_assoc($qry)) {
$sqlFields[] = strtolower($field['Field']);
}
}[/php]
Now its telling me that the columns don’t match, but i think i know what’s causing that. I need to find a way to add the start and stop dates at the beginning of an array, then it should be fixed.
The only thing I might add is to verify that $fcontents is not empty
While I was looking at your code you snuck one in first…
You query string does not match your database table column labels. Spelling or order. Your previous code did not show.
it does and it doesn’t. the fields in $sqlFields are generated by a query, but because i’m ignoring the first 2 lines of the text file, i no longer have the start and end dates. Those dates i have to manually add through a form. I think that’s where it’s messing up in the query. I need to find a way to add those dates to the $fcontents array. After that, if its messing up, i can look at the physical data.
This is getting on my nerves. I tried to add the dates to the beginning of the arr array, but the output doesn’t look anything like the example on php.net. I’m trying to use array_unshift() to do it, and this is the output
[php]
Array
(
[0] => Array
(
[0] => 05/01/12
[1] => 05/31/12
)
[1] => DE
[2] => A
[3] => spotify:track:3yVXQ8dc45fXsby4RvZ502
[4] => 436647721143
[5] => N/A
[6] => USRPR1252801
[7] => Shards
[8] => Maksim
[9] => N/A
[10] => Shards - Single
[11] => 4
[12] => R-Records
)
[/php]
According to the example, its supposed to look like
[php]
Array
(
[0] => 05/01/12
[1] => 05/31/12
[2] => DE
[3] => A
[4] => spotify:track:3yVXQ8dc45fXsby4RvZ502
[5] => 436647721143
[6] => N/A
[7] => USRPR1252801
[8] => Shards
[9] => Maksim
[10] => N/A
[11] => Shards - Single
[12] => 4
[13] => R-Records
)
[/php]
Looks like your adding an array to an array.
you should be merging the two arrays.
array_merge()
$new_array=array_merge($array_one,$array_two);
yeah, I goofed my other code snippets
$line should have been $row
Dude, you’re frikin genious Thank you both for helping me out. I got it working. I just had to change up one of the for loops to fix the sqlFields variable, after that it worked.
If anyone wants a simple csv import script, here you go
[php]
$file = $path.$_FILES[‘file’][‘name’];
$fcontents = file($file, FILE_IGNORE_NEW_LINES);
if($companyTbl == “your table here”) {
$qry = mysql_query("SHOW FIELDS FROM `{$companyTbl}`") or die(mysql_error());
while($field = mysql_fetch_assoc($qry)) {
for($a = 1; $a < mysql_num_rows($qry); $a++) {
$field = mysql_fetch_assoc($qry);
$sqlFields[] = strtolower($field['Field']);
}
}
$dt[] = $start;
$dt[] = $end;
for($i = 3; $i < count($fcontents); $i++) {
$line = $fcontents[$i];
$arr = explode("\t", $line);
foreach($arr as $key => $value) {
$arr[$key] = trim(mysql_real_escape_string($value));
if(empty($arr[$key])) {
$arr[$key] = 'N/A';
}
}
$new_con = array_merge($dt, $arr);
$sql = "INSERT INTO {$companyTbl}(" . implode(', ', $sqlFields) . ") VALUES ('" . implode("', '", $new_con) . "')";
mysql_query($sql);
if(mysql_errno()) {
unlink($file);
die(mysql_error());
}
}
[/php]
Keep in mind that this uses a pull down box to choose what table needs to be used, but it could easily be modified to suit anyone’s needs.