PHP - Webform to csv (splitting commas from field (not wanted)) - php

I have some PHP on my 'website' that I am using to send some data from a webform into a csv file.
It is working, but the field called 'primary_classification' contains some text which have commas in. With those classifications, it is splitting the text across two cells in the csv.
This is the PHP:
<?php
$keys = array('user_name', 'gr_num', 'primary_classification');
$csv_line = array();
foreach($keys as $key){
array_push($csv_line,'' . $_GET[$key]);
}
$fname = 'test.csv';
$csv_line = implode(',',$csv_line);
if(!file_exists($fname)){$csv_line = "\r\n" . $csv_line;}
$fcon = fopen($fname,'a');
$fcontent = $csv_line;
fwrite($fcon,$csv_line."\n");
fclose($fcon);
echo ("<SCRIPT LANGUAGE='JavaScript'>
window.alert('The classification has been added!')
window.location.href='http://localhost:8080/classification.html';
</SCRIPT>");
?>
This is the output from the csv when using 'Individual behaviour' (working), and 'Education, learning and skills' (not working).
Does anyone know where I am going wrong?
Thanks

I am pretty sure it is enough if you wrap your fields in quotes, so simply change:
array_push($csv_line,'' . $_GET[$key]);
into
array_push($csv_line, '"' . $_GET[$key] . '"');

Related

Add to textfile doesn't work

I have created a small script to add a string to a text file, but it does not seem to work.
<?php
$email = $_POST["email"];
$name = $_POST["name"];
$city = $_POST["city"];
// Concatenate all values into one string.
$subscription = strtolower(trim($email)) . "," . ucwords(trim($name)) . "," . ucwords(trim($city));
$file = "21_nieuwsbrief.txt";
file_put_contents($file, (string)$subscription, FILE_APPEND);
echo $subscription;
?>
When testing this on the server, the echo $subscription shows the correct output.
But when I go to 21_nieuwsbrief.txt, it does not show any lines of text.
Could someone help me out here?
Thanks in advance.

Combining multiple php files

The current code provided on previous question is working. I need help to modify it so I can tell it what php files to combine.
The code below combines every .php file it finds in a directory.
(ie: Need to combine only the following pages, page1.php, page2.php, page3.php, page4.php, page5.php, page6.php, page7.php, page8.php, page9.php, page10.php, page11.php, page12.php )
Thanks in advance.
<?php
foreach (glob("*.php") as $filename) {
$code.=file_get_contents("./$filename");
}
file_put_contents("./combined.php",$code);
?>
If you know the file names and they are not going to change then you can do this:
<?php
$files = array("a.php", "b.php", "c.php");
$comb;
foreach ($files as $k)
{
$comb .= file_get_contents("./".$k);
}
file_put_contents("./combined.php",$comb);
?>
If you are getting them from a form submission then do something like this:
<?php
$files = array();
foreach ($_POST['files_to_combine'] as $k)
{
$files .= $k;
}
$comb;
foreach ($files as $k)
{
$comb .= file_get_contents("./".$k);
}
file_put_contents("./combined.php",$comb);
?>
** As a security note please make sure to sanitize your inputs if you use the second method! this code is only a proof of concept to make it simple to understand and use.
I have no idea why I get negative on my question asking to have it modified seeing how the programmer that answered it copied it from another site.
I don't do this for a living and have no plans too. These are personal
things I try to do to make my job go easier and without paper.
So here is the answer.
<?php
$txt1 = file_get_contents('page-001.php');
$txt1 .= "\n" . file_get_contents('page-002.php');
$txt1 .= "\n" . file_get_contents('page-003.php');
$txt1 .= "\n" . file_get_contents('page-004.php');
$txt1 .= "\n" . file_get_contents('page-005.php');
$txt1 .= "\n" . file_get_contents('page-006.php');
$txt1 .= "\n" . file_get_contents('page-007.php');
$txt1 .= "\n" . file_get_contents('page-008.php');
$txt1 .= "\n" . file_get_contents('page-009.php');
$txt1 .= "\n" . file_get_contents('page-010.php');
$txt1 .= "\n" . file_get_contents('page-011.php');
$txt1 .= "\n" . file_get_contents('page-012.php');
$fp = fopen('newcombined.php', 'w');
if(!$fp)
die('Could not create / open text file for writing.');
if(fwrite($fp, $txt1) === false)
die('Could not write to text file.');
echo 'Text files have been merged.';
?>

Echo file content based on separator

I have a txt file with lines in this format
this is a name|this is a type|this is a description
this is a name|this is a type|this is a description
this is a name|this is a type|this is a description
I need to access those lines and echo them like this:
<li type="this is a type" description="this is a description">this is a name</li>
I have no idea how to approach this.
Thanks in advance
Normally I wouldn't write code for you without you having provided an example of what you've tried, but in this case it's pretty basic.
Use PHP's file function to read a file into an array (line by line), then use explode to break that line up:
<?php
$contents = file('yourfile.txt');
foreach($contents as $eachline) {
list($name, $type, $description) = explode("|", $eachline);
echo '<li type="' . $type . '" description="' . $description . '">' . $name . '</li>';
}
?>
PHP manual: http://us2.php.net/manual/en/function.file.php
The first step is to read every line of the file.
How to read a file line by line in php
After that explode the string by the pipe symbol $out = explode("|", $string);
after that you have an array and you can access the values with $out[0]; for example.
This is easypeasy:
$parts = explode("|", $line);
$out = "<li type='$parts[1]' description='$parts[2]'>$parts[0]</li>"

PDF to print iteration in PHP

Can anyone tell me if it's possible to iterate over an array of SimpleXML objects that contain PDF data and have each print to separate PDF files? I've been fighting with this for over a week now. My latest loop code is as follows:
foreach($xml->DocumentPDFs->DocumentPDF->PDFBytes as $PDFBytes => $value) {
$binary = base64_decode($value);
file_put_contents($xml->EnvelopeStatus->EnvelopeID . "/" . $xml->EnvelopeStatus->DocumentStatuses->DocumentStatus->Name . ".pdf", $binary,FILE_APPEND);
}
This prints out the first PDF and then exits the loop.
So it turns out it was syntax issues. Both in the base64_decode call and the file_put_contents call:
foreach($xml->DocumentPDFs->DocumentPDF as $value) {
$binary = base64_decode($value->PDFBytes);
file_put_contents($xml->EnvelopeStatus->EnvelopeID . "/" . $value->Name . ".pdf", $binary);
}
So there you go.

php file put contents reverse

Ok sorry if its a stupid question im a begginer.
Im making a small shoutbox just for practise.
It inserts the shout infos in a txt file.
My problem is that, it lists the text from top to bottom, and i would like to do this reversed.
if(isset($_POST['submit'])) {
$text = $_POST['text'];
if(!empty($text)) {
$text = $_POST['text'];
$name = $_POST['name'];
$time = date("H:i");
$content =
"<div class='text'><em>" . $time . "</em>
<span class='c11'><b>" . "<a href='userinfo_php_willbe_here.php' target='_blank'>" . htmlspecialchars($name) . "</a>" . ":</span></b>
" . htmlspecialchars($text) . "
</div>\n";
file_put_contents($file, $content, FILE_APPEND | LOCK_EX);
}
}
here is my code.
i was googleing around with not much luck maybe i wasnt looking hard enough.
could please someone give me a hint?
thank you
No way to do so with one function call. You need to read the content from your target file, prepend the data in php and rewrite the whole file (see file_get_contents).
$fileContent = file_get_contents($file);
$fileContent = $content . $fileContent;
file_put_contents($file, $fileContent, LOCK_EX);
You can also use the array_reverse like so:
// Data in file separated by new-line
$data = explode("\n",file_get_contents("filename.txt"));
foreach(array_reverse($data) as $value) {
echo $value."\n";
}
You can only prepend to a file by means of reading it and writing afterwards.
file_put_contents($file, $content . file_get_contents($file), LOCK_EX);

Categories