dhara
dhara

Reputation: 248

how to solve trying to get property of non object in codeigniter framework

I am trying to get the last inserted amount to be displayed on textbox.. Controller code:

    $this->db->select('amount');
    $this->db->order_by("recno", "desc");
    $this->db->limit(1);
    $query = $this->db->get('daybook');
    $data['r']=$query->result_array(); 

View code:

<input type="text" placeholder="Credit" name="amount1" id="TDAmt" class="form-control input-xs sum2" value="<?php echo $r->amount; ?>"  >

I am getting the below error

    Message: Undefined variable: r
    Message: Trying to get property of non-object

How can i solve this..Help me to achieve this..

Upvotes: 0

Views: 34

Answers (2)

Golwin
Golwin

Reputation: 157

If the result will be only one, you can use 'row_array' instead of 'result_array'. If you use row array you can access the value by $r['amount'] If it is result array you can access the value by $r[0]['amount']

Upvotes: 1

Kousher Alam
Kousher Alam

Reputation: 1055

Your error message is saying Message: Trying to get property of non-object. You also calling $data['r']=$query->result_array(); So it is not an object, It is an associative array. As it is not an object rather than it is a associative array so you can not use -> Operator. You need to access value like $r['amount'].

You also can value check in your template file. like this <?php echo isset($r) ? $r['amount'] : ''; ?>

You can also var_dump before printing in the template.

I think it will help you. Thank you.

Upvotes: 1

Related Questions