无法获取Codeigniter计数结果

时间:2016-01-20 02:21:43

标签: php sql-server database codeigniter

我对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);
    }
}

对于视图本身,我使用的是bootstrap模板,所以我没有使用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,但是我找不到关于我的问题的正确话题,所以我想感谢你们,如果你能帮助我并教我如何让这个东西工作...... < / p>

更新

所以我跟着@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>

2 个答案:

答案 0 :(得分:0)

尝试使用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();

 }

}

使用DB where

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

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

Codeigniter Query Builder Class

Codeigniter Doc's

答案 1 :(得分:0)

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