【问题标题】:Can't Get Codeigniter Count Result to Work无法使 Codeigniter 计数结果正常工作
【发布时间】:2016-01-20 02:21:42
【问题描述】:

我对 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>

【问题讨论】:

    标签: php sql-server database codeigniter


    【解决方案1】:

    试试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

    【讨论】:

      【解决方案2】:
      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);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-02-14
        • 2012-10-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-19
        • 2014-09-11
        相关资源
        最近更新 更多