处理表单后置值和数组操作

时间:2022-07-01 12:32:55

I have $_POST values like this.

我有这样的$ _POST值。

The below value is coming from a form.

以下值来自表格。

What I would like to do is.

我想做的是。

  1. Author Code 1 and Author Code 3 has a new flag called [chkName_1] => 1
    So this value alone should get inserted into the database

    作者代码1和作者代码3有一个名为[chkName_1] => 1的新标志所以这个值应该单独插入到数据库中

  2. Author Code 2 does not have the flag and that should be ignored.

    作者代码2没有标志,应该被忽略。

    [author_code_1] => 1
    [author_name_1] => Author 1
    [chkName_1] => 1
    [author_code_2] => 2
    [author_name_2] => Author 2
    [author_code_3] => 3
    [author_name_3] => Author 3
    [chkName_3] => 1
    

The above array should run in a loop and the insert statement should look something like this.

上面的数组应该在循环中运行,insert语句看起来应该是这样的。

insert Into author_log (`author_code`,`author_name`) values (1,'Author 1');     
insert Into author_log (`author_code`,`author_name`) values (3,'Author 3');     

In other words, check the flag values and if it is set to 1 then insert into db.

换句话说,检查标志值,如果设置为1,则插入db。

Thanks,
Kimz
PS: I don't even have any idea of handling this and running a forloop.

谢谢,Kimz PS:我甚至不知道如何处理这个并运行forloop。

1 个解决方案

#1


1  

<?php
    $authors = array(
        'author_code_1' => 1,
        'author_name_1' => 'author1',
        'chkName_1' => 1,
        'author_code_2' => 2,
        'author_name_2' => 'author2',
        'author_code_3' => 3,
        'author_name_3' => 'author3',
        'chkName_3' => 1
    );

    // _ CODE STARTS HERE
    $list = array();
    foreach ($authors as $key => $entry) {
        if (strpos($key, 'author_name_') !== false) {
            $index = str_replace('author_name_', '', $key);
            $list[$index]['name'] = $entry;
        } elseif (strpos($key, 'author_code_') !== false) {
            $index = str_replace('author_code_', '', $key);
            $list[$index]['code'] = $entry;
        } else if (strpos($key, 'chkName_') !== false) {
            $index = str_replace('chkName_', '', $key);
            $list[$index]['chk'] = $entry;
        }

    }
    // ^ CODE ENDS HERE

    print_r($list);
?>

Use this code to transform that mess of a post request in something more operation-friendly. Though you should really modify your form.

使用此代码将事务请求的混乱转换为更易于操作的内容。虽然你应该真的修改你的表格。

Test

测试

#1


1  

<?php
    $authors = array(
        'author_code_1' => 1,
        'author_name_1' => 'author1',
        'chkName_1' => 1,
        'author_code_2' => 2,
        'author_name_2' => 'author2',
        'author_code_3' => 3,
        'author_name_3' => 'author3',
        'chkName_3' => 1
    );

    // _ CODE STARTS HERE
    $list = array();
    foreach ($authors as $key => $entry) {
        if (strpos($key, 'author_name_') !== false) {
            $index = str_replace('author_name_', '', $key);
            $list[$index]['name'] = $entry;
        } elseif (strpos($key, 'author_code_') !== false) {
            $index = str_replace('author_code_', '', $key);
            $list[$index]['code'] = $entry;
        } else if (strpos($key, 'chkName_') !== false) {
            $index = str_replace('chkName_', '', $key);
            $list[$index]['chk'] = $entry;
        }

    }
    // ^ CODE ENDS HERE

    print_r($list);
?>

Use this code to transform that mess of a post request in something more operation-friendly. Though you should really modify your form.

使用此代码将事务请求的混乱转换为更易于操作的内容。虽然你应该真的修改你的表格。

Test

测试

相关文章