Jump to content

Uploading and sorting csv file


Nodral

Recommended Posts

Hi All

 

Please can someone help me.

 

I have a CSV file with over 3000 lines.  Each one contains the following information

 

Country Insurance Name Mandatory or Optional Description and Coverage Group SIPP Cost Per Day Cost Per Week Cost Per Month Deductable

I have written the following script to read line by line, compare the country and insurance with the previous and if it is the same, tag certain parts of each line onto the end of the previous.

 

However, I am getting no output from this, and I keep getting an error saying I am using too much memory or my computer hangs.

 

I am fairly new to php so any pointers would be good.

 

Firstly, does my script look like it should do what I want it to do?

If so, any clues how to get around the momory issue.

If not, what have I done wrong?

 

Cheers guys

 

<?php
//open file paths
$handle=fopen("insurances.csv","r");
$handle_out=fopen("insuranceoutput.csv","a");

//writes the first line of the csv file into an array to act as a comparrison
if($handle){
	$line=fgets($handle);
	$line_array=explode(",",$line);
	$full_array=$line_array;
	fclose($handle);
}

}
if($handle)
{
while (!feof($handle))
{
	// read one line into array
	$line=fgets($handle);
	$line_array=explode(",",$line);
	fclose($handle);

	//test to see if country matches previous
	while ($line_array[0] == $full_array[0])
	{
		//test to see if insurance matches previous
		while ($line_array[1] == $full_array[1])
		{
			//write array values 4 - 9 to main array
			$full_array[]=array("$line_array[4]","$line_array[5]","$line_array[6]","$line_array[7]","$line_array[8]","$line_array[9]");

			}
		}
		//write to new file when new insurance is found

		foreach($full_array as $output)
			{
			fwrite($handle_out, "$output,");
			}
			fwrite($handle_out,"\n");
			fclose($handle_out);

	}
	// write to new file if new country is found.
	foreach($full_array as $output)
			{
			fwrite($handle_out, "$output,");
			}
			fwrite($handle_out,"\n");
			fclose($handle_out);
}
}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.