PHP 错误消息:数组到字符串的转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24111831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
A PHP Error Message: Array to string conversion
提问by Dhenta1212
i'm a newbie in codeigniter , just i got this error:
我是 codeigniter 的新手,只是我遇到了这个错误:
A PHP Error was encountered
Severity: Notice
Message: Array to string conversion
Filename: views/coba.php
Line Number: 52
and still i can't solve it. this my code on controller:
我仍然无法解决它。这是我在控制器上的代码:
public function getvalue()
{
$result = $this->admin_model->harga();
$data = array(
'harga' => $result,
);
if ($this->input->post('btnKirim')==true) {
$data['nama']=$this->input->post('nama');
$data['tgl_masuk']=$this->input->post('tgl_masuk');
$data['tgl_ambil']=$this->input->post('tgl_ambil');
$data['berat']=$this->input->post('berat');
$data['status']=$this->input->post('status');
}
$data['judul'] = 'Halaman Pegawai';
$data['content'] = 'coba';
$this->load->view('template/index',$data);
}
and this code for my model :
和我的模型的代码:
public function harga(){
$query= $this->db->query('SELECT harga from satuan');
return $query->result();
}
it's my view.php on where i got this error message:
这是我收到此错误消息的地方的 view.php:
<div class="form-group">
<label for="total" class="control-label">Total Harga</label>
<div>
<input name="total" id="total" type="numeric" class="form-control" value="<?php echo $harga; ?>" readonly='total'>
</div>
</div>
Why am I getting this message, what can I do to solve it?
为什么我会收到这条消息,我该怎么做才能解决它?
回答by hgazibara
Method resulteither returns an array of objects or an empty array. Either way, method hargareturns an array.
方法result要么返回一个对象数组,要么返回一个空数组。无论哪种方式,方法都会harga返回一个数组。
Since you're using that result to populate value of a field, it's not clear if you really want all results from a table or just some specific, maybe even just one result. Whatever you want, you need to convert that to a scalar value to be able to use echowithout warnings.
由于您使用该结果来填充字段的值,因此不清楚您是否真的想要表中的所有结果,或者只是一些特定的结果,甚至可能只是一个结果。无论您想要什么,您都需要将其转换为标量值,以便能够在echo没有警告的情况下使用。
It's possible to iterate over returned results, so you could do something like this:
可以迭代返回的结果,因此您可以执行以下操作:
public function harga() {
// Since your HTML containts 'Total harga', I'm assuming you
// have multiple results and you need to add them all up
$result = 0
foreach($query->result() as $row) {
$output += $row->harga;
}
return $result;
}
If you need only one result, consider using $query->row()to get a single row from the table (and even better, use limitor wherein your sql query).
如果您只需要一个结果,请考虑使用$query->row()从表中获取一行(甚至更好,在您的 sql 查询中使用limit或where)。
To fully understand what your problem is, check the documentation.
要完全了解您的问题是什么,请查看文档。
回答by Hasan
you can try code :
你可以试试代码:
$result = $this->admin_model->harga();
foreach($query->result() as $row) {
$data['harga']= $row->harga;
}

