简体   繁体   English

codeigniter echo查询结果数组

[英]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" “致命错误:无法使用stdClass类型的对象作为数组”

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: 通过将foreach循环更改为以下内容,继续访问视图中的数据:

    <?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. 另一个选择是更改模型中的get_fichas()函数以将结果作为数组返回。 Instead of using the result() function, you could use the result_array() function. 您可以使用result_array()函数,而不是使用result()函数。 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. 有关使用CodeIgniter生成查询结果的更多信息,请查看http://ellislab.com/codeigniter/user-guide/database/results.html

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 }?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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