Jump to content

Piece of Cake


bazazu

Recommended Posts

If you surround each value in double quotes, the newlines within a value will make no difference to the CSV.

 

e.g.

 

"0","0","0","0","This is some other text

that goes onto two lines"

"5","5","5","5","And here's another line"

 

However, any double quotes within the value will now cause you problems. so you need to do a str_replace('"', '""', $value) on each value too.

 

So should be something like this:

 

$string = '"' . $_POST['love1'] . '","' . $_POST['love2'] . '","' . $_POST['love3'] . '","' . $_POST['love4'] . '","' . str_replace('"', '""', $_POST['addtext']) . '"' . "\r\n";

 

 

Link to comment
Share on other sites

You should be ok if you make these changes:

 

$string = $pfw_first_row[$i] . "," . $_POST[$pfw_first_row[$i]] . "\r\n";

 

becomes

 

$string = '"' . $pfw_first_row[$i] . '","' . str_replace('"', '""', $_POST[$pfw_first_row[$i]] . '"' . "\r\n";

 

and

 

$string = rtrim($existing_file[$i], "\r\n") . "," . $_POST[$pfw_first_row[$i]] . "\r\n";

 

becomes

 

$string = rtrim($existing_file[$i], "\r\n") . ',"' . str_replace('"', '""', $_POST[$pfw_first_row[$i]] . '"' . "\r\n";

 

Link to comment
Share on other sites

You should be ok if you make these changes:

 

$string = $pfw_first_row[$i] . "," . $_POST[$pfw_first_row[$i]] . "\r\n";

 

becomes

 

$string = '"' . $pfw_first_row[$i] . '","' . str_replace('"', '""', $_POST[$pfw_first_row[$i]] . '"' . "\r\n";

 

and

 

$string = rtrim($existing_file[$i], "\r\n") . "," . $_POST[$pfw_first_row[$i]] . "\r\n";

 

becomes

 

$string = rtrim($existing_file[$i], "\r\n") . ',"' . str_replace('"', '""', $_POST[$pfw_first_row[$i]] . '"' . "\r\n";

 

 

after making the suggested changes and filling the form with data..

on first time filling the data i wrote in the text box

1st test entry into form (1st)

eg text line one (1st)

eg text line 2 (1st)

line3 (1st)

on second time filling the data i wrote into text area

2nd test entry into form (2nd)

eg text line one (2nd)

eg text line 2 (2nd)

line3 (2nd)

 

and it did not change the output sequence for the better :(. have attached the output file

 

[attachment deleted by admin]

Link to comment
Share on other sites

Sorry, I missed the closing ) on the str_replace function.

 

Try

 

$string = '"' . $pfw_first_row[$i] . '","' . str_replace('"', '""', $_POST[$pfw_first_row[$i]]) . '"' . "\r\n"; 

 

and

 

$string = rtrim($existing_file[$i], "\r\n") . ',"' . str_replace('"', '""', $_POST[$pfw_first_row[$i]]) . '"' . "\r\n";

 

If it still doesn't work, I'll get the script running here and post the code.

Link to comment
Share on other sites

Sorry, I missed the closing ) on the str_replace function.

 

Try

 

$string = '"' . $pfw_first_row[$i] . '","' . str_replace('"', '""', $_POST[$pfw_first_row[$i]]) . '"' . "\r\n"; 

 

and

 

$string = rtrim($existing_file[$i], "\r\n") . ',"' . str_replace('"', '""', $_POST[$pfw_first_row[$i]]) . '"' . "\r\n";

 

If it still doesn't work, I'll get the script running here and post the code.

 

I made the changes and re entered the same data i did previously but still no luck..

have attached the output

 

[attachment deleted by admin]

Link to comment
Share on other sites

Hi Bazazu,

 

Sorry for the delay. I tried to run your code and finally realised what was going on. As was mentioned earlier, the normal structure of a CSV file is that each record set is on a new line. You are trying to append each value onto the end of existing rows so it does take a little bit more manipulating to get it the way you want.

 

Basically, there was a problem with using fgets(). This just looks at each line individually and won't handle CSV structure when the values themselves contain new lines. Luckily, there is another function we can use to read the data correctly - fgetcsv(). This takes parameters to state how the fields are split up and enclosed.

 

I have rewritten the code and commented it so you should be able to follow along.

 

<?php
//saving record in a text file
if ($_POST)
{
$filename = "info.csv";
// array to store each line of our csv file
$csv = array();
// Let's store this as an array so we can loop through it
$fields = array("love1", "love2", "love3", "love4", "addtext");

// try to open our file
$fh = fopen($filename, "r");

if ($fh) 
{
	// if we did manage to open it
	while (!feof($fh))
	{
		// read the contents line by line
		// csv format needs to be comma separated enclosed with "
		$line = fgetcsv($fh, 1024, ',', '"');
		// and store the contents in our array
		$csv[] = $line;
	}
}
else
{
	// the file didnt exist so we need to add our field names 
	// as the first element on each line
	foreach ($fields as $i => $field)
	{
		$csv[$i][] = $field;
	}
}
// we're done reading the contents
fclose($fh);

// add the new fields to the array
foreach ($fields as $i => $field)
{
	// double up any " in our fields so they remain enclosed 
	$csv[$i][] = str_replace('"', '""', $_POST[$field]);
}

// open our file for writing
$fh = fopen($filename, "w+");

if (!$fh)
	die("Cannot open file {$filename} for writing");

// write each element in our array to a newline in the file
foreach ($csv as $row)
	fputcsv($fh, $row, ',', '"');

//we're all done
fclose($fh);

echo("<p align='center'><font face='Arial' size='3' color='#FF0000'>thanx</font></p>");
}

 

Hope this helps.

Link to comment
Share on other sites

so each element of $csv should itself be an array...

 

$csv => array (
    0 => array(
        "love1", "love2", "love3", "love4", "addtext"
    )
);

 

somehow you've got a boolean in there

 

$csv => array (
    0 => true
);

 

I don't know how this happened. The code should always populate $csv with arrays. Can you try to debug it and see where $csv[] is assigned a boolean instead?

Link to comment
Share on other sites

so each element of $csv should itself be an array...

 

$csv => array (
    0 => array(
        "love1", "love2", "love3", "love4", "addtext"
    )
);

 

somehow you've got a boolean in there

 

$csv => array (
    0 => true
);

 

I don't know how this happened. The code should always populate $csv with arrays. Can you try to debug it and see where $csv[] is assigned a boolean instead?

 

 

Is it coz of the radio buttons ??

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.