使用codeigniter更新多行

时间:2015-07-10 08:19:01

标签: php mysql codeigniter

我正在尝试在codeigniter中一次更新多行。我有以下一个我需要为每个用户更新的字段的示例

<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>

我正在努力抓住更新的价值。实际的更新语句应该不是问题,我只关心为每个用户检索正确的表单值。

    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
    }

3 个答案:

答案 0 :(得分:0)

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代码

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

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

答案 1 :(得分:0)

问题是您不了解POST如何构建阵列。从上面的HTML中,表单将构建以下POST数组:

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

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

将数组视觉布局,您现在可以迭代,以获取您的值。但是,我在你的代码中无法理解的一件事是你使用explode()的原因,你想把它变成一个数组?您可以轻松地在数据库中迭代和更新此信息,如下所示:

$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.
}

答案 2 :(得分:0)

如果您在表单处理页面上执行echo "<pre>" .print_r($_POST, true)."</pre>";,结果应该看起来像是

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

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

        )

)

所以,这应该有助于你抓住价值观:

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
}

结果这个结构:

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

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

)