简体   繁体   中英

codeigniter echo query result array

Method inside the model:

    public function get_fichas(){

    $query = $this->db->query("SELECT * FROM fichas;");

    return $query->result();

}

Then, I'm trying to pass this data to the controller. Method on the controller:

public function listar_fichas(){

    $data['fichas_info'] = $this->fichas_model->get_fichas();
    $this->load->view('templates/header');
    $this->load->view('fichas/listar_fichas', $data);

}

When I try to list the data in a view, I get the following error:

"Fatal error: Cannot use object of type stdClass as array"

Here is how I'm trying to list:

View file:

<?php foreach($fichas_info as $row){?>
    <table>
        <tr>
            <td><?php echo $row['cod_produto'] ;?></td>
            <td><?php echo $row['nome_produto'] ;?></td>
            <td ><?php echo $row['versao'];?></td>
        </tr>
    </table>
<?php }?>

I think I'm doing something wrong on the view. Perhaps I'm passing the data incorrectly to the view. Can someone please tell what I'm doing wrong? Thank you!

<td><?php echo $row['cod_produto'] ;?></td>
<td><?php echo $row['nome_produto'] ;?></td>
<td><?php echo $row['versao'];?></td>

should be:

<td><?php echo $row->cod_produto ;?></td>
<td><?php echo $row->nome_produto ;?></td>
<td ><?php echo $row->versao;?></td>

The result set is an object, so each column name is a property of the object. You were accessing them as an index of an array.

You are fetching results as an object, but trying to access the data as an array. There are a couple of things you could do to resolve this:

Continue to access the data in the view by changing your foreach loop to the following:

    <?php foreach($fichas_info as $row) { ?>
        <table>
            <tr>
                <td><?php echo $row->cod_produto; ?></td>
                <td><?php echo $row->nome_produto; ?></td>
                <td><?php echo $row->versao; ?></td>
            </tr>
        </table>
    <?php endforeach; ?>

This will access the data in your object. Another option would be to change the get_fichas() function in your model to return the result as an array. Instead of using the result() function, you could use the result_array() function. To demonstrate:

    public function get_fichas(){
        $query = $this->db->query("SELECT * FROM fichas;");
        return $query->result_array();
    }

This will return an array to your controller instead of an object. You can then loop through this array in your view like you would any other array.

Have a look at http://ellislab.com/codeigniter/user-guide/database/results.html for more information on generating query results with CodeIgniter.

Do this :

<?php foreach($fichas_info as $row){?>
<table>
   <tr>
    <td><?php echo $row->cod_produto ;?></td>
     <td><?php echo $row->nome_produto ;?></td>
    <td ><?php echo $row->versao;?></td>
  </tr>
  </table>
  <?php }?>

Since you are fetching results as object. So you should use like this in your view.

<?php foreach($fichas_info as $row){?>
<table>
       <tr>
        <td><?php echo $row->cod_produto ;?></td>
         <td><?php echo $row->nome_produto ;?></td>
        <td ><?php echo $row->versao;?></td>
      </tr>
      </table>
      <?php }?>

I think you need this:

<?php foreach($fichas_info as $row){?>
<table><tr>
    <td><?php echo $row->cod_produto ;?></td>
     <td><?php echo $row->nome_produto ;?></td>
    <td ><?php echo $row->versao;?></td>
</tr></table>
<?php }?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM