How to call codeigniter controller function from view

后端 未结 14 1660
小蘑菇
小蘑菇 2020-11-27 05:53

How to call codeigniter controller function from view? When i call the function in a controller, get a 404 page.

相关标签:
14条回答
  • 2020-11-27 06:43

    You can call controller function from view in the following way:

    Controller:

    public function read() {
        $object['controller'] = $this; 
        $this->load->view('read', $object);
    }
    

    View:

    // to call controller function from view, do
    $controller->myOtherFunct();
    
    0 讨论(0)
  • 2020-11-27 06:47

    Go to the top of your View code and do it like this :

      <?php
         $this->load->model('MyModelName');
         $MyFunctionReturnValue = $this->MyModelName->MyFunctionName($param));
    ?>
    
    <div class="row">
        Your HTML CODE
    </div>
    
    0 讨论(0)
  • 2020-11-27 06:49

    We can also pass controller function as variable in the view page.

    class My_controller extends CI_Controller {
        public function index() {
               $data['val']=3;
               $data['square']=function($val){
                                   return $val*$val;
                                       };
        $this->load->view('my-view',$data);
    }
    }
    

    In the view page

    <p>Square of <?=$val?>
       <?php 
         echo $square($val); 
       ?>
    </p>
    

    The output is 9

    0 讨论(0)
  • 2020-11-27 06:52

    One idea i can give is,

    Call that function in controller itself and return value to view file. Like,

    class Business extends CI_Controller {
        public function index() {
                $data['css'] = 'profile';
    
                $data['cur_url'] = $this->getCurrURL(); // the function called and store val
                $this->load->view("home_view",$data);
         }
         function getCurrURL() {
                    $currURL='http://'.$_SERVER['HTTP_HOST'].'/'.ltrim($_SERVER['REQUEST_URI'],'/').'';
                return $currURL;
         }
    
    }
    

    in view(home_view.php) use that variable. Like,

    echo $cur_url;
    
    0 讨论(0)
  • 2020-11-27 06:54

    it is quite simple just have the function correctly written in the controller class and use a tag to specify the controller class and method name, or any other neccessary parameter..

    <?php
    if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    class Iris extends CI_Controller {
        function __construct(){
            parent::__construct();
            $this->load->model('script');
            $this->load->model('alert');
    
        }public function pledge_ph(){
            $this->script->phpledge();
        }
    }
    ?>
    

    This is the controller class Iris.php and the model class with the function pointed to from the controller class.

    <?php 
    if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    class Script extends CI_Model {
        public function __construct() {
            parent::__construct();
            // Your own constructor code
        }public function ghpledge(){
            $gh_id = uniqid(rand(1,11));
            $date=date("y-m-d");
            $gh_member = $_SESSION['member_id'];
            $amount= 10000;
            $data = array(
                'gh_id'=> $gh_id,
                'gh_member'=> $gh_member,
                'amount'=> $amount,
                'date'=> $date
            );
            $this->db->insert('iris_gh',$data);
        } 
    }
    ?>
    

    On the view instead of a button just use the anchor link with the controller name and method name.

    <html>
        <head></head>
        <body>
            <a href="<?php echo base_url(); ?>index.php/iris/pledge_ph" class="btn btn-success">PLEDGE PH</a>
        </body>
    </html>
    
    0 讨论(0)
  • 2020-11-27 06:55

    views cannot call controller functions.

    0 讨论(0)
提交回复
热议问题