![](/img/trans.png)
[英]How can I combine multiple operator statements into one statement in PHP?
[英]How can I combine or simplify multiple statements
我有重复的语句,需要帮助来简化或合并语句 它们都有相似的值,范围从 1 月到 12 月,并且项目(在本示例中,销售额更改为不同的类别,销售额更改为 ncvat)更改了 32 个不同的类别,每个组都有一个不同的提交值
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());
}
然后我有
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());
}
因此它进行了 32 种不同的表单提交动作
通常,当您有很多重复性任务时,循环是您的最佳选择。 在这种情况下,我认为 2 个循环将解决您的问题。
//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());
我会做这样的事情:
$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);
}
}
}
例如,您可以通过在表单中设置“隐藏”字段来更改“ncvat”。 喜欢:
<input type="hidden" name="type" value="ncvat">
接着:
$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);
}
}
}
如果您可以通过隐藏输入或具有相同名称的提交按钮的值将sales
、 ncvat
等作为type
传递,那么它会容易得多:
$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?
}
}
如果您不能在表单中传递type
,那么您可以这样做来获取提交:
$types = ['submit1' => 'sales', 'submit2' => 'ncvat']; //etc...
$type = reset(array_intersect_key($types, $_POST));
但是,由于您仅显示2020/data.xml
并且未指定FILE_APPEND
,因此如果这是唯一的文件,则每次循环都会覆盖它,并且您将只有dec
数据。 也许你有单独的文件? 或者构建 XML 并且只写一次?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.