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

I tried to call up data from the model , but when running the results in view the word " array" . is there who can help me ? i am using codeigniter

Controller

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

Model

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

View

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

Result

Hari ini : array

Answers


First you check the return value of $this->mcrud->jumlah_visitor() after that you print the value. If it's an array you have to use loop for that.


<?php echo $pengunjung; ?>

to

<?php print_r($pengunjung); ?>

$pengunjung variable is array not string variable


Yes of-course... in your Model you are returning an array

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

The same you are trying to catch in your controller $this->mcrud->jumlah_visitor(), return your index-key just like $data->index_key

Try printing array as pengunjung is holding an array, not a simple variable.

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

You will get complete array info.


You can't echo an array. you should use a loop to display result of $pengunjung. In View just use

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


Rewrite your model function like this

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

Need Your Help

401 Response using Google Cloud API in Android App

android google-app-engine google-cloud-storage

I am attempting to use Google AppEngine with my app (to replace AWS). I have the datastore working with the following code:

Trying to figure out what the error means in this line of code I have written.

c++

I'm very new to C++ so this might be an easy question that has an obvious answer. Yet, as it is obvious, I can't seem to figure it out myself. NOTE: This is my first time asking a question here. So