Welcome Guest, Not a member yet? Register   Sign In
Error Counting Database Data
#1

Hello, I'm pretty new to codeigniter and I've been struggling very much about this month just to make this thing work.

Currently I'm creating a web app, the purpose is monitoring some data and count all the data at specific date.

What I'm trying to do was just counting all the data in a table. So here's my approach:

Model:
PHP Code:
<? 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('data_id');
            $this->db->from('my_table');
            $this->db->where("data_id", $data_id);
                        
                        if ($data_id='5'); // if the data number is 5, then it count the data only with that ID
            echo $this->db->count_all_results();
        }
        
}
        
?>


Controller:
PHP Code:
<?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['countmydata'] = $this->dash_model->testtotal();
        
        
$this->load->view('dashboard'$countmydata);
    }




View:
PHP Code:
<thead>
                                                <tr>
                                                    <th>Bank Name</th>
                                                    <th>Total Transaction</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <tr>
                                                    <td>My Bank</td>
                                                    <td><?php echo $countmydata?></td>
                                                </tr> 
The error I got was the model doesn't declare the class name or so it seems:

Code:
An uncaught Exception was encountered

Type: RuntimeException

Message: C:\xampp\htdocs\application\models/Dash_model.php exists, but doesn't declare class Dash_model

Filename: C:\xampp\htdocs\system\core\Loader.php

Line Number: 336

Backtrace:

File: C:\xampp\htdocs\application\controllers\Dash_control.php
Line: 15
Function: model

File: C:\xampp\htdocs\index.php
Line: 292
Function: require_once

I hope someone can help me, I've been learning Codeigniter for weeks and I still don't get it...
Thank you very much...
Reply
#2

In your model above take the semi-colon off the end of the if statement.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(01-19-2016, 03:34 AM)equa24 Wrote: Model:
PHP Code:
<? class Dash_model extends CI_Model {

    // ...

?>

Code:
An uncaught Exception was encountered

Type: RuntimeException

Message: C:\xampp\htdocs\application\models/Dash_model.php exists, but doesn't declare class Dash_model

Filename: C:\xampp\htdocs\system\core\Loader.php

Line Number: 336

Backtrace:

File: C:\xampp\htdocs\application\controllers\Dash_control.php
Line: 15
Function: model

File: C:\xampp\htdocs\index.php
Line: 292
Function: require_once

Your model should start with <?php , let's give it a try.
Reply
#4

Hey, thanks a lot it works now...
Oh my god... I'm laughing right now... Smile

It solved the MVC problem, but I still can't get the query run...

I tried it with different approach by now:

MODEL:
PHP Code:
<?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();
        }
        
}
        
?>



CONTROLLER:
PHP Code:
<?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);
    }




VIEW: (I'm using a Bootstrap HTML5 Template's table, so I put the count result in one of the <td>)
PHP Code:
<thead>
 
                                               <tr>
 
                                                   <th>Bank Name</th>
                                                    <
th>Total Transaction</th>
 
                                               </tr>
 
                                           </thead>
 
                                               <tr>
 
                                                   <td>Lovely Bank</td>
 
                                                   <td><?php echo $testing?> </td>
                                                </tr> 



Still no count result showing... The error I got was:
PHP Code:
A PHP Error was encountered

Severity
Notice

Message
Undefined variabletesting

Filename
views/dashboard.php

Line Number
147

Backtrace
:

FileC:\xampp\htdocs\application\views\dashboard.php
Line
147
Function: _error_handler

File
C:\xampp\htdocs\application\controllers\Welcome.php
Line
19
Function: view

File
C:\xampp\htdocs\index.php
Line
292
Function: require_once 


Thank you for your help, guys... I really appreciate it...
Reply
#5

(01-19-2016, 07:02 PM)equa24 Wrote:
PHP Code:
A PHP Error was encountered

Severity
Notice

Message
Undefined variabletesting

Filename
views/dashboard.php

Line Number
147

Backtrace
:

FileC:\xampp\htdocs\application\views\dashboard.php
Line
147
Function: _error_handler

File
C:\xampp\htdocs\application\controllers\Welcome.php
Line
19
Function: view

File
C:\xampp\htdocs\index.php
Line
292
Function: require_once 

The error message shows that Welcome controller was called, not your Dash_control. May you have a mistake?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB