i have a Form with a table like this:
The projects can be from 1 up to 10.
| Date | project1 | project2 | projectn | holiday | ill |
|---|---|---|---|---|---|
| 2022-06-27 | 3 | 5 | |||
| 2022-06-28 | 8 |
Here is my code:
$postarray = array();
foreach ($_POST as $field => $value) {
$postdata = array($field => $value);
array_push($postarray, $postdata);
}
$count = count($postarray);
$countcalculation = $count/ 7;
$output = array_chunk($postarray, $countcalculation);
$input= new Test\Hours\HoursEntry();
$input= $input->hoursentry($output);
Here is my output:
Array
(
[0] => Array
(
[0] => Array
(
[day_0] => 2022-06-27
)
[1] => Array
(
[project1_0] => 3
)
[2] => Array
(
[project2_0] => 5
)
[3] => Array
(
[project1_n] =>
)
[4] => Array
(
[holiday_0] =>
)
[5] => Array
(
[ill_0] =>
)
[1] => Array
(
[0] => Array
(
[day_1] => 2022-06-28
)
[1] => Array
(
[project1_1] => 1
)
[2] => Array
(
[project2_1] => 2
)
[3] => Array
(
[projectn_1] => 2
)
[4] => Array
(
[holiday_1] =>
)
[5] => Array
(
[ill_1] => 8
)
)
)
I need a input in my Database like this:
| Date | project | hours | holiday | ill |
|---|---|---|---|---|
| 2022-06-27 | project1 | 5 | ||
| 2022-06-28 | 8 |
Here is my try to write into the database:
$i = 0;
foreach ($output as $postdata) {
foreach ($postdata as $post => $p) {
$count = count($postdata);
foreach ($p as $input => $value) {
$var[$i] = $value;
$name[$i] = $input;
$i++;
}
$day = date_format(new DateTime($var[0]), 'Y.m.d');
$ill = $var[$count];
$holiday = $var[$count - 1];
for ($z = 1; $z <= $count - 2; $z++) {
$hours = $var[$z];
$project = $name[$z];
// The Echo is to simulate the MySql query
echo $tag . '</br>';
echo $hours. '</br>';
echo $project . '</br>';
}
// The Echo is to simulate the MySql query
echo $ill. '</br>';
echo $holiday. '</br>';
}
}