2012-12-14 26 views
1

我在使用update_batch發佈時收到此錯誤。我已經看到了其他職位,但我的行參考是DB_active_rec.php文件,其他人在他們的MC中看到它。該錯誤不會阻止發佈的update_batch,並且它會爲每個$ rb_items返回錯誤。 enter image description hereCodeigniter數組到字符串轉換錯誤

控制器:

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)) 爲有三個品牌的視圖。無論錯誤如何,所有這些都可以正確發佈。

回答

0

您的html標籤name在名稱attributs後面有[],所以您將在發佈後獲得數組值。這就是你得到這個錯誤。

只是從名稱屬性中刪除[]並提交表單。

//在名稱後面使用[]是否有任何具體原因?

+0

據我所知沒有[]沒有數組被創建。 'print_r($ rb_items)'返回數組()。 'print_r($ rb_items)'with []返回Array([0] => Array([id] => 223 [brand] => Q&V [rating] => 1r [comment] => 1 [tasted] => 1 )[1] => Array([id] => 224 [brand] => Quick Check [rating] => 2r [comment] => 2 [tasted] => 1))當我檢查兩個產品時。 – vizyourdata

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

不要忘記,雖然驗證!

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; 

}