【问题标题】:Codeigniter array to string conversion errorCodeigniter 数组到字符串的转换错误
【发布时间】:2012-12-02 03:53:51
【问题描述】:

我在使用 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] =&gt; Array ( [rb_id] =&gt; 192 [brand] =&gt; Napa Valley Soda Co [rating] =&gt; r0 [comment] =&gt; c0 [tasted] =&gt; 1 ) [1] =&gt; Array ( [rb_id] =&gt; 193 [brand] =&gt; Natural Brew [rating] =&gt; r1 [comment] =&gt; c1 [tasted] =&gt; 1 ) [2] =&gt; Array ( [rb_id] =&gt; 194 [brand] =&gt; Naturale 90 [rating] =&gt; r2 [comment] =&gt; c2 [tasted] =&gt; 1 ) ) 查看三个品牌的视图。无论错误如何,所有这些都正确发布。

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

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

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

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

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

    【讨论】:

      【解决方案2】:

      您的 html 标签名称在名称属性后具有 [],因此您将在发布后将值作为数组获取。这就是你得到这个错误的原因。

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

      //你在名字后使用[]有什么特别的原因吗?

      【讨论】:

      • 据我所知,没有 [] 不会创建数组。 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 ) )当我检查两个产品时。
      【解决方案3】:

      您希望在您的视图中拥有一个增量值 $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'));
      

      不要忘记验证!

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

          猜你喜欢
          • 2023-03-11
          • 1970-01-01
          • 1970-01-01
          • 2015-12-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多