简体   繁体   English

使用Codeigniter更新多行

[英]Using codeigniter to update multiple rows

I am trying to update multiple rows at a time in codeigniter. 我试图一次在Codeigniter中更新多行。 I have the following a an example of the fields I need to update for each user 以下是我需要为每个用户更新的字段的示例

<div class="row">
    <input type="text" name="id[46][firstname]" value="">
    <input type="text" name="id[46][lastname]" value="">
</div>

<div class="row">
    <input type="text" name="id[48][firstname]" value="">
    <input type="text" name="id[48][lastname]" value="">
</div>

I am struggling to grab the values for update. 我正在努力获取要更新的值。 The actual update statement shouldn't be a problem, I'm just concerned with retrieving the correct form values per user. 实际的更新语句应该不是问题,我只关心与每个用户检索正确的表单值。

    foreach($_POST as $key => $value){
        $keyparts = explode('_', $key);
        if(count($keyparts) == 2){
            switch ($keyparts[0]) {
                case 'firstname':
                    $records[$keyparts[1]]->firstname = $value;
                    break;
                case 'lastname':
                    $records[$keyparts[1]]->lastname = $value;
                    break;
                default:
                    break;
            }
        }
        // update statement to go here
    }

HTML Code HTML代码

<div class="row">
<input type="text" name="46[firstname]" value="">
<input type="text" name="46[lastname]"  value="">
</div>

<div class="row">
<input type="text" name="48[firstname]" value="">
<input type="text" name="48[lastname]" value="">
</div>

PHP Code PHP代码

$records = array();
foreach($_POST as $key => $value){

    if(is_array($value))
    {
        foreach($value as $new_key=>$new_val)
        {
            $records[$key][$new_key] = $new_val;
        }           
    }
}

The problem is that you are not understanding how your POST is building the array. 问题是您不了解POST如何构建阵列。 From you HTML above, the form will build the following POST array: 通过上面的HTML,表单将构建以下POST数组:

array =>
    [id] =>
        [46] =>
            [firstname] => "john"
            [lastname] => "doe"

        [48] =>
            [firstname] => "jane"
            [lastname] => "doet"

with the array being visually laid out like that, you can now iterate, to obtain your values. 像这样在视觉上布置数组,现在可以迭代以获得值。 However, one thing I do not understand in your code, is why you are using explode() , what are you trying to turn into an array? 但是,我在您的代码explode()不懂的一件事是,为什么要使用explode() ,您打算将什么变成数组? You can easily iterate and update this information in your database like so: 您可以轻松地在数据库中迭代和更新此信息,如下所示:

$this->load->model("my_model");
$postdata = $this->input->post();
foreach($postdata['id'] as $id => $user_info){
     $this->my_model->update_user($id, $user_info); // your model method should handle the update information by ID.
}

if you do echo "<pre>" .print_r($_POST, true)."</pre>"; 如果您确实echo "<pre>" .print_r($_POST, true)."</pre>"; on the form processing page, the result should look linke this 在表单处理页面上,结果应如下所示

    Array
(
    [id] => Array
        (
            [46] => Array
                (
                    [firstname] => firstNameValue1
                    [lastname] => lastNameValue1
                )

            [48] => Array
                (
                    [firstname] => firstNameValue2
                    [lastname] => lastNameValue2
                )

        )

)

So, this should help you catch the values: 因此,这应该可以帮助您捕获值:

foreach($_POST['id'] as $key => $value){
  $records[$key] = (object)array();
  $records[$key]->firstname = $value['firstname'];
  $records[$key]->lastname = $value['lastname'];

    // update statement to go here
}

Results this structure: 结果该结构:

Array
(
    [46] => stdClass Object
        (
            [firstname] => firstNameValue1
            [lastname] => lastNameValue1
        )

    [48] => stdClass Object
        (
            [firstname] => firstNameValue2
            [lastname] => lastNameValue2
        )

)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM