1

I tried to call up data from the model, but when running the results in view the word "array". How do I present the data in my view?

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

1
  • 3
    $this->mcrud->jumlah_visitor() returns an array as you have defined over there in your method, treat it as such, loop over when needed Commented Feb 11, 2016 at 7:28

6 Answers 6

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0
<?php echo $pengunjung; ?>

to

<?php print_r($pengunjung); ?>

$pengunjung variable is array not string variable

Comments

0

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.

1 Comment

thanks @Vijay Sai Chaudary, but not work still the same result
0

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

Comments

0

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

Comments

0

Your model method isn't broken, but it can be refined. You should remove the condition, the loop, and use method chaining to return an array of zero or more arrays to the controller.

public function pengunjungHariIni(): array
{
    return $this->db
        ->group_by('ip')
        ->get_where('tbcounter', ['date' => date('Ymd')])
        ->result_array();
    // "SELECT * FROM `tbcounter` WHERE `date` = '20250306' GROUP BY `ip`"
}

Your mcrud model name indicates that your model class doesn't have a narrow topical concern. A well designed model class will not be used to access all database tables -- "god classes" are a bad idea for maintainability. Model classes should be logically devoted to a specific subset of data retrieval tasks in the context of your application.

We only see one method here, but maybe your model pertains to traffic or users or visitors. Maybe your model class and filename should be Visitor_model (Jumlah_model). I'll adjust the controller method with this in mind.

$this->load->model('Jumlah_model', 'JumlahModel');
$this->load->view(
    'layout/wrapper',
    [
        'pengunjung' => $this->JumlahModel->pengunjungHariIni(),
        'isi' =>'user/monitoring',
    ]
);

In your view layer, the $pengunjung array will be unconditionally defined as an array. Use a foreach() loop to access the data from each row and present whatever data you wish.

<div class="pull-left">Today: </div>
<div class="pull-right number">
    <?php
    if (!$pengunjung) {
        echo "Tiada pelawat lagi"; // no visitors yet
    } else {
        echo '<table>'
            foreach ($pengunjung as $row) {
                printf('<tr><td>%s</td><td>%d</td></tr>', $row['ip'], $row['jumlah']);
            }
        echo '</table>';
    }
    ?>
</div>

For those who haven't translated the Malay to English, jumlah is amount, pengunjung is visitors, and hari ini is today.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.