1

I'm using codeigniter i want to show some data taken form a database by querying as following.

$this->db->where('sex !=', $iam);
  $this->db->where('sex', $searching_for);
  $this->db->where('Age >=' , $age_from);
  $this->db->where('Age <=' , $age_to);
 if($Province != 1){
     $this->db->where('Province' , $Province);
 }

        $this->db->limit($limit, $start);
        $query = $this->db->get("members");
        return $query->result_array();

The $iam,$searching_for, $age, $age_to is provided by user and I'm passing them from conttroller file using session array.

    $search_info=array(
    'iam'           =>  $this->input->post('iam'),
    'searching_for' =>  $this->input->post('searching_for'),
    'age_from'      =>  $this->input->post('age_from'),
    'age_to'        =>  $this->input->post('age_to'),
    'country'       =>  $this->input->post('country'),
    'Province'      =>  $this->input->post('Province')
);

$this->session->set_userdata(array("search_info" => $search_info));

and my pagination function is also in controller file and it is like this

public function pagination(){
        $this->load->library("pagination");
        $config = array();
    $config["base_url"] = base_url() . "controller_search/index";

    $this->load->model('models_search');
    $config["total_rows"] = $this->models_search->search();

    $config["per_page"] = 1;
    $config["uri_segment"] = 3;
    $this->pagination->initialize($config);
    //$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;3
    //echo $this->uri->segment(3);
    //echo ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;3;
    $page = $this->uri->segment(3);
    $data["search_result"] = $this->models_search->fetch_categories($config["per_page"], $page);
    $data["links"] = $this->pagination->create_links();
    $data['error'] = '';
    $this->load->view('home_header.php');
    $this->load->view('search/search_result',$data);
}

But the thing is when i click the page numbers it didn't show any thing, i tried commenting all the where clues in the query and then it works.
So i think the error is in session array and i tried to var_dump the session_all so then it shows array(0){}

Can anyone help me in this case ?

12
  • 1
    what does var_dump( $this->session->all_userdata()) output? Is it blank? Jun 3, 2013 at 4:47
  • 1
    The session is getting set for the first time but when navigated to some other page or same page, the session is getting destroyed. right? Jun 3, 2013 at 5:03
  • 1
    In your controller have you included $this->load->library('session');, unless you are autoloading sessions. Jun 3, 2013 at 5:05
  • 1
    Below $this->session->set_userdata(array("search_info" => $search_info)); add $this->session->set_userdata('dummy', 'somevalue'); and check whether dummy session exists throughout. Jun 3, 2013 at 5:12
  • 1
    So its evident that there's no problem setting sessions in your controller. You are setting the session values obtained through POST. Therefore first time when it contain details, session variables are set with those details but second time with no details in post variables, the session variables are overwritten with empty values. Jun 3, 2013 at 5:23

5 Answers 5

1

Use this code:

$this->session->set_userdata("search_info" => $search_info);

code to set session

1

Set your session data by this code:

$this->session->set_userdata("sess_data", "mydata");

Retrive this session data by this code:

$this->session->userdata("sess_data");
1
  • "Your", not "ur". Chat speak is specifically disallowed here.
    – user229044
    Jun 3, 2013 at 12:03
1

I think you are accessing session array wrong way.

Instead of

$iam 

use

$this->session->userdata('iam');

Or

$this->session->userdata('$iam');

Whichever works in your case....

0

I just took all the posted values to variables in index function and then i put them in to session array as

 $search_info=array(
            'iam'           =>  $iam,
            'searching_for' =>  $searching_for,
            'age_from'      =>  $age_from,
            'age_to'        =>  $age_to,
            'country'       =>  $country,
            'Province'      =>  $Province
        );

       $this->session->set_userdata(array("search_info" => $search_info)); 

then i redirect it to pagination function. then it works neatly. before it was overwrite the post items and the session array when i moving towards pages. now because of the redirect it was stoped. as i think. any way it is working now very well than you every one for helping me. specially Mr.John Blake thank you very much sir.

0

check if you have load the session library.If not either load in your controller as

$this->load->library('session')

or you may auto load session in library of config/autoload.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.