Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何组合或简化多个语句_Php_Post_Submit_File Put Contents - Fatal编程技术网

Php 如何组合或简化多个语句

Php 如何组合或简化多个语句,php,post,submit,file-put-contents,Php,Post,Submit,File Put Contents,我有重复的报表,需要帮助简化或合并报表。它们都有类似的值,范围从1月到12月,并且32个不同类别的项目(本例中销售更改为不同类别,销售更改为ncvat)发生了更改,每个组有不同的提交值 if(isset($_POST['submit1'])){ $xml->sales->jan = $_POST['jan']; file_put_contents("2020/data.xml", $xml->asXML()); } ............

我有重复的报表,需要帮助简化或合并报表。它们都有类似的值,范围从1月到12月,并且32个不同类别的项目(本例中销售更改为不同类别,销售更改为ncvat)发生了更改,每个组有不同的提交值

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种不同的表单提交操作

通常,当您有大量重复任务时,循环是最佳选择。在这种情况下,我认为两个循环将解决您的问题

//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());

通常当你有很多重复性的任务时,循环是你最好的选择。在这种情况下,我认为两个循环将解决您的问题

//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);
      }
   }
}
$months=[
“一月”,
“十二月”,
...
];
$numberOfIterations=32;
对于($i=1;$i ncvat->$month=$_POST[$month];
文件内容(“2020/data.xml”,$xml->asXML(),文件附加);
}
}
}
例如,您可以通过在表单中设置“隐藏”字段来更改“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);
      }
   }
}
$months=[
“一月”,
“十二月”,
...
];
$numberOfIterations=32;
对于($i=1;$i$type->$month=$_POST[$month];
文件内容(“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);
      }
   }
}
$months=[
“一月”,
“十二月”,
...
];
$numberOfIterations=32;
对于($i=1;$i ncvat->$month=$_POST[$month];
文件内容(“2020/data.xml”,$xml->asXML(),文件附加);
}
}
}
例如,您可以通过在表单中设置“隐藏”字段来更改“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);
      }
   }
}
$months=[
“一月”,
“十二月”,
...
];
$numberOfIterations=32;
对于($i=1;$i$type->$month=$_POST[$month];
文件内容(“2020/data.xml”,$xml->asXML(),文件附加);
}
}
}

如果您可以通过隐藏输入或同名提交按钮的值将
销售
ncvat
等传递为
类型
,则更容易:

$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?
    }
}
如果无法在表单中传递
类型
,则可以执行此操作以获得提交:

$types  = ['submit1' => 'sales', 'submit2' => 'ncvat']; //etc...
$type = reset(array_intersect_key($types, $_POST));

但是,由于您只显示
2020/data.xml
,并且没有指定
FILE\u APPEND
,如果这是唯一的文件,那么每次循环都会覆盖它,并且您只有
dec
数据。可能您有单独的文件?或者构建xml并只写一次?

如果您可以通过
sales
ncvat
等。通过隐藏输入或同名提交按钮的值,键入,则更容易:

$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?
    }
}
如果无法在表单中传递
类型
,则可以执行此操作以获得提交:

$types  = ['submit1' => 'sales', 'submit2' => 'ncvat']; //etc...
$type = reset(array_intersect_key($types, $_POST));


但是,由于您只显示
2020/data.xml
,并且没有指定
FILE\u APPEND
,如果这是唯一的文件,那么每次循环都会覆盖它,并且您只有
dec
数据。可能您有单独的文件?或者构建xml并只写入一次?

是否可以在每个表单上设置隐藏输入如果值为
sales
ncvat
或所有提交按钮的名称都相同
submit
,但其中一个按钮作为值,则会更容易。此外,您每次都会覆盖
数据.xml
,并且它只包含
dec
数据。或者,您是否每个月都有不同的文件、类型或在?您放弃了吗?是否可以在每个表单上设置一个值为
sales
ncvat
的隐藏输入,或者让所有提交按钮都有相同的名称
submit
,但其中一个作为值?这样会更容易。此外,您每次都会覆盖
数据.xml
,并且只包含
dec
数据。或者每个月有不同的文件和类型吗?您放弃了吗?“项目(本例中销售更改为不同类别销售更改为ncvat)”除了可以是
ncvat
sales
或其他30个字段之外。请注意,客户端的某个人可以轻松地将
类型
输入字段更改为他们想要的任何内容,您可能希望在服务器端用一系列可能的值来验证这一点。@GrumpyCrouton您是对的,切换情况会更好of$type=$\u POST['type']这将在每次迭代中覆盖文件
2020/data.xml
,这将使除最后一次迭代外的每次迭代都毫无意义。如果要附加到该文件,请添加
文件\u append
标志“该项目(本例中销售更改为不同类别销售更改为ncvat)”除了可以是
ncvat
sales
或其他30个字段之外。请注意,客户端的某个人可以轻松地将
类型
输入字段更改为他们想要的任何内容,您可能希望在服务器端用一系列可能的值来验证这一点。@GrumpyCrouton您是对的,切换情况会更好of$type=$\u POST['type']这将在每次迭代中覆盖文件
2020/data.xml
,这将使除最后一次迭代外的每次迭代都毫无意义。如果要附加到它,请添加
文件\u append
标记。我只需查看我的表单结构。我在输入字段中回显了xml文件值-带有submit1(销售=始终提交1)和提交2(ncvat=always submit 2。因此,所有值都是通过echo语句从xml文件中预填充的。因此,如果我更改一个月值,则月份值将被新值覆盖,其余月份值将被相同的echo值覆盖,希望这将简化我的代码。如果您无法更改表单,则添加了一个替代选项(这将是最好的)。我只看一下我的表单结构。我的输入字段中回显了xml文件的值-submit1(sales=always submit1)和submit2(ncvat=always submit2)。因此,所有值都是通过echo语句从xml文件中预填充的。因此,如果我更改