无法使代码点火器计数结果正常工作


Can't Get Codeigniter Count Result to Work

我对Codeigniter很陌生,并且几乎在为这个我真的真的真的完全不理解的简单任务而苦苦挣扎。

正在使用Codeigniter版本3.0.4,我正在开发一个项目来创建管理页面来监视一些交易数据。

我正在尝试做的只是计算 SQL Server 表中的数据......只是...

所以,这是我的模型:

<?php class Dash_model extends CI_Model {
        public function __construct()
        {
                // Call the CI_Model constructor
                parent::__construct();
                $this->load->database();
        }
        public function testtotal()
        {
            $this->db->select('transaction_id');
            $this->db->from('dbo.transaction_table'); // I'm using MS SQL Server
            $where = "transaction_id='5' OR transaction_id='4'";
            $this->db->where($where);
            return $this->db->count_all_results();
        }
}
?>

然后这是我的控制器:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Dash_control extends CI_Controller {
    public function __construct()
       {
            parent::__construct();
            $this->load->database();
       }
    public function index()
    {
        $this->load->model('dash_model');
        $data['testing']=$this->dash_model->testtotal();
        $this->load->view('dashboard',$data);
    }
}

对于视图本身,我使用的是引导模板,所以我没有使用 Codeigniter 的 HTML 表类:

<thead>
                                                <tr>
                                                    <th>Bank Name</th>
                                                    <th>Total Transaction</th>
                                                </tr>
                                            </thead>
                                                <tr>
                                                    <td>Lovely Bank</td>
                                                    <td><?php echo $testing; ?> </td>
                                                </tr>

我以为一切看起来都很好,但我得到了这个错误:

A PHP Error was encountered
Severity: Notice
Message: Undefined variable: testing
Filename: views/dashboard.php
Line Number: 147
Backtrace:
File: C:'xampp'htdocs'application'views'dashboard.php
Line: 147
Function: _error_handler
File: C:'xampp'htdocs'application'controllers'dash_control.php
Line: 19
Function: view
File: C:'xampp'htdocs'index.php
Line: 292
Function: require_once

就是这样,伙计们...就像我说的,我真的不明白这一点。这是我第一次与Codeigniter打交道,我办公室的其他员工也从未与Codeigniter打交道,因为他们大多是桌面程序员,他们也在从事另一个项目。

我已经在 Stackoverflow 上搜索了这个,但我找不到关于我的问题的正确主题,所以我要感谢你们,如果你能帮助我并教我如何让这个东西工作......

更新

所以我按照@santosh指南...我认为该模型似乎有效,但现在我的控制器中出现以下错误:

A PHP Error was encountered
Severity: Notice
Message: Undefined variable: testing
Filename: controllers/dash_control.php
Line Number: 19
Backtrace:
File: C:'xampp'htdocs'application'controllers'dash_control.php
Line: 19
Function: _error_handler
File: C:'xampp'htdocs'index.php
Line: 292
Function: require_once

这是我的控制器中的代码:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Dash_control extends CI_Controller {
    public function __construct()
       {
            parent::__construct();
            $this->load->database();
       }
    public function index()
    {
        $this->load->model('dash_model');
        $data['testing']=$this->dash_model->testtotal();
        $this->load->view('dashboard', $testing);
    }
}

在我看来,这是错误:

A PHP Error was encountered
Severity: Notice
Message: Undefined variable: testing
Filename: views/dashboard.php
Line Number: 147
Backtrace:
File: C:'xampp'htdocs'application'views'dashboard.php
Line: 147
Function: _error_handler
File: C:'xampp'htdocs'application'controllers'dash_control.php
Line: 19
Function: view
File: C:'xampp'htdocs'index.php
Line: 292
Function: require_once

这是我放在视图文件中的内容:

<thead>
    <tr>
    <th>Bank Name</th>
    <th>Total Transaction</th>
    </tr>
</thead>
    <tr>
    <td>Lovely Bank</td>
    <td><?php echo $testing; ?></td>
    </tr>

尝试使用return num_rows();

<?php 
class Dash_model extends CI_Model {
public function __construct() {
   parent::__construct();
}
public function testtotal() {
     $this->db->select('transaction_id');
     $this->db->from($this->db->dbprefix . 'transaction_table'); 
     $this->db->where('transaction_id', '5');
     $this->db->or_where('transaction_id', '4');
     $query = $this->db->get();
     return $query->num_rows();
 }
}

使用数据库,其中

我建议自动加载您的数据库

$autoload['libraries'] = array('database');

代码点火器查询生成器类

代码点火器文档的

public function testtotal() {
        $this->db->select('transaction_id');
        $this->db->from('dbo.transaction_table'); // I'm using MS SQL Server
        $where = "transaction_id='5' OR transaction_id='4'";
        $this->db->where($where);
        $data=$this->db->get();
        return count($data);
}

发送$data而不是$testing在这里你去:

$this->load->view('dashboard', $data);