I have this code that creates a CVS file with the CF7 information the customer has provided, and it is attached into the mail I receive, the code is into functions.php.
The problem I have is that if a customer puts a comma into any field, like if he put on the name field John, Smith, it breaks the CSV
Do you have any idea how can I fix it?
//Create the shortcode which will set the value for the DTX field
function cf7dtx_counter(){
$val = get_option( CF7_COUNTER, 0) + 1; //Increment the current count
return $val;
}
add_shortcode('CF7_counter', 'cf7dtx_counter');
//Action performed when the mail is actually sent by CF7
function cf7dtx_increment_mail_counter(){
$val = get_option( CF7_COUNTER, 0) + 1; //Increment the current count
update_option(CF7_COUNTER, $val); //Update the settings with the new count
}
add_action('wpcf7_mail_sent', 'cf7dtx_increment_mail_counter');
// Add this to functions.php //
// Create CSV by scraping email using before_send_mail action //
add_action( 'wpcf7_before_send_mail', 'cf7_csv_creation' );
function cf7_csv_creation($cf7) {
// Allow to target the ID of specific form //
$form_id = $cf7->id();
// Check certain form by ID - remove this IF statement if not specific //
// Make sure the file is saved into wp-content to retieve it within WPCF7 settings as an attachement //
$user_register_csv = 'wp-content/uploads/csvs/csvreserva.csv';
// Create file contents - if you have more fields add them to the output below //
$output = "poblacio,tipus,data-entrada,data-sortida,PrimerCognom,SegonCognom,nom,Sexe,data-naixament,identificacio,Adreca,CodiPostal,Ciutat,Provincia,Pais,Telefon,Mobil,email,Estudis,Curs,Centre,Familiar,NIF-familiar,professio-familiar,Adreca-familiar,CodiPostal-familiar,Poblacio-familiar,Provincia-familiar,Pais-familiar,Mail-familiar,Telefon-familiar,Mobil-familiar,questionari-coneixament,questionari-aficions,accepto,num-registre,maildesti,titol,url,arxiu-dni,arxiu-universitat,arxiu-pagament,arxiu-dni-url,arxiu-universitat-url,arxiu-pagament,url,Submitted From,data,accepta-politica,ip,navegador";
$output .= "\n";
$output .= $_POST["residencia"] . ", ";
$output .= $_POST["menu-147"] . ", ";
$output .= $_POST["date-5"] . ", ";
$output .= $_POST["date-6"] . ", ";
$output .= $_POST["text-145"] . ", ";
$output .= $_POST["text-4445"] . ", ";
$output .= $_POST["text-144"] . ", ";
$output .= $_POST["radio-603"] . ", ";
$output .= $_POST["date-34"] . ", ";
$output .= $_POST["text-146"] . ", ";
$output .= $_POST["text-147"] . ", ";
$output .= $_POST["text-1478"] . ", ";
$output .= $_POST["text-149"] . ", ";
$output .= $_POST["text-150"] . ", ";
$output .= $_POST["menu-950"] . ", ";
$output .= $_POST["text-151"] . ", ";
$output .= $_POST["text-1521"] . ", ";
$output .= $_POST["email-775"] . ", ";
$output .= $_POST["text-200"] . ", ";
$output .= $_POST["text-201"] . ", ";
$output .= $_POST["text-202"] . ", ";
$output .= $_POST["text-301"] . ", ";
$output .= $_POST["text-302"] . ", ";
$output .= $_POST["text-313"] . ", ";
$output .= $_POST["text-303"] . ", ";
$output .= $_POST["text-304"] . ", ";
$output .= $_POST["text-305"] . ", ";
$output .= $_POST["menu-951"] . ", ";
$output .= $_POST["text-306"] . ", ";
$output .= $_POST["text-309"] . ", ";
$output .= $_POST["text-308"] . ", ";
$output .= $_POST["text-3048"] . ", ";
$output .= $_POST["text-543"] . ", ";
$output .= $_POST["text-697"] . ", ";
$output .= $_POST["Si"] . ", ";
$output .= $_POST["cf7-coutner"] . ", ";
$output .= $_POST["reserves#sample.com"] . ", ";
$output .= $_POST["Formulario recibido"] . ", ";
$output .= $_POST["https://www.sample.com"] . ", ";
$output .= $_POST["file-628"] . ", ";
$output .= $_POST["file-629"] . ", ";
$output .= $_POST["file-630"] . ", ";
$output .= $_POST[""] . ", ";
$output .= $_POST[""] . ", ";
$output .= $_POST[""] . ", ";
$output .= $_POST["1"] . ", ";
$output .= $_POST["_date"] . ", ";
$output .= $_POST["Si"] . ", ";
$output .= $_POST["_remote_ip"] . ", ";
$output .= $_POST["_user_agent"] . ", ";
$output .= $_POST["your-email"];
// Save contents to file //
file_put_contents($user_register_csv, $output);
}
// Clear file/user data after submission //
add_action('wpcf7_mail_sent', function ($cf7) {
$user_register_csv = 'wp-content/uploads/csvs/csvreserva.csv';
file_put_contents($user_register_csv, '');
// File cleared and ready to be rewritten on next submission //
});
// Remember - add the above path to the WPCF7 File attachment setting within the relevant form //
I think the guy that commented on you post has a better solution but alternatively you can add this line before you assign a value to $output
$_POST = str_replace(",","",$_POST);
This will replace all the commas in $_POST with an empty character so even if the user enters a comma it will get replaced with an empty character.
If you want to keep the commas that the user enters, you can enclose the $_POST variable in quotation marks when extending the value of the $output variable. For example:
$output .= '"'.$_POST["residencia"].'", ';
I have a function that prepares a receipt output. But since it has all kinds of conditions it ends up being very long and difficult to understand..
How would one go about refactoring this? Any ideas?
If I split this into 100 small functions would that really be better?
public static function prepare_receipt($printer)
{
if (self::hasItems($printer['id']))
{
$output = '';
if ($_POST['pre_receipt'])
{
$output .= "======== Pre receipt =======\n\n\n";
}
/**
* Time and table
*/
if ($_POST['isTakeaway'] || $_POST["isDeliveryGuys"] || $_POST["isBolt"]) {
$output .= "Table: " . $_POST['table'] . "\n";
$output .= "Floor: " . $_POST['floor'] . "\n";
$output .= "Time: " . $_POST['takeawayTime'] . "\n";
if ($_POST['order_comment']) {
$output .= "Comment: " . removeSpecialChars($_POST['order_comment']) . "\n";
}
} else {
$output .= "Table: " . $_POST['table'] . "\n\n";
$output .= "Floor: " . $_POST['floor'] . "\n\n";
if ($_POST['order_comment']) {
$output .= "Comment: " . removeSpecialChars($_POST['order_comment']) . "\n";
}
}
$output .= "------------------------\n";
/**
* Food items
*/
foreach ($_POST['orderedItems'] as $orderedItem)
{
$has_unprinted_quantity = false;
if (isset($orderedItem['last_printed_quantity'])) {
$unprinted_quantity_count = intval($orderedItem['is_printed_quantity']) - intval($orderedItem['last_printed_quantity']);
if ($unprinted_quantity_count > 0) {
$has_unprinted_quantity = true;
}
}
if ( ($orderedItem['should_print'] &&
!$orderedItem['is_printed'] &&
$orderedItem['is_visible']) ||
$_POST['pre_receipt'] ||
$has_unprinted_quantity)
{
if (is_array($orderedItem['printers'])) {
$in_printer = in_array($printer['id'], $orderedItem['printers']);
} else {
$in_printer = in_array($printer['id'], json_decode($orderedItem['printers'], true));
}
if ( $in_printer || $_POST['pre_receipt'] )
{
if ($orderedItem['is_sidedish'] && !$_POST['pre_receipt']) {
continue;
}
if ($has_unprinted_quantity) {
$output .= $unprinted_quantity_count . 'x ';
} else {
$output .= $orderedItem['quantity'] . 'x ';
}
// We ned to split it for multiple lines...
$itemDescriptionParts = self::split($orderedItem['description']);
foreach ($itemDescriptionParts as $itemDescription) {
$itemDescriptionClean = removeSpecialChars($itemDescription);
$output .= $itemDescriptionClean;
}
// Add price for pre receipt
if ($_POST['pre_receipt']) {
$output .= " - " . number_format($orderedItem['price_with_discount'], 2, '.', ',');
}
if (!$_POST['pre_receipt']) {
if ($orderedItem['comments'] != '') {
$output .= " > " . removeSpecialChars(substr($orderedItem['comments'], 0, 27)) . "\n";
}
}
/** Side dishes */
if (isset($orderedItem['side_dishes']) && !$_POST['pre_receipt'])
{
foreach ($orderedItem['side_dishes'] as $side_dish) {
$output .= "\n + " . removeSpecialChars(substr($side_dish['description'], 0, 27)) . "\n";
}
}
$output .= "\n";
}
}
}
/**
* Sums
*/
/**
* Footer
*/
$output .= "------------------------\n";
if ($_POST['pre_receipt'])
{
$output .= "\nSubtotal: " . number_format($_POST['order']['subtotal'], 2, '.', ',') . "\n";
$output .= "Discount: " . number_format($_POST['order']['discount'], 2, '.', ',') . "\n";
$output .= "Total: " . number_format($_POST['order']['total'], 2, '.', ',') . "\n\n";
}
$output .= "Time: " . getTime() . "\n";
return $output;
}
else
{
return 'EMPTY';
}
}
Any pointers in the right direction would be much appreciated.
Refactoring works often well, if it follows semantics. In your case: You made already comments for different sections. This is often a sign for a function on it's own.
Just to give you an idea: How it may look like afterwards:
$output .= create_headline(...);
$output .= create_time_table(...);
$output .= create_separator();
foreach ($_POST['orderedItems'] as $orderedItem) {
$output .= create_food_item($orderedItem, $_POST['pre_receipt'], ...);
}
$output .= create_separator();
$output .= create_footer(...);
This will save time when searching for a bug in a certain area of the receipt.
I would advice https://en.wikipedia.org/wiki/Divide-and-conquer_algorithm, and your function already has comment that indicates how this function can be divided in multiple that has a single responsability.
I would also advice not to use $_POST directly, all input data must always be validated and possibly filtered. Data from input should be passed as dependency, to adhere dependency injection, see https://phptherightway.com/ for other good practices.
I would also avoid the use of string concatenation, store all the parts within an array and then join/implode them using a separator.
Looking at your code smart use of ternary operators and shifting the orderitem loop into different function can drastically reduce your code length by half. There is no need to create function for each operation like printing head, printing tail etc as the logic in those print is pretty simple and your code can prettry muddy and difficult to navigate if there are large number of unwanted functions. You can do something like below. Also note using . (dot) operator for string concatenation make your string less readable hence prefer printing variables with {} operator.
<?php
public static function prepare_receipt($printer)
{
if (self::hasItems($printer['id']))
{
$output = isset($_POST['pre_receipt']) ? "======== Pre receipt =======\n\n\n" : "" ;
//addiing time table
$output .= "Table: {$_POST['table']}. \n Floor: {$_POST['floor']}. \n";
//adding time if it is takeaway or isDeliveryGuys or isBolt
$output .= ($_POST['isTakeaway'] || $_POST["isDeliveryGuys"] || $_POST["isBolt"]) ? "Time: {$_POST['takeawayTime']}. \n" : "" ;
//adding order comment
$output .= $_POST['order_comment']) ? "Comment: {removeSpecialChars($_POST['order_comment'])} \n" : "" ;
//print order items
this->getOrderItems($_POST[orderedItems], &$output);
// footer
$output .= "------------------------\n";
if ($_POST['pre_receipt'])
$output .= "\nSubtotal: {number_format($_POST['order']['subtotal'], 2, '.', ',')} \n Discount: { number_format($_POST['order']['discount'], 2, '.', ',') } \n Total: {number_format($_POST['order']['total'], 2, '.', ',')} \n\n";
$output .= "Time: " . getTime() . "\n";
return $output;
}
else
{
return 'EMPTY';
}
}
?>
.
I have a variable $bijlage, which is the result of a for loop. I would like a new line for every iteration.
$bijlage ="";
for($x = 0; $x < count($_POST['link']); $x++) {
$bijlage .= $_POST['link'][$x];
}
$bijlage is part of $message
$message = "$aanspreking \n \n$typinleiding\n \n $opdracht\n\n$toegevoegdelinks\n\n$bijlage\n\n\n$typbegroeting";
If it's going to be HTML output try concatenating <br> with each iteration like this:
$bijlage .= $_POST['link'][$x] . "<br />";
Or, if it is just plain text, I would do either of:
$bijlage .= $_POST['link'][$x] . "\n";
$bijlage .= $_POST['link'][$x] . chr(10);
$bijlage .= $_POST['link'][$x] . PHP_EOL;
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
I am trying to make a webpage that will take user input and add it to a .txt file. It is supposed to work like this webpage http://150.216.54.86:808/homework8/AirlineSurvey.html
Why am I receiving "Parse error: syntax error, unexpected ';'" on line 27?
<?php
$WaitTime = addslashes($_POST["wait_time"]);
$Friendliness = addslashes($_POST["friendliness"]); //missing );
$Space = addslashes($_POST["space"]);
$Comfort = addslashes($_POST["comfort"]); //missing $
$Cleanliness = addslashes($_POST["cleanliness"]);
$Noise = addslashes($_POST["noise"]);
if (empty($WaitTime) ||
empty($Friendliness) ||
empty($Space) ||
empty($Comfort) ||
empty($Cleanliness) ||
empty($Noise))
echo "<hr /><p>You must enter a value in each field. Click
your browser's Back button to return to the form.</p><hr />";
else {
$Entry = $WaitTime . "\n";
$Entry .= $Friendliness . "\n";
$Entry .= $Space . "\n";
$Entry .= $Comfort . "\n";
$Entry .= $Cleanliness . "\n";
$Entry .= $Noise . "\n";
$SurveyFile = fopen("survey.txt", "w"); /missing ;
if (flock($SurveyFile, LOCK_EX)) {
if (fwrite($SurveyFile, $Entry) > 0) {
echo "<p>The entry has been successfully added.</p>";
flock($SurveyFile, LOCK_UN;
fclose($SurveyFile);
else
echo "<p>The entry could not be saved!</p>";
}
} else
echo "<p>The entry could not be saved!</p>";
}
empty($Noise))
echo "<hr /><p>You must enter a value in each field. Click
your browser's Back button to return to the form.</p><hr />";
else {
$Entry = $WaitTime . "\n";
$Entry .= $Friendliness . "\n";
$Entry .= $Space . "\n";
$Entry .= $Comfort . "\n";
$Entry .= $Cleanliness . "\n";
$Entry .= $Noise . "\n";
$SurveyFile = fopen("survey.txt", "w"); //missing ;
//missing }
}
if (flock($SurveyFile, LOCK_EX)) {
if (fwrite($SurveyFile, $Entry) > 0) {
echo "<p>The entry has been successfully added.</p>";
flock($SurveyFile, LOCK_UN;
fclose($SurveyFile);
else
echo "<p>The entry could not be saved!</p>";
}
else {
echo "<p>The entry could not be saved!</p>";
}
}
?>
You've missed a close parenthesis ) at your flock($SurveyFile, LOCK_UN;, that's why you are receiving parse error.
It should be like this flock($SurveyFile, LOCK_UN);
So I I've tried to make this code log an exception but it gives me the error message object of class domain_model could not be converted to string
The function looks as follows:
function errorLog($log, $error_type, $string, $file, $row, $error_hash, $error_trace)
{
$text = $error_hash . ' (' . date('Y-m-d H:i') . "):\n[Error type]: " . $error_type . "\n[Message]: " . $string . "\n[File]: " . $file . "\n[Row]: " . $row . "\n";
$text .= "[Trace]:\n";
foreach ($error_trace as $t)
{
$text .= ((isset($t['type']) && isset($t['object'])) ? $t['object'] . $t['type'] . $t['function'] : $t['function']) . "\n";
$text .= $t['file'] . "\n";
$text .= $t['line'] . "\n";
$text .= print_r($t['file'], 1) . "\n";
}
file_put_contents(BASE . $log . '.log', $text, FILE_APPEND);
}
After alot of thinking eventually saw that the line in making a mess is infact this one:
$text .= ((isset($t['type']) && isset($t['object'])) ? $t['object'] . $t['type'] . $t['function'] : $t['function']) . "\n";
And as I see it the only one in need of conversion should be $t['object'] however using (string)$t['object'] didn't work and still gives me the same error. Is there any other solution on how to convert it to a string than this?
I've looked at how they suggest it to be done here