dongrong8972 2013-05-19 04:44
浏览 40
已采纳

为什么我在CodeIgniter中遇到“数组到字符串转换”错误? [关闭]

I'm currently having a problem with this error:

array to string conversion

Here's the code:

controller:

function get_tariff()
{      
   $this->load->model('model_tariff');
   $data['destination']=$this->input->post('dest',true);
   $data['lines']=$this->input->post('lines',true);
   $data['weight']=$this->input->post('weight',true);
   $data['priceperkg']=$this->model_tariff->get_tariff();
   $data['pricetotal']=$data['priceperkg']* $data['weight'];
   $this->load->view('tariff_result',$data);
}

model:

function get_tariff()
{        
   $destination=$this->input->post('dest');
   $lines=$this->input->post('lines');
   $weightt=$this->input->post('weight');
   $this->db->select('price');
   $this->db->from('view_tariff');
   $this->db->where('city_name',$destination);
   $this->db->where('lines',$lines);
   $price=$this->db->get();
   return $price->result();    
}

view:

Price per kg <?php echo $priceperkg?>;
Bayar total <?php echo $pricetotall?>;
  • 写回答

2条回答 默认 最新

  • doubi7306 2013-05-19 04:56
    关注

    The CodeIgniter database method result() returns an array of objects, not a string literal (price). You need to do some further transformations. For example, if you are expecting a single row, then try row() which returns a single object. In turn, you can then reference the property price:

    return $price->row()->price;
    

    Or, you can treat this several other ways.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?