Codeigniter数组到字符串转换错误

时间:2012-12-14 06:24:22

标签: php codeigniter

使用update_batch发布时收到此错误。我已经看到了其他帖子,但我的行引用是DB_active_rec.php文件,其他人在他们的MC中看到它。该错误不会阻止update_batch发布,它会返回每个$ rb_items的错误。 enter image description here

控制器:

  public function update_rb(){

    $rb_items = array();
        $rb_id=$this->input->post('id', TRUE);
        $rb_brand=$this->input->post('brand', TRUE);
        $rb_tasted=$this->input->post('tasted', TRUE);
        $rb_rating=$this->input->post('rating', TRUE);
        $rb_comment=$this->input->post('comment', TRUE);

        $i=0;
        foreach($rb_id as $id){
            $rb_items[$i] = array(
                'id'=>$id,
                'brand'=>$rb_brand[$i],
                'rating'=>$rb_rating[$i],
                'comment'=>$rb_comment[$i]
                );
                if(empty($rb_tasted)){
                    $rb_items[$i]['tasted']=0;
                } else if
                (in_array($id,$rb_tasted)){
                $rb_items[$i]['tasted']=1;
                } else{
                    $rb_items[$i]['tasted']=0;
                }                   
            $i++;
        }
        $this->model->update_rb($rb_items);
    }

型号:

    public function update_rb($rb_items){
        $this->rb_db->update_batch('rb_selection',$rb_items,'id');
    }

查看:

    <tr>
        <input type="hidden" name="id[]" value="<?php echo $row['id'];?>">
        <input type="hidden" name="brand[]" value="<?php echo $row['brand'];?>">
        <td><?php echo "<p>".$row['brand']."</p>";?></td>
        <td><input type="checkbox" <?php if($row['tasted'] == 1){echo "checked = checked";}?> name="tasted[]" value="<?php echo $row['id'];?>" id="tasted"/></td>
        <td><input type="text" name="rating[]" <?php echo $row['rating'];?>/></td>
        <td><textarea name='comment[]' id='comment' cols="350"><?php echo $row['comment'];?></textarea></td>
    </tr>

有没有人看到此错误或知道我的代码中缺少什么?谢谢你的帮助!

print_r($rb_items)返回Array ( [0] => Array ( [rb_id] => 192 [brand] => Napa Valley Soda Co [rating] => r0 [comment] => c0 [tasted] => 1 ) [1] => Array ( [rb_id] => 193 [brand] => Natural Brew [rating] => r1 [comment] => c1 [tasted] => 1 ) [2] => Array ( [rb_id] => 194 [brand] => Naturale 90 [rating] => r2 [comment] => c2 [tasted] => 1 ) ) 对于有三个品牌的视图。无论错误如何,所有这些都是正确发布的。

4 个答案:

答案 0 :(得分:13)

我的CI版本存在问题,即2.1.2。 在第1407行的DB_active_rec.php文件中,

此: $not[] = $k.'-'.$v;

应该是: $not[] = $k2.'-'.$v2;

这里找到了正确的答案: https://stackoverflow.com/a/12910038/1738895

答案 1 :(得分:0)

你的html标签名称在名称属性后面有[],所以你会在发布后得到数组值。这就是你得到这个错误。

从name属性中删除[]并提交表单。

//在名字后使用[]有什么具体原因吗?

答案 2 :(得分:0)

您希望视图中的内容是迭代时具有增量值$i并且:

<tr>
    <input type="hidden" name="comments[<?php echo $i;?>][id]" value="<?php echo $row['id'];?>">
    <input type="hidden" name="comments[<?php echo $i;?>][brand]" value="<?php echo $row['brand'];?>">
    <td><?php echo "<p>".$row['brand']."</p>";?></td>
    <td><input type="checkbox" <?php if($row['tasted'] == 1){echo "checked = checked";}?> name="comments[<?php echo $i;?>][tasted]" value="<?php echo $row['id'];?>" id="tasted"/></td>
    <td><input type="text" name="comments[<?php echo $i;?>][rating]" <?php echo $row['rating'];?>/></td>
    <td><textarea name='comments[<?php echo $i;?>][comment]' id='comment' cols="350"><?php echo $row['comment'];?></textarea></td>
</tr>

您创建一个名为comments的数组,其中包含一组数据。因此,如果您有两行,您将获得这种数组:

array(
    1 => array(
        'id' => '..',
        'brand' => '..',
        'tasted' => '..',
        'rating' => '..',
        'comment' => '..'
    ),
    2 => array(
        'id' => '..',
        'brand' => '..',
        'tasted' => '..',
        'rating' => '..',
        'comment' => '..'
    )
);

然后你的控制器需要的只有:

 $this->model->update_rb($this->input->post('comments'));

不要忘记验证!

答案 3 :(得分:0)

将你的数组传递给这个函数,它应该解决你的问题

function replaceArrayToString($arr = array()) {
$newArr = array();
foreach($arr as $key=>$value)
{
    if (is_array($value))
    {
       unset($arr[$key]);

        //Is it an empty array, make it a string
        if (empty($value)) {
            $newArr[$key] = '';
        }
        else {
            $newArr[$key] = $this->replaceArrayToString($value);
        }

    }
    else {
        $newArr[$key] = $value; 
    }

}
return $newArr;

}
相关问题