faza
faza

Reputation: 257

Object of class stdClass could not be converted to int

I have error like this Object of class stdClass could not be converted to int

controller

if ($this->input->post('nama') == 'pendek boju jeans') {
                    $a=0.810;
                }else{
                    $a=1.144;
                }
                $kain= $this->input->post('id_kain');
                $b=$this->jeans_model->luaskain($kain);
                $tpotongan=round($a/$b);

model

    public function luaskain($kain)
  {
    $this->db->select("ukuran");
    $this->db->from("bahankain");
    $this->db->where('id_kain',$kain);
    $query = $this->db->get();
    return $query->row();
  }

in my model get value from bahankain.ukuran like this

|ukuran|
|800|

why error Object of class stdClass could not be converted to int and how to solve this?

Upvotes: 1

Views: 34179

Answers (3)

Decent Dabbler
Decent Dabbler

Reputation: 22783

$query->row(), in your method luaskain($kain), returns an object of type stdClass, that represents a row (one or more fields with values) from the result-set of a database query – not a single field value.

In order to access the value of a single field, in this case the field ukuran, you need to access it like $b->ukuran. You can use that value to divide with (but make sure it's non-zero, of course, because you cannot divide by zero).

Upvotes: 0

meet
meet

Reputation: 404

Try this in model

public function luaskain($kain)
  {
    $this->db->select("ukuran");
    $this->db->from("bahankain");
    $this->db->where('id_kain',$kain);
    $query = $this->db->get();
    $result=$query->row();
        if (isset($result))
        {
            return $result->columnname;//write your column name
        }
        else
        {
            return 1;
        }
  }

Upvotes: 0

u_mulder
u_mulder

Reputation: 54831

I don't know what are the names in you script mean, but the problem is that object, as I said, is a structure with at least some properties and int is just a number.

So when you divide something by object - what does it means? What value from object should be taken? Who can tell? The same is true when you divide object by something.

So, I suppose, first you should var_dump or print_r your $b variable which is object, used in a division. And then - find out what number from $b variable you should use. I suppose it's a ukuran (whatever it means).

So, more precise code is:

$tpotongan = round($a / ($b->ukuran)); // taking ukuran property from object

Upvotes: 1

Related Questions