问题描述
我对Codeigniter很新,并且只是简单的任务,我真的真的不理解。
我使用Codeigniter版本3.0.4和我正在一个项目创建一个管理页面来监视一些交易数据。
我想做的只是计数数据从SQL Server表...只是...
所以,这是我的模型:
< $ php class Dash_model extends CI_Model {
public function __construct()
{
//调用CI_Model构造函数
parent: :__构造();
$ this-> load-> database();
}
public function testtotal()
{
$ this-> db-> select('transaction_id');
$ this-> db-> from('dbo.transaction_table'); //我正在使用MS SQL Server
$ where =transaction_id ='5'OR transaction_id ='4';
$ this-> db-> where($ where);
return $ this-> db-> count_all_results();
}
}
?>
然后这里是我的控制器:
<?php
defined('BASEPATH')或exit('不允许直接脚本访问);
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>银行名称< / th>
< th>总交易< / th>
< / tr>
< / thead>
< tr>
< td> Lovely Bank< / td>
< td><?php echo $ testing; > < / td>
< / tr>
我认为一切都很好,但我遇到了这个错误:
遇到PHP错误
严重性:注意
消息:未定义变量:testing
文件名:views / dashboard.php
行号:147
Backtrace:
文件:C:\ xampp\htdocs \application\views\dashboard.php
行:147
功能:_error_handler
文件:C:\ xampp\htdocs\application\controllers\\ \\ dash_control.php
行:19
功能:view
文件:C:\xampp\htdocs\index.php
行:292
功能:require_once
这是家伙...
和我一样,真的不明白这个。这是我第一次处理Codeigniter,我办公室的其他员工也从来不处理Codeigniter,因为他们大多是桌面程序员,他们也在另一个项目。
我已经搜索Stackoverflow这个,但我找不到正确的话题我的问题,所以我想感谢你们,如果你可以帮助我,教我如何让这个东西工作...
UPDATE
所以我按照@santosh指南...
我认为该模型似乎工作,但我现在在我的控制器中有以下错误:
遇到PHP错误
严重性:Notice
消息:未定义变量:testing
文件名:controllers / dash_control.php
行号:19
Backtrace:
文件:C:\xampp\htdocs\application\controllers\dash_control.php
行:19
功能:_error_handler
文件:C:\xampp\htdocs\index.php
行:292
功能:require_once
这是我的控制器中的代码:
<?php
defined('BASEPATH')或exit('不允许直接脚本访问);
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);
}
}
这里是我的视图中的错误:
遇到PHP错误
严重性:注意
消息:未定义变量:测试
文件名:views / dashboard.php
行号:147
Backtrace:
文件:C: \xampp\htdocs\application\views\dplage.php
行:147
功能:_error_handler
文件:C:\ xampp\htdocs\\ \\application\controllers\dash_control.php
行:19
功能:view
文件:C:\xampp\htdocs\index.php
Line:292
功能:require_once
这是我在我的视图文件:
< thead>
< tr>
< th>银行名称< / th>
< th>总交易< / th>
< / tr>
< / thead>
< tr>
< td> Lovely Bank< / td>
< td><?php echo $ testing; ?>< / td>
< / tr>
解决方案尝试使用 return num_rows );
<?php
类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();
}
}
使用
我建议您自动加载数据库
$ autoload ['libraries'] = array );
Codeigniter
Codeigniter
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_onceThat'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_onceThis 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_onceand 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
这篇关于无法获取Codeigniter计数结果工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!