codeigniter echo query result array
Solution 1
<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.
Solution 2
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.
Solution 3
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 }?>
Solution 4
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 }?>
user1511579
Updated on March 16, 2020Comments
-
user1511579 about 4 years
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!
-
Muhammad Raheel about 11 yearsi am unable to understand what brings people to vote a simple and usual answer
-
Wesley Murch about 11 years@raheelshan I know what you mean. It is because those people can only comprehend things that are simple.