简体   繁体   中英

Can't Get Codeigniter Count Result to Work

I'm pretty new to Codeigniter and pretty much struggling with just this simple task which I really really really don't understand at all.

I'm using Codeigniter version 3.0.4 and I'm working on a project to create an admin page to monitored some transaction data.

What I'm trying to do is just counting data from SQL Server table... Just that...

So, here's my model:

<?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();
        }

}

?>

And then here's my controller:

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

For the view itself, I'm using a bootstrap template, so I'm not using Codeigniter's HTML table class:

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

I thought everything looks fine, but I got this error:

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

That's it guys... And like I said, I really don't understand about this. This is the first time I'm dealing with Codeigniter, other employees in my office also never dealing with Codeigniter since mostly they are desktop programmers and they're also working on another project.

I've searched Stackoverflow for this but I can't find the right topic about my problem, so I would like to thank you guys if you can help me and teach me how to get this thing to work...

UPDATE

So I followed @santosh guide... I think the model seems working, but I now got the following error in my controller:

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

This is the code in my Controller:

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

And here's the error in my view:

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

and this is what I put in my view file:

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

Try with 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();

 }

}

Using DB where

I would recommend auto load your database

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

Codeigniter Query Builder Class

Codeigniter Doc's

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

Send $data instead $testing here you go:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM