When submit a form, I want to write the form inputs to a text file, including a 2 columns of a html table:
function toString()
{
$filename = 'D:\file_' . time() . '_' . $_SESSION['username'] . '.txt';
$data = 'grid_blocks=' . $_POST["grid_blocks"] . "\n" .
'leverettj=' . $_POST["leverettj"] . "\n" .
'length=' . $_POST["length"] . "\n" .
'krw='. "\n";
$file = file_put_contents($filename, $data, FILE_APPEND | LOCK_EX);
if ($file === false) {
die('There was an error writing this file');
} else {
echo "$file bytes written to file";
}
}
I managed to write the single values to a .txt file, but now I want to write two html table columns also to this file. For example column values: sw1: 0.15, 0.2, 0.3 and krw: 100, 200 ,300 I want to write this values in this format to the txt file: krw=0.15%100++0.2%200++0.3%300
This is my foreach loop:
foreach ($_POST['sw1'] as $key => $value) {
if (!empty($_POST['sw1'][$key]) && ($_POST['krw'][$key])) {
$_POST['sw1'][$key];
$_POST['krw'][$key];
}
};
The foreach should be implemented after 'krw='. in the $data variable. Can somebody please point me in the right direction or with examples?
EDIT I've figured it out, this is now my code:
function toString()
{
$filename = 'D:\file_' . time() . '_' . $_SESSION['username'] . '.txt';
$tableKrw = "";
foreach ($_POST['sw1'] as $key => $value) {
if (!empty($_POST['sw1'][$key]) && ($_POST['krw'][$key])) {
$sw1=$_POST['sw1'][$key];
$sw2=$_POST['krw'][$key];
$tableKrw .= $sw1 .'&'. $sw2.'+';
}
};
$data = 'grid_blocks=' . $_POST["grid_blocks"] . "\n" .
'leverettj=' . $_POST["leverettj"] . "\n" .
'length=' . $_POST["length"] . "\n" .
'krw=' . $tableKrw . "\n";
$file = file_put_contents($filename, $data, FILE_APPEND | LOCK_EX);