Hi everyone,
basicly I have a CSV file which uses ^ as a delimiter. I have a textarea which is populated by the contents of the CSV file, and after editing it the user is redirected to the edit page.

For some reason, everytime I save the file there is a line which is added between each line of text. I want to get rid of the empty lines.

Edit.php

<form action="save.php" method="post">
<textarea name="events" cols="100" rows="10" style="text-align:left">
<?php 
	if (($handle=fopen("events.csv","r"))!==FALSE)
	{
		$rowNo=1;
				
		while (($data = fgetcsv($handle, 1000, "^")) !== FALSE) 
		{
			$num=count($data);
			$data=str_replace("&rsquo;","'",$data);?>
			
			<?php		
			if ($rowNo==1)
			{
				for ($c=0; $c < $num; $c++)
				{
					echo $data[$c];
				}
			}
			
			else
			{
				for ($c=0; $c < $num; $c++)
				{
					echo $data[$c];
					
					if ($c==($num-1))
					{
					
					}
					
					else
					{
						echo "^";	
					}
				}
			}
			?>
			
			<?php
						
			$rowNo++;
		}
	}	
?>
</textarea><br />
<input type="submit" name="Edit" value="Save" />
</form>

Save.php

<?php
$events=$_POST['events'];

$myFile = "events.csv";
$fh = fopen($myFile, 'w') or die("can't open file");
$events=str_replace("&rsquo;","'",$events);
$events = stripcslashes($events);
fwrite($fh,$events);
fclose($fh);
header('Location:edit.php');
?>

Example of events.csv

Events / Summer 2010
Milkshake Disco^A disco for 6 to 10 year olds^Friday 28th May^Friday 25th June
Summer Project^A project running from Mon - Thurs for 6 to 10 year olds^August 2nd to 26th

What do I do to get rid of the lines that are added?

Cheers,
Ben

Recommended Answers

All 2 Replies

When you are opening whole file in textarea then why you are treating it as csv, simply load text area with content, modify and save. I hope following code will help you

<form action="save.php" method="post">
<textarea name="events" cols="100" rows="10" style="text-align:left">
<?php 
	$handle=fopen("events.csv","r");
	echo fread($handle,filesize("events.csv"));
	fclose($handle);

?>
</textarea><br />
<input type="submit" name="Edit" value="Save" />
</form>

That code worked straight away. I don't understand why the lines were appearing but it doesn't happen with you code :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.