2016-01-20 68 views
1

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

视图本身,我使用的是引导模板,所以我没有使用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,但是我找不到关于我的问题的正确话题,所以我想感谢你们,如果你能帮助我并教我如何让这件事情起作用......

UPDATE

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

回答

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

Query Builder Class

Doc's

+0

非常感谢!有效! –

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