Presentation of data in the view with CodeIgniter

1

I'm trying to display the data coming from my database in View. I followed the various tutorials I found on the internet but none of them could answer me: /.

Model

public function get_titles() {
    $title = $this->db->get('TB_TITLES');
    return $title->result_array();
}

Controller

public function titulosCadastrados($page = 'titulosCadastrados') {
    if (!file_exists(APPPATH.'views/pages/titulos/'.$page.'.php')) {
        show_404();
    }
    $data['titles'] = $this->title_model->get_titles();
    $this->load->view('templates/system-header');
    $this->load->view('pages/titulos/titulosCadastrados', $data);
    $this->load->view('templates/system-footer');
}

View

<tbody>
    <tr>  
        <?php foreach ($titles as $title) { ?>
            <tr><?php echo $title->name ?></tr>
        <?php } ?>
    </tr>
</tbody>

Table in the database

CREATE TABLE IF NOT EXISTS tb_titles(
ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(100) NOT NULL,
DESCRIPTION VARCHAR(500),
VALUE DOUBLE NOT NULL,
PAID_OUT_AT TIMESTAMP NOT NULL,
REGISTERED_AT TIMESTAMP DEFAULT CURRENT_TIMESTAMP(),
UPDATED_AT TIMESTAMP DEFAULT CURRENT_TIMESTAMP()
);

The displayed error is:

  

Message: Trying to get property of non-object

     

Filename: Titles / TitlesCadastrados.php

     

Line Number: 39

     

Backtrace:

     

File:   C: \ CibraProgramas \ Xampp \ htdocs \ financas \ application \ views \ pages \ titles \ titulosCadastrados.php   Line: 39   Function: _error_handler

Any idea what it might be?

    
asked by anonymous 22.01.2018 / 18:55

1 answer

1

The problem is in get_titles() you ask to be returned an array containing other arrays and not an array of objects. The way to access an array is different from accessing an object so the error in the view.

Change:

public function get_titles() {
    $title = $this->db->get('TB_TITLES');
    return $title->result_array(); //retorna um array de arrays
}

To:

public function get_titles() {
    $title = $this->db->get('TB_TITLES');
    return $title->result();
}

Documentation:

results

    
22.01.2018 / 19:20