非常感谢您回答my question before
所以我的Codeigniter还有另外一个问题,它仍然是同一个项目,只计算我数据库中的数据行。
我想要做的是在Controller中创建一个函数,然后在View中调用该函数。
所以这是我的原始代码:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Dash_control extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->database();
$this->load->model('dash_model');
}
public function index()
{
$data['cashtotal']=$this->dash_model->totalCash();
$data['approvedtocash']=$this->dash_model->cashapproved();
$data['rejectedtocash']=$this->dash_model->cashrejected();
$this->load->view('dashboard',$data);
}
}
然后是我的观看代码:
<thead>
<tr>
<th>Bank Name</th>
<th>Total Transaction</th>
<th>Cash Approved</th>
<th>Cash Rejected</th>
</tr>
</thead>
<tbody>
<tr class="warning">
<td>Awesome Bank</td>
<td><?php echo $cashtotal; ?></td>
<td><?php echo $approvedtocash; ?></td>
<td><?php echo $rejectedtocash; ?></td>
</tr>
</tbody>
使用上面的代码,一切正常。直到我修改了下面的代码(使用与上面相同的视图文件):
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Dash_control extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->database();
$this->load->model('dash_model');
}
public function index()
{
$this->load->view('dashboard');
}
public function anotherfunction()
{
$data['cashtotal']=$this->dash_model->totalCash();
$data['approvedtocash']=$this->dash_model->cashapproved();
$data['rejectedtocash']=$this->dash_model->cashrejected();
$this->load->view('dashboard',$data);
}
}
之后我在浏览器上收到此错误:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: cashtotal
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: 18
Function: view
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();
$this->load->model('dash_model');
}
public function index()
{
$this->CashTransSum();
$this->PayTransSum();
}
public function CashTransSum()
{
$data['cashtotal']=$this->dash_model->totalCash();
$data['approvedcash']=$this->dash_model->cashapproved();
$data['rejectedcash']=$this->dash_model->cashrejected();
$this->load->view('dashboard',$data);
}
public function PayTransSum()
{
$data['paytotal']=$this->dash_model->totalPay();
$data['approvedpay']=$this->dash_model->payapproved();
$data['acqrejectedpay']=$this->dash_model->payrejected();
$this->load->view('dashboard',$data);
}
}
然后我得到了同样的错误:
Severity: Notice
Message: Undefined variable: paytotal
Filename: views/dashboard.php
Line Number: 156
Backtrace:
File: C:\xampp\htdocs\application\views\dashboard.php
Line: 156
Function: _error_handler
File: C:\xampp\htdocs\application\controllers\dash_control.php
Line: 29
Function: view
File: C:\xampp\htdocs\application\controllers\dash_control.php
Line: 17
Function: CashTransSum
File: C:\xampp\htdocs\index.php
Line: 292
Function: require_once
这里说我需要把它叫做......哪一个?我把代码错了吗?
答案 0 :(得分:3)
您需要将anotherfunction()
功能中的index
称为
public function index()
{
$this->anotherfunction();// call your function
}
public function anotherfunction()
{
$data['cashtotal']=$this->dash_model->totalCash();
$data['approvedtocash']=$this->dash_model->cashapproved();
$data['rejectedtocash']=$this->dash_model->cashrejected();
$this->load->view('dashboard',$data);
}
答案 1 :(得分:1)
你得到的错误,这是完全正常的,你需要用$data
数组填充所有变量,试试这个:
protected $data;
public function index()
{
$this->CashTransSum();
$this->PayTransSum();
$this->load->view('dashboard', $this->data);
}
public function CashTransSum()
{
$this->data['cashtotal']=$this->dash_model->totalCash();
$this->data['approvedcash']=$this->dash_model->cashapproved();
$this->data['rejectedcash']=$this->dash_model->cashrejected();
}
public function PayTransSum()
{
$this->data['paytotal']=$this->dash_model->totalPay();
$this->data['approvedpay']=$this->dash_model->payapproved();
$this->data['acqrejectedpay']=$this->dash_model->payrejected();
}