douzhi9939 2016-01-21 13:40
浏览 106
已采纳

Codeigniter无法调用控制器内的函数

Thanks a lot for answering my question before

So I have another problem in my Codeigniter, it's still the same project, just counting data rows in my database.

What I would like to do is creating a function in a Controller, and then call that function in a View.

So here's my ORIGINAL 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();
            $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);
    }

}

and then here's my view code:

<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>      

With the codes above, everything works fine. Until I modified my code like this one below (with the same view file like above):

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

}

After that I got this error on my browser:

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

What did I do wrong? Is there something I'm missing? So I can't create multiple function inside my controller? Thank you before.

UPDATE

I followed Saty's guide and it worked. Thanks a lot! Since this is a table, I would like to add another function, so I add it up like this:

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

}

And then I got the same error:

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 

It says here that I need to call it ONCE... Which one? Did I put the code wrong?

  • 写回答

2条回答 默认 最新

  • dssk35460 2016-01-21 13:46
    关注

    You need to call anotherfunction() inside your index function as

     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条)

报告相同问题?

悬赏问题

  • ¥50 ZYNQ7020双核FLAHS烧写的问题
  • ¥20 ue 5 中想要实现第一人称人物左右行走摆动的效果,摄像头只向右摆动一次(关键词-结点)
  • ¥15 钢岔管添加弹性约束模拟围岩作用
  • ¥15 AD9164瞬时带宽1.8G,怎么计算出来?
  • ¥20 为什么安装CSiBridge时出现了这个问题,尝试了很多方法都不行,求详细操作步骤?
  • ¥15 鼠标右键,撤销删除 复制 移动,要怎样删除
  • ¥15 使用MATLAB进行余弦相似度计算加速
  • ¥15 服务器安装php5.6版本
  • ¥15 我想用51单片机和数码管做一个从0开始的计数表 我写了一串代码 但是放到单片机里面数码管只闪烁一下然后熄灭
  • ¥20 系统工程中,状态空间模型中状态方程的应用。请猛男来完整讲一下下面所有问题