2013-05-19 26 views
0

我目前有这个错误的一个问题:为什么在CodeIgniter中出现“数组到字符串转换”错误?

array to string conversion

下面的代码:

控制器:

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

型号:

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

观点:

Price per kg <?php echo $priceperkg?>; 
Bayar total <?php echo $pricetotall?>; 
+0

在哪条线上出现此错误? – Rikesh

+0

错误的行号在哪里? –

+0

这一行$ data ['pricetotal'] = $ data ['priceperkg'] * $ data ['weight']; – mabbs

回答

1

result()返回对象的数组,而不是字符串字面(价格)的CodeIgniter的数据库的方法。你需要做一些进一步的转变。例如,如果您期待一行,则尝试使用row(),它将返回单个对象。反过来,您可以参考物业price

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

或者,你可以把这个几个其他的方式。

+0

感谢它的工作.. – mabbs

0

CodeIgniter Active Result function

该函数返回query result作为数组对象,或空数组上失败。通常你会在http://ellislab.com/codeigniter/user-guide/database/results.html用这个,像这样:

$query = $this->db->query("YOUR QUERY"); 

foreach ($query->result() as $row) 
{ 
    echo $row->title; 
    echo $row->name; 
    echo $row->body; 
} 

上述功能是result_object()的别名。

如果运行可能不会产生结果的查询,建议您先测试结果:

$query = $this->db->query("YOUR QUERY"); 

if ($query->num_rows() > 0) 
{ 
    foreach ($query->result() as $row) 
    { 
     echo $row->title; 
     echo $row->name; 
     echo $row->body; 
    } 
} 
相关问题