Reputation: 43
i'm a newbie in codeigniter , just i got this error:
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:
<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?
Upvotes: 3
Views: 47239
Reputation: 29
you can try code :
$result = $this->admin_model->harga();
foreach($query->result() as $row) {
$data['harga']= $row->harga;
}
Upvotes: 0
Reputation: 1832
Method result
either returns an array of objects or an empty array. Either way, method harga
returns an array.
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 echo
without warnings.
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 limit
or where
in your sql query).
To fully understand what your problem is, check the documentation.
Upvotes: 6