Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter and Set Session in Model

I want to unset and set session in model function depend on passed id.
I have language field for every record and should change session considering that.
in sample word i have a link like this : http://www.mysite.com/report/2020
now this article by this id maybe saved by english language but current session is french.so I should change the session. here is my code but it dos not work.

  //function for checking language by id
  public function lang($id)
     {
         $data = $this->db
                        ->select('language')
                        ->from('content')
                        ->where('id',$id)
                        ->get();

        if ($data->num_rows > 0) {

            return $data;
  }else{
            return false;
        }
    }
  //function for reading data from db depend on id and language
  public function report($id)
    {

    $cat=2;
    $code=$id;

    $langu= $this->lang($id)->row()->language;
    if($langu==3){
    $this->session->unset_userdata('lang');
    $this->session->set_userdata('lang','dr');
    }
    if($langu==2){
    $this->session->unset_userdata('lang');
    $this->session->set_userdata('lang','pa');
    }
    if($langu==1){
    $this->session->unset_userdata('lang');
    $this->session->set_userdata('lang','en');
    }
    $language= $this->session->userdata('lang');
    $lang=$language;


    $data = $this->db
                        ->select('*')
                        ->from('content')
                        ->where('category',$cat)
                        ->where('language',$lang)
                        ->where('id',$code)
                        ->get();

        if ($data->num_rows > 0) {

            return $data;
        }else{
            return false;
        }

    }
like image 227
Bahrami-Reza Avatar asked Jan 19 '14 06:01

Bahrami-Reza


People also ask

How do you set a session value in CI?

$_SESSION['item'] = 'value'; $this->session->mark_as_flash('item'); Or alternatively, using the set_flashdata() method: $this->session->set_flashdata('item', 'value'); You can also pass an array to set_flashdata() , in the same manner as set_userdata() .

Which method is used to set session in CodeIgniter?

Sessions data are available globally through the site but to use those data we first need to initialize the session. We can do that by executing the following line in constructor. $this->load->library('session'); After loading the session library, you can simply use the session object as shown below.

What is session in CodeIgniter?

The Session class permits you to maintain a user's “state” and track their activity while they browse your site. CodeIgniter comes with a few session storage drivers, that you can see in the last section of the table of contents: Using the Session Class.


1 Answers

the best way to session is:

you have to check the query in model, and call the model in controller, if its true set session data within controller....

Setting Session within the model is not a good idea for an MVC architecture.

like image 138
Adarsh M Pallickal Avatar answered Sep 27 '22 20:09

Adarsh M Pallickal