Codeigniter:在视图中查看mysql数据

时间:2018-06-27 13:39:14

标签: php codeigniter

我有问题。 我无法在视图中显示我的assoc表。 print_r表示只有一个uname, 控制器:

    $this->db->select('uname, uid, content, date');
    $this->db->join('users','posts.p_uid = users.uid');
    $this->db->from('posts');
    $posts = $this->db->get();
    foreach($posts->result_array() as $row){
        $data[] = $row;
    }
    print_r($data);
    $this->load->view('posts',$data);

查看:

<div class="col-md-6 col-lg-4 border p-0 offset-md-4 ">
<div class="col-md-12 col-lg-12 border">
<?php print_r($uname); ?>
</div> 
</div>

print_r来自控制器的结果:

Array ( 
    [0] => Array ( 
        [uname] => admin 
        [uid] => 6 
        [content] => test test test test test test test test 
        [date] => 2018-06-27 
    ) 
    [1] => Array ( 
        [uname] => admin 
        [uid] => 6 
        [content] => test2 test2 test2 test2 test2 test2 test2 test2 test2 test2 
        [date] => 2018-06-26 
    ) 
    [2] => Array ( 
        [uname] => admin2 
        [uid] => 7 
        [content] => test3 test3 test3 test3 test3 test3 test3 test3 
        [date] => 2018-06-17 
    ) 
)

print_r来自以下视图的结果:admin2

1 个答案:

答案 0 :(得分:1)

您没有将uname参数传递给视图:

检查以下内容:

foreach($posts->result_array() as $row){
        $data['uname'][] = $row;      // Set the parameter to pass to your view will be array
    }

这将给出array中的uname, uid, content, date

如果您想将字段值传递给视图,则执行以下操作可能会有所帮助。

我认为您的回答如下(根据您的评论)。

foreach($posts->result_array() as $row){
            $data['uname'][] = $row['uname'];      // Set the parameter to pass to your view will be your value
        }