3

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?

3
  • 1
    It's means that $this->admin_model->harga() returns an array, but your code expects it to be a string. Check your model's method to see why this happens. Commented Jun 8, 2014 at 23:26
  • Try print_r($result); and see what is returned Commented Jun 8, 2014 at 23:30
  • my model just select the query from database, but i don't know where it's getting wrong. Commented Jun 8, 2014 at 23:39

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

Comments

0

you can try code :

$result = $this->admin_model->harga();
foreach($query->result() as $row) {
    $data['harga']= $row->harga;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.