ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Trying to get property of non-object

April 06, 2011 9:44pm

Subscribe [3]
  • #1 / Apr 06, 2011 9:44pm

    BenKxK

    3 posts

    Hi guys. I did a search on the forum but found nothing that helped me so I thought I’d post 😊. 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

    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

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

    View

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

    If i do a var dump on $edit i get:

    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 / Apr 06, 2011 11:11pm

    memVN

    30 posts

    Model, you can edit as

    function edit_row() {
            $this->db->where('id', $this->uri->segment(4));
            $query = $this->db->get('data');
            return $query->row();
    }
  • #3 / Apr 07, 2011 4:20am

    InsiteFX

    6819 posts

    You can also simpify your controller code by doing the below.
    Controller:

    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 / Apr 07, 2011 9:16am

    BenKxK

    3 posts

    Thanks guys!

    Can I ask… when getting all records I return query as result

    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 / Apr 08, 2011 12:48am

    memVN

    30 posts

    Ok, if You want to return more than one row, You must use foreach in view to show content, simple
    In Controller

    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

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


    In page view

    <?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 / Apr 08, 2011 1:20am

    InsiteFX

    6819 posts

    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 / Apr 09, 2011 7:27am

    BenKxK

    3 posts

    Cheers guys 😊

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases