从数据库中检索数据,但结果为“数组”


retrieve data from the database but the result of " array"

我试图从模型中调用数据,但是当运行结果时,查看单词"数组"。 有谁可以帮助我吗? 我正在使用点火器

控制器

$data=array('pengunjung' => $this->mcrud->jumlah_visitor(),
            'isi'        =>'user/monitoring');
$this->load->view('layout/wrapper', $data); 

function jumlah_visitor() {
    $date = date("Ymd");
    $this->db->where('date',$date);
    $this->db->group_by(array('ip'));           
    $ambil= $this->db->get('tbcounter');
    if ($ambil->num_rows() > 0) {
        foreach ($ambil->result_array() as $data) {
            $hasil[] = $data;
        }
        return $hasil;
    }   
}

视图

<div class="pull-left">Hari Ini : </div>
<div class="pull-right number"> <?php echo $pengunjung; ?></div>

结果

哈里伊尼:阵列

首先检查$this->mcrud->jumlah_visitor()的返回值,然后打印值。如果它是一个数组,你必须为此使用循环。

<?php echo $pengunjung; ?>

<?php print_r($pengunjung); ?>

$pengunjung变量是数组而不是字符串变量

当然是的...
在您的Model中,您要返回一个数组

 foreach ($ambil->result_array() as $data) {
            $hasil[] = $data;
        }
        return $hasil;

你试图在你的controller $this->mcrud->jumlah_visitor(),中捕捉到的一样 像$data->index_key一样归还您的index-key

尝试打印数组,因为pengunjung持有an array,而不是简单的variable

echo "<pre>";
print_r($this->mcrud->jumlah_visitor);
echo "</pre>";

您将获得完整的阵列信息。

不能回显数组。 应使用循环来显示$pengunjung的结果。在视图中只需使用

foreach( $pengunjung as $key => $value) { //echo $value; }

这样重写模型函数

function jumlah_visitor() {
    $date = date("Ymd");
    $this->db->where('date',$date);
    $this->db->group_by(array('ip'));           
    $ambil= $this->db->get('tbcounter');
     $data = $ambil->result_array();
    if (data->num_rows() > 0) {
        foreach ($data as $data1) {
            $hasil[] = $data1;
        }
        return $hasil;
    }   
}