Codeigniter - 将数据从 MY_Controller 传递到 Main_controller 以查看

Codeigniter - Passing data from MY_Controller to Main_controller to view

这是完整的代码(我想select通过MY_Controller上的一个变量集来查看): 我想传递那个变量,但它没有 "reach" 它给我的视图 $ses_group = "not_logged_in" 带有测试回显,我没有在 MY_Controller

上设置任何内容
class MY_Controller extends CI_Controller {
protected $special_data = array();
public function __construct()
{
    parent::__construct();
}
function index() {
if (logged_in() == TRUE)
{
if (in_group('users'))
{
    $this->special_data['ses_group'] = 'users';
}elseif (in_group('empresas'))
{
    $this->special_data['ses_group'] = 'empresas';
}elseif (in_group('admin'))
{
    $this->special_data['ses_group'] = 'admin';
}else{
    //  $this->special_data['ses_group'] = 'not_logged_in';
}
}
return $this->special_data;
}

Main_Controller:

function index(
$data = array(
                    'ses_group' => $this->special_data
            );
            $this->load->view('auth/descricao_anuncio', $data);
)

观点:

<?php if($ses_group="not_logged_in"){ ?>
<li><a href="<?php echo base_url("login/login")?>" class="">Login</a></li>


<?php  }elseif($ses_group="users"){ ?>

<li><a href="<?php echo base_url("users_controller/edit_user")?>">Your Area</a></li>
<li><a href="<?php echo base_url("login/logout")?>">Logout</a></li>


<?php  }elseif($ses_group="empresas"){  ?>

<li><a href="<?php echo base_url("empresas_controller/edit_empresa")?>">Empresa</a></li>
<li><a href="<?php echo base_url("login/logout")?>">Logout</a></li>

<?php  }?>

echo $ses_group;

再次感谢!

控制器

 <?php

class MY_Controller extends CI_Controller {

protected $special_data = array();

 function MY_Controller ()  {
        parent::Controller();
    }

function special_data($val)
{
if(a){
$this->special_data['ses_group'] = 'users';
}elseif(b){
$this->special_data['ses_group'] = 'companies';
}else{
$this->special_data['ses_group'] = 'admin';
}
return $this->special_data;// return value of the function
}

}

您可以通过在您的 MY_Controller

中使用 $this-> 将您的值从控制器发送到视图

控制器

 class Main_controller extends MY_Controller {
      function  __construct()  {
        parent::MY_Controller();
    }


      $this->data['group']= $this->special_data['ses_group'];// call function and pass parameter
      $this->load->view('view_x', $this->data);

}

观看次数

<?php echo $group; ?>

这是你需要的吗

控制器

    class MY_Controller extends CI_Controller {

    protected $special_data = array();

function special_data($val)
{
if($val=="a"){
$this->special_data = 'users';
}elseif($val=="b"){
$this->special_data = 'companies';
}else{
$this->special_data = 'admin';
}
return $this->special_data;// return value of the function
}
}

class Main_controller extends MY_Controller {


       function __construct()
    {
   parent::__construct();

    }


public function index()
    {

    $data = array(

                    'group' => $this->special_data('a')
            );

            $this->load->view('view_x', $data); 
   }

}

查看

<?php echo $group; ?>