问题
I have repetitive statements and need help to simplify or combine statements They all have similar values and range from jan - dec and the item (sales change to different category in this example sales change to ncvat) changes for 32 different categories and each group has a different submit value
if(isset($_POST['submit1'])){
$xml->sales->jan = $_POST['jan'];
file_put_contents("2020/data.xml", $xml->asXML());
}
...................................
...................................
if(isset($_POST['submit1'])){
$xml->sales->dec = $_POST['dec'];
file_put_contents("2020/data.xml", $xml->asXML());
}
then I have
if(isset($_POST['submit2'])){
$xml->ncvat->jan = $_POST['jan'];
file_put_contents("2020/data.xml", $xml->asXML());
}
...................................
...................................
if(isset($_POST['submit2'])){
$xml->ncvat->dec = $_POST['dec'];
file_put_contents("2020/data.xml", $xml->asXML());
}
And so it carries on for 32 different form submit action
回答1:
Usually when you have a lot of repetitive tasks, a loop is your best option. In this case, I think 2 loops will solve your problem.
//list of "categories". This also dictates how many outer-loops there will be.
//Duplicates categories are allowed if needed.
$types = [
'sales',
'ncvat',
//...etc
];
//list of months
$months = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'];
//loop each `$types`, and use `$key + 1` as an indicator for which "submit value" you are processing
foreach($types as $key => $type)
//to start $sub at `1` instead of `0`
$submit_value = $key + 1;
//check if submit value exists for current loop (e.g $_POST['submit1'])
if(isset($_POST["submit{$submit_value}"])) {
//loop each month
foreach($months as $month) {
//update xml for current month in current submission loop
$xml->{$type}->{$month} = $_POST[$month];
}
}
}
//submit all changes at once instead of overwriting on each inner-loop.
file_put_contents("2020/data.xml", $xml->asXML());
回答2:
I would do something like that :
$months = [
'jan',
'dec',
...
];
$numberOfIterations = 32;
for ($i = 1 ; $i <= $numberOfIterations ; $i++) {
if (isset($_POST["submit{$i}"])) {
foreach ($months as $month) {
$xml->ncvat->$month = $_POST[$month];
file_put_contents("2020/data.xml", $xml->asXML(), FILE_APPEND);
}
}
}
You can change "ncvat" by, for example, setting up an "hidden" field in your form. Like :
<input type="hidden" name="type" value="ncvat">
and then :
$months = [
'jan',
'dec',
...
];
$numberOfIterations = 32;
for ($i = 1 ; $i <= $numberOfIterations ; $i++) {
if (isset($_POST["submit{$i}"])) {
$type = $_POST['type'];
foreach ($months as $month) {
$xml->$type->$month = $_POST[$month];
file_put_contents("2020/data.xml", $xml->asXML(), FILE_APPEND);
}
}
}
回答3:
If you can pass sales
, ncvat
etc. as type
via a hidden input or the value of the submit buttons with the same name then it is much easier:
$months = ['jan', 'feb']; //etc...
$types = ['sales', 'ncvat']; //etc...
foreach($months as $month) {
if(!empty($_POST['type']) && !empty($_POST[$month]) && in_array($_POST['type'], $types)) {
$xml->{$_POST['type']}->{$month} = $_POST[$month];
file_put_contents("2020/data.xml", $xml->asXML()); //maybe this goes after the loop?
}
}
If you can't pass the type
in the form then you can do this to get the submit:
$types = ['submit1' => 'sales', 'submit2' => 'ncvat']; //etc...
$type = reset(array_intersect_key($types, $_POST));
However, since you only show 2020/data.xml
and don't specify FILE_APPEND
, if that is the only file it will be overwritten each time through the loop and you will only have dec
data. Maybe you have separate files? Or build the XML and write it only once?
来源:https://stackoverflow.com/questions/62537544/how-can-i-combine-or-simplify-multiple-statements