【问题标题】:Handling form post values and array manipulation处理表单发布值和数组操作
【发布时间】:2014-07-31 09:44:30
【问题描述】:

我有这样的$_POST 值。

以下值来自表单。

我想做的是。

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

  2. 作者代码 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
    

上面的数组应该循环运行,插入语句应该是这样的。

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

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

谢谢,
金兹
PS:我什至不知道处理这个并运行一个forloop。

【问题讨论】:

  • 这有点像老笑话,我怎么去市政厅。好吧,我不会从这里开始。该数组无法帮助您处理其中的信息。因此,我将更改 字段的 HTML,以更有用的方式命名它们。但是你没有显示你的
    html。将其添加到您的问题中,您将获得更好的解决方案。
  • 不是开玩笑。我急需帮助
  • 我并不是在暗示它是。就像我说的,为
    发布你的 html,我会建议一个更好的解决方案
  • 我要做的就是让事情变得更容易,制作一个表单分组名称数组,这样我就可以在没有复杂的情况下插入值

标签: php html arrays forms arraylist


【解决方案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);
?>

使用此代码将混乱的发布请求转换为更易于操作的方式。 虽然你真的应该修改你的表单。

Test

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-19
    • 1970-01-01
    • 2014-11-19
    • 1970-01-01
    • 2018-12-06
    • 1970-01-01
    • 2016-11-23
    • 1970-01-01
    相关资源
    最近更新 更多