Php 未定义变量:codeigniter中的用户总数
我试图使用codeigniter中的num\u row()计算manajemen\u用户表中的数据量 这里是我的桌子(manajemen_用户): 这里是我的控制器:Php 未定义变量:codeigniter中的用户总数,php,codeigniter,Php,Codeigniter,我试图使用codeigniter中的num\u row()计算manajemen\u用户表中的数据量 这里是我的桌子(manajemen_用户): 这里是我的控制器: <?php defined('BASEPATH') OR exit('No direct script access allowed'); class Statistik extends CI_Controller { public function index() { //load mo
<?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);
}
}
朱姆拉用户
但在我看来,我有这样一个错误
消息:未定义变量:jumlah\u用户
消息:为foreach()提供的参数无效。
我不知道错误在哪里。对不起,我是codeigniter的新手。有人能帮我吗?谢谢。控制器
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);
}
模型
看法
朱姆拉用户
无需使用foreach。只用
echo $jumlah_user
在Codeigniter中,如果传递的数据库结果集包含多条记录,则需要使用foreach按行方式获取数据
foreach(return_data->result() as $result){
echo $result->column_name;
}
如果只有一条记录,您可以按如下方式访问它
echo $return_data->row()->column_name
如果从控制器向视图返回单个值,则可以通过变量名直接访问该值。您确定manajemen_用户表中有一些记录吗?我认为您必须检查$jumlah_user是否为_数组,然后在foreach中使用它;获取所有查询结果。您在$jumlah_user变量中出错,因为$jumlah_user的数值不是结果数组。
class Statistik_model extends CI_Model
{
function total_rows() {
$this-> db -> select("count(id) as total_count")->get('manajemen_user')->row()->total_count;
}
}
<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>
echo $jumlah_user
foreach(return_data->result() as $result){
echo $result->column_name;
}
echo $return_data->row()->column_name