简体   繁体   中英

Codeigniter array to string conversion error

I am receiving this error when posting with update_batch. I have seen other posts on this but my line reference is to the DB_active_rec.php file and others were seeing it in their MC. The error doesn't stop the update_batch from posting and it returns the error for each $rb_items. 在此处输入图片说明

Controller:

  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);
    }

Model:

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

View:

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

Has anyone seen this error or know what I am missing in my code? Thanks for the help!

print_r($rb_items) returns 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 ) ) for a view with three brands on it. All of this is posting correctly regardless of the error.

There is an issue with my version of CI which is 2.1.2. In the DB_active_rec.php file on line 1407,

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

should be: $not[] = $k2.'-'.$v2;

The correct response was found here: https://stackoverflow.com/a/12910038/1738895

Your html tags name having [] after name attributs so you will get values as an array after posting it. that is what you are getting this error.

just remove [] from name attribute and submit the form.

//Is there any specific reason you use [] after names?

What you want to have in your view is while iterating have a incremental value $i and:

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

You make an array called comments which will contain an arrays of data. So if you have two rows you will get this kind of array:

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

All you need in your controller then:

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

Don't forget about validating though!

Pass your array to this function, it should fix your problem

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;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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