在 2 个控制器函数之间传递数据

Passing data between 2 controller functions

简单来说,我希望 status1 转到 ajaxleadsreport 控制器

稍微复杂一点的解释:

我有 2 个视图 classes,分别称为 indexreport 和 ajaxleadsreport。 ajaxleadsreport 获取所有数据并显示它。现在我有一个传递给 indexreport 的 GET 变量,我希望能够将它传递给 ajaxleadsreport 以根据传递的 GET 变量过滤当前数据。

控制器Class:

public function listsreport($slug='')
    {
        $status1 = $this->input->get('status');
        print_r($status1);
        $main['content']=$this->load->view('crm/leads/indexreport',$content,true);
     }
    public function ajaxleadsreport($p='')
    {       
            $output = array( 'html'=>$this->load->view('crm/leads/ajaxleadsreport',$content, true)); 
        echo json_encode($output); 
    }

indexreport 视图 Class:

<?php
$i=$return=$uriseg;
$post=$this->input->post(); $sess=$this->session->userdata();
$post = $post?$post:$sess;
?>
<div>
...
</div>
$(document).ready(function (){
  getleads();
 });
function getleads(p){ 

    $.ajax({
    type: "post",dataType:"json",async:false,cache:true,
    url: "<?php echo site_url('leads/ajaxleadsreport'); ?>"+( parseInt(p)>0?'/'+p:''),
    data: $('#objlistform').serialize(),
    success: function(e){$('#leadswrap').hide().html(e.html).fadeIn('slow');} 
  }); return false;
}

ajaxleadsreport 视图 class:

<?php  
$sess=$this->session->userdata(); 
$status1 = $this->input->get('status');
// This is where I'm trying to put my GET value of status for filtering but it gives NULL.
$post = array('fstatus'=> $status,'fpriority'=> $sessn['fpriority']);
$postd = json_encode(array_filter($post));
?>
...
<script>
$(document).ready(function() {
  function sendreq(){
    setpostdatas();cleartable();getleads();
  }
   var slug = '<?php echo $slug?>';
   var postd = '<?php echo $postd; ?>';
    $('#item-list').DataTable({
        "processing": true,
        "stateSave": true,
        "serverSide": true,
        "ordering": false,
        "ajax": {
            url: "<?php echo site_url(); ?>leads/loadLeads",
            data: {slug: slug, postdata: postd},
            type : 'POST',
            "dataSrc": function ( d ) {
                d.myKey = "myValue";
                if(d.recordsTotal == 0 || d.data == null){
                   $("#item-list_info").text("No records found");
                }
                return d.data;
            }
        },
        'columns': [
            {"data": "id", "id": "id"},
            {"data": "lead_status", "lead_status": "lead_status"},
            {"data": "priority", "priority": "priority"},  
        ]
    });

正如您在上面的代码中所看到的,我已经在 ajaxleadsreport 视图 class 中尝试了 $status1 = $this->input->get('status'); 但是输出为 NULL,因为 GET 值是在我的 indexreport 视图中传递的 class。当我在 indexreport 控制器中执行 print_r($status1) 时,它给出了正确的输出,但在 ajaxleadsreport 控制器中为 NULL。

所以基本上现在我需要一种方法将此 GET 值传递给 ajaxleadsreport 控制器。

在控制器中class声明变量:

protected $status1;
public function listsReport() { 
  $this->status1 =  $this->input->get('status');
  // [...]
}

然后您可以从之后调用的任何函数访问 $this->status1

你可以使用闪存数据here:

将您的状态设置为 flashdata,然后像这样在 ajaxleadsreport 中获取它:(flashdata 仅在下一次请求时存在一次)

$this->session->set_flashdata('status', $status1);

在 ajaxleadsreport 中:

$this->session->flashdata('status');