Welcome Guest, Not a member yet? Register   Sign In
Trying to get property of non-object
#1

[eluser]BenKxK[/eluser]
Hi guys. I did a search on the forum but found nothing that helped me so I thought I'd post Smile. Pretty new to code igniter (about a week now).

I am trying to return a row from the database and then show the contents in a view.

Here is the code I have...

Controller
Code:
function edit() {
        $edit = array();
        $query = $this->admin_model->edit_row();
        $edit['edit'] = $query;

        $edit['main_content'] = 'admin/edit_event_view';
        $this->load->view('includes/template', $edit);
    }
Model
Code:
function edit_row() {
        $this->db->where('id', $this->uri->segment(4));
        $query = $this->db->get('data');
        return $query->result();
    }

View
Code:
echo form_input('event_name', $edit->edit_name);

If i do a var dump on $edit i get:
Code:
array(1) { [0]=> object(stdClass)#17 (8) { ["id"]=> string(1) "4" ["event_name"]=> string(3) "Ben" ["info"]=> string(5) "Hello" ["location"]=> string(8) "My house" ["date"]=> string(10) "2011-03-11" ["time_from"]=> string(3) "9am" ["time_to"]=> string(4) "11am" ["organiser"]=> string(10) "Ben Stokoe" } }

(this is random input btw). Can't understand why it isn't working. Any ideas? Thanks
#2

[eluser]memVN[/eluser]
Model, you can edit as
Code:
function edit_row() {
        $this->db->where('id', $this->uri->segment(4));
        $query = $this->db->get('data');
        return $query->row();
}
#3

[eluser]InsiteFX[/eluser]
You can also simpify your controller code by doing the below.
Controller:
Code:
function edit()
{
    $edit = array();
    $edit['edit'] = $this->admin_model->edit_row();

    $edit['main_content'] = 'admin/edit_event_view';
    $this->load->view('includes/template', $edit);
}

InsiteFX
#4

[eluser]BenKxK[/eluser]
Thanks guys!

Can I ask... when getting all records I return query as result
Code:
function get_records() {
        $query = $this->db->query('SELECT * FROM data ORDER BY id DESC');
        return $query->result();
    }
But I return this one as a row... is it as simple as the first one returns more than one row? or what?

Cheers
#5

[eluser]memVN[/eluser]
Ok, if You want to return more than one row, You must use foreach in view to show content, simple
In Controller
Code:
function edit()
{
    $edit = array();
    $edit['edit'] = $this->admin_model->edit_row();

    $edit['main_content'] = 'admin/edit_event_view';
    $this->load->view('includes/template', $edit);
}

In Model
Code:
function edit_row() {
   $this->db->where('id', $this->uri->segment(4));
   $query = $this->db->get('data');
   return $query->result();
}


In page view

Code:
<?php foreach($edit as $row) : ?>
<?php echo form_input('event_name', $row->edit_name);?>
<?php echo form_input('info', $row->info);?>
<?php echo form_input('location', $row->location);?>
<?php endforeach;?>
#6

[eluser]InsiteFX[/eluser]
Code:
function get_records()
{
    $data = array();

    $this->db->order_by("id", "desc");
    $query = $this->db->get('data');

    if ($query->num_rows() > 0)
    {
        foreach ($query->result_array() as $row)
        {
            $data[] = $row;
        }
    }

    $query->free_result();
    return $data;
}

InsiteFX
#7

[eluser]BenKxK[/eluser]
Cheers guys Smile




Theme © iAndrew 2016 - Forum software by © MyBB