skipping lines in a text file

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 :slight_smile:

Dude, you’re frikin genious :slight_smile: 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.

Sponsor our Newsletter | Privacy Policy | Terms of Service