传递给控制器​​的对象数组,在视图中以列表形式查看和显示它们

时间:2017-04-23 07:02:31

标签: php mysql codeigniter

我必须从我的表中检索多个colums,名为user_login并在视图中显示它。 我在一系列对象中收到它,我在显示数据时遇到问题。 我的模特是

$this->db->select('username,country,designation');
$this->db->from('user_login');

$this->db->join('friends','friends.friend_id=user_login.id');



$query = $this->db->get();

我的查询转储是

CI_DB_mysqli_result Object ( [conn_id] => mysqli Object ( [affected_rows] => 5 [client_info] => mysqlnd 5.0.11-dev - 20120503 - $Id: 15d5c781cfcad91193dceae1d2cdd127674ddb3e $ [client_version] => 50011 [connect_errno] => 0 [connect_error] => [errno] => 0 [error] => [error_list] => Array ( ) [field_count] => 3 [host_info] => Localhost via UNIX socket [info] => [insert_id] => 0 [server_info] => 5.5.5-10.1.19-MariaDB [server_version] => 50505 [stat] => Uptime: 4836 Threads: 1 Questions: 151 Slow queries: 0 Opens: 19 Flush tables: 1 Open tables: 13 Queries per second avg: 0.031 [sqlstate] => 00000 [protocol_version] => 10 [thread_id] => 41 [warning_count] => 0 ) [result_id] => mysqli_result Object ( [current_field] => 0 [field_count] => 3 [lengths] => [num_rows] => 5 [type] => 0 ) [result_array] => Array ( ) [result_object] => Array ( ) [custom_result_object] => Array ( ) [current_row] => 0 [num_rows] => [row_data] => )

请让我知道我应该传递给控制器​​什么,应该传递给veiw.i知道如何使用foreach循环显示数组对象中的元素。但是无论我尝试什么,它都会在视图中给出未定义的错误。 我的控制器是

$result=$this->Main_Model->display_following($user_id);






$this->load->view('following',$result);

3 个答案:

答案 0 :(得分:0)

您必须使用result_array()函数

将结果存储在varibale中
<?php

$this->db->select('username,country,designation');
$this->db->from('user_login');

$this->db->join('friends','friends.friend_id=user_login.id');
$query = $this->db->get();
$result = $query->result_array();

答案 1 :(得分:0)

$ user_id怎么样?有必要吗?我希望这就是你要求的。

IN CONTROLLER

$data['result'] = $this->Main_Model->display_following()->result_array();
$this->load->view('following',$data);

在视图中

<?php
foreach($result as $res){
    echo $res['username'].$res['country'].$res['designation']."<br/>";
}
?>

答案 2 :(得分:0)

模特:(遵循Amit Gaud的回答)

return $query->result_array();

控制器:

$data['result'] = $this->Main_Model->display_following($user_id); $this->load->view('following',$data);

查看:

<ul>
<?php 
      if(isset($result) && is_array($result) && count($result)>0)
      {
         foreach($result as $key=>$value)
         { ?>
            <li><?php echo $value['yourfield']; ?></li>
   <?php }
      }
      else
      { ?>
          <li>No Results</li>
<?php } ?>
</ul>