【问题标题】:Codeigniter Printing Query ResultsCodeigniter 打印查询结果
【发布时间】:2012-11-08 06:14:56
【问题描述】:

简单的问题,但我不知道为什么它不能正确打印或按预期工作。我在模型中有这个(有效,因为当我 print_r($result) 它显示数据时:

function get_latest_entries()
{
    $query = $this->db->get('property');
    return $query->result_array();
}

对于控制器:

public function index()
{
    $resultSet['properties'] = $this->propertymodel->get_latest_entries();
    $this->load->view('home', $resultSet);
}

在视图中我想遍历数组(表有描述、城市、地址列):

<?php
foreach($resultSet as $item)
{
    echo $item->city;
    echo $item->description;
}
?>   

我在主页上得到两条记录,显示结果如上:

严重性:通知 消息:未定义的变量:resultSet 文件名:views/home.php 行号:16

还有

严重性:警告 消息:为 foreach() 提供的参数无效 文件名:views/home.php 行号:16

【问题讨论】:

  • 使用 $properties 而不是 $resultSet
  • 把它作为答案@ShayanHusaini,这样我就可以将它标记为一个:)

标签: codeigniter


【解决方案1】:

使用$properties 代替$resultSet

【讨论】:

    【解决方案2】:

    使用这个...您将$properties 传递给您的视图,而不是$resultSet..

    <?php
           foreach($properties as $item) // use properties
           {
            echo $item->city;
            echo $item->description;
           }
        ?>   
    

    【讨论】:

      【解决方案3】:

      这是您的代码中的问题。 -您已将数据作为数组数组返回,并且您正尝试作为对象访问, -第二个是,您已将属性变量传递给查看,并且您正在访问结果集。 - 所以这是您的代码,其中包含错误

      <?php
      foreach($resultSet as $item)
      {
          echo $item->city;
          echo $item->description;
      }
      ?> 
      

      -您的代码的正确版本在这里...

      <?php
      foreach($properties as $item)
      {
          echo $item['city'];
          echo $item['description'];
      }
      ?> 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-23
        • 2012-11-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多