[英]Codeigniter array to string conversion error
使用update_batch發布時,我收到此錯誤。 我看過其他文章,但是我的行參考是DB_active_rec.php文件,其他人在他們的MC中看到了它。 該錯誤不會阻止update_batch的發布,它會為每個$ rb_items返回錯誤。
控制器:
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 ) )
,上面有三個品牌。 不管錯誤如何,所有這些都可以正確發布。
我的CI版本是2.1.2。 在第1407行的DB_active_rec.php文件中,
$not[] = $k.'-'.$v;
應該是: $not[] = $k2.'-'.$v2;
在此處找到正確的響應: https : //stackoverflow.com/a/12910038/1738895
您的html標簽名稱在名稱后加上[],因此發布后將獲得數組形式的值。 那就是你得到這個錯誤。
只需從名稱屬性中刪除[]並提交表單即可。
//您是否在名稱后使用[]有任何特定原因?
您希望在迭代過程中擁有增量值$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'));
但是不要忘記驗證!
將數組傳遞給此函數,它將解決您的問題
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.