John Davidson

php - fputcsv() problem. Textarea isn't saved in one column

0 comments
Message:


Today, I found my limit in data operations knowledge.


I can successfully save input fields as well as textareas by doing this:


    $f = fopen($dir, "a");
fputcsv($f, $content, ";");
fclose($f);

Later on, I can retrieve the data and store it in array by using explode()... or so have I thought!


The problem is in me. I like BIG textareas. I am currently making a forum page and I would like to grant my visitors ability to break the line when creating a thread.


For instance:


 /*
I break the line by pressing ENTER
And again
*/

This is the code I wrote so far:


let's say, the input from the user looks like this


+---------------------------+
| Hello, |
| I find this form amazing. |
| |
| Can I get a hug? |
+---------------------------+

The user pressed ENTER 3 times making this thread..


if(isset($_POST['thread']))
{
$thread = htmlspecialchars($_POST['thread']);
$date = date('d.m.Y');

fclose($e);
$f = fopen("../data/forum-data/threads.csv", "a");
$data = array($user, $thread, $date, time(), $mail);
fputcsv($f, $data, ";", chr(127));
fclose($f);
echo '<center><div class="success-box">';
echo 'SUCCESS!';
echo '</center></div>';

$show_forum = true;
//echo "Username: " . $user . "<br>" . "Email: " . $mail;
}

in my threads.csv file, this is what's been saved:


someuser;Hello
I find this form amazing

can I get a hug?;05.12.2021;1638716270; [email protected]

This is wished:


someuser;Hello"< br >"I find this form amazing"< br >""< br >"can I get a hug?;05.12.2021;1638716270;   [email protected]

I want it to be saved in one row because the data cannot be retrieved at all. For example:


$o = fopen(../data/forum-data/threads.csv);
while(!feof($o))
{
$row = fgets($o, 4096);
$column = explode(";", $row);
foreach($column as $element)
{
echo $element . "< br >";
}
}
fclose($o);

Now, I assure you. The page is blank. Nothing gets printed out, unless the data is saved in a single row like in example from above. I am open to any solution that features php code. I have no idea about .js whatsoever.


Respectfully Regarding,


I am crying



My Answer:
't a valid argument for fputcsv() function. The fputcsv() function is used to write an array to a CSV file, not a string. If you want to write the contents of a textarea to a CSV file, you will need to first extract the data from the textarea and convert it into an array before using fputcsv().

Here is an example of how you can achieve this:

php
// Get the contents of the textarea
$textarea_content = $_POST['textarea_name'];

// Split the contents of the textarea into an array
$data = explode("\n", $textarea_content);

// Open a file for writing
$fp = fopen('output.csv', 'w');

// Loop through the array and write each line to the CSV file
foreach ($data as $line) {
fputcsv($fp, array($line));
}

// Close the file
fclose($fp);


In this example, we first get the contents of the textarea using $_POST['textarea_name']. We then split the contents into an array using the explode() function, with "\n" as the delimiter. We then open a file for writing using fopen() and loop through the array, writing each line to the CSV file using fputcsv(). Finally, we close the file using fclose().

This should allow you to write the contents of a textarea to a CSV file using PHP.

Rate this post

3 of 5 based on 2777 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us