dongzhui9936 2016-07-14 09:13
浏览 52
已采纳

Codeigniter:无法从一个控制器获取会话变量值到另一个控制器

I have included session in autoload, and its working on all other places. I'm having problem in retrieving session variable it return null result. While in the controller where i have set the session there it's working fine.
Here is the code where i'm setting it in else condition:

class Controller_catagory extends CI_Controller
{

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

        $this->load->model('generic_model');
        $this->load->model('backend/model_post');
        $this->load->model('backend/model_permissions');
    }

    public function index($param1='',$param2='') 
     {
        $id=$this->generic_model->getAllRecords('dramas',array(
            'drama_slug' => $param2 ),'drama_id','DESC');
        // print_r($id);
        if (!empty($id)) 
        {
            foreach ($id as $key) 
            {
                $id = $key['drama_id'];
            }
        }
        $data;

        if (!empty($param1) && empty($param2)) 
        {
            $data["page"]    = 'frontend/includes/view_alldramas';


            $id = $this->generic_model->getAllRecords('channel',array('channel_slug' => $param1 ),'channel_id','DESC');

            if (!empty($id)) 
            {
                foreach ($id as $key) 
                {
                    $id = $key['channel_id'];
                    $ch_slug= $data['ch_slug'] = $key['channel_slug'];
                    $this->session->set_userdata('ch_slug',$ch_slug);
                }
            }

            $this->session->set_userdata('channel_capture',$id);

            $data['dramas_pagination'] = $this->model_post->get_specific_channel_pagination(0,12,$id);
            $data["get_dramas"]=$this->model_post->get_all_dramas();

            $data['channels'] = $this->generic_model->getAllRecords('dramas', array('channel_fk' => $id ),'drama_id','DESC');
        }
        else
        {
            // The id is printing right result
            echo $id;

            // Here i'm setting session, if i retrieve here its working
            $this->session->set_userdata('drama_episode',$id);

            $data['episodes_pagination'] = $this->model_post->get_specific_post_pagination(0,12,$id);
            $data["get_episodes"]=$this->model_post->get_all_dramas();
            $data['dramas'] = $this->generic_model->getAllRecords('post',$arr = array(
            'dramas_fk' => $id ),'id','DESC');
            $data["page"]    = 'frontend/includes/view_allposts';

        }

        $data['title']   = 'GLOBAL VIDEOS';
        $data['heading'] = 'Dramas List';
        $data["top"]     = 'frontend/includes/top_home';


        $this->load->view('frontend/index',$data);
    }
  }

}

Now here is another class where i'm trying to get the value of set session but its not retrieving the data and i'm getting empty record.

Note: i am doing the same thing with 'channel_capture' and i'm successfully getting its value

class Home extends CI_Controller {

 public function __construct() 
{
    $data = array();
    parent::__construct();


    $this->load->model('backend/model_post');
    $this->load->model('generic_model');
}
public function ajax_posts()
{
    $start= $_GET['start'];

    // it gives empty result here don't know why
    $id = $this->session->userdata('drama_episode');

    //prints nothing
     echo "This key: ".$id;

    $post_pagination=$this->model_post->get_specific_post_pagination($start, 12, $id);
    var_dump($post_pagination);
    $str='';
    $base_url=base_url();

    if (empty($post_pagination))
    { 
        return false;
    }

    foreach($post_pagination as $post)
    {

        $str.=          '<div class="col-lg-3 col-sm-6 col-md-4 epi_height" >';
        $str.=              '<a href='.$base_url.$post['slug'].'>';
        $str.=              '<img class="img-responsive" src='.$base_url.$post['thumbnail'].' alt="recent dramas" />';
        $str.=              $post['title'];
        $str.=              '</a>';
        $str.=         '</div>';    

    }
        echo $str; 
   }
}
  • 写回答

3条回答 默认 最新

  • dourao1877 2016-07-14 10:07
    关注

    Try to set session before if else condition like:

    $this->session->set_userdata('drama_episode',$id);
    

    your if else goes here and then retrieve it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?