0

I tried to count the amount of data in the manajemen_user table using num_row() in codeigniter.

here my table(manajemen_user):

id|nama|username|email|password|jabatan
1|Admin|admin|[email protected]|123|Administrator

here my controller :

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Statistik extends CI_Controller
{
    public function index()
    {
        //load model
        $this->load->model('statistik_model');
        $data = array();
        $data['jumlah_user'] = $this->statistik_model->total_rows();

        //load view
        $this->load->view('backend/index',$data);
    }
}

here my model

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Statistik_model extends CI_Model
{
    function total_rows() {
        $query = $this->db->get('manajemen_user');
        return $query->num_rows();
  }
}

and here my view :

<div class="col-lg-3 col-xs-6">
          <?php foreach ($jumlah_user as $total_user):?>
          <!-- small box -->
          <div class="small-box bg-yellow">
            <div class="inner">
              <h3><?php echo $total_user ?></h3>

              <p>Jumlah User</p>
            </div>
            <div class="icon">
              <i class="ion ion-person-add"></i>
            </div>
            <a href="#" class="small-box-footer">More info <i class="fa fa-arrow-circle-right"></i></a>
          </div>
          <?php endforeach; ?>
        </div>

but i have an error like this in my view.

Message: Undefined variable: jumlah_user

Message: Invalid argument supplied for foreach()

I do not know where the mistake is. sorry i am new in codeigniter. can anyone help me? Thank you.

2
  • Are you sure you have some records in manajemen_user table? I think you must check that $jumlah_user is_array and then use it in foreach. Commented Oct 12, 2019 at 7:58
  • Use $query->result(); to fetch all query results. Commented Oct 12, 2019 at 8:01

2 Answers 2

0

Controller

function index() {
    //load model
    $this -> load -> model('statistik_model');
    $data = array();
    $data['jumlah_user'] = $this -> statistik_model -> total_rows();

    //load view
    $this -> load -> view('backend/index', $data);
}

MOdel

class Statistik_model extends CI_Model
{
    function total_rows() {
        $this-> db -> select("count(id) as total_count")->get('manajemen_user')->row()->total_count;
  }
}

View

<div class="col-lg-3 col-xs-6">

          <!-- small box -->
          <div class="small-box bg-yellow">
            <div class="inner">
              <h3><?php echo $jumlah_user ?></h3>

              <p>Jumlah User</p>
            </div>
            <div class="icon">
              <i class="ion ion-person-add"></i>
            </div>
            <a href="#" class="small-box-footer">More info <i class="fa fa-arrow-circle-right"></i></a>
          </div>

        </div>
Sign up to request clarification or add additional context in comments.

1 Comment

You are making a mistake in $jumlah_user variable , As $jumlah_user have number value not the array of results.
0

No need to use foreach. Just use

echo $jumlah_user

In Codeigniter, if you passing a database result set that has more than one record, then you need to use foreach to fetch data as row-wise.

foreach(return_data->result() as $result){
  echo $result->column_name;
}

If there are only one record, you can access it as follows.

echo $return_data->row()->column_name

If you return a single value from your controller to your view, you can directly access it by variable name.

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.