非常感謝回答my question before笨失敗調用功能一個控制器內
所以,我有一個問題在我笨,它仍然在同一個項目,只是在我的數據庫統計數據行。
我想要做的是在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
我做了什麼錯?有什麼我失蹤? 所以我不能在我的控制器內創建多個功能? 謝謝你。
UPDATE
我跟着SATY指南和它的工作。非常感謝! 由於這是一個表格,我想添加其他功能,所以我將其添加像這樣:
<?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
這裏說我需要稱之爲一次......哪一個?我把代碼弄錯了嗎?
你還在打索引嗎?如果是這樣,那麼錯誤是正確的。如果你仍然想要索引方法,只需簡單地在那裏包含聲明。 – Kisaragi