Welcome Guest, Not a member yet? Register   Sign In
Setting session data in the controller
#1

[eluser]bcarter[/eluser]
Hi there, hope someone can help!

I am trying to set a session variable and I cannot for the life of me how to set it in the controller. I can set it no problem in the view but by this time it is too late!!!

Here's my code..

Model
Code:
class Update_model extends Model {
    
    function Update_model() {
        
        parent::Model();
    }
    
    function get_data()
    {
        $username = $this->session->userdata('username');
        $password = $this->session->userdata('password');
        
        $this->db->where('email', $username);
        //$this->db->where('password', $password);
        $query = $this->db->get('staff', 1);
        
        if($query->num_rows == 1)
        {
            $results = $query->row();
            return     $results;
        }        
    }
    
    function get_cpd_activity()
    {
        $staffID = $this->session->userdata('staffID');
        
        $this->db->where('course_staffID', $staffID);
        $query = $this->db->get('cpd_activity');
        
        $results = $query->result();
        return     $results;
    }
}

View
Code:
<?php
    
$staffID = $details->staffID;
$this->session->set_userdata('staffID', $staffID);

?>

<h1>CPD Overview</h1>

<h3 class="overview-heading">Personal Details</h3>

<p>Name: &lt;?php echo "$details->fname $details->sname" ?&gt;</p>
<p>Department: &lt;?php echo $details->department ?&gt;</p>
<p>Job Title: &lt;?php echo $details->jobRole ?&gt;</p>

<h3 class="overview-heading">Occupatinal Competence</h3>
<p>&lt;?php echo $details->competence ?&gt;</p>

<h3 class="overview-heading">CPD Activity</h3>

<h4>Current CPD Activity</h4>

<table class="wide-table margin-50btm">
    <tbody>
        <tr>
            <th width="50%">Course Title</th>
            <th width="30%">Course Type</th>
            <th width="20%">End Date</th>
        </tr>
        &lt;?php foreach ($cpd as $row) {?&gt;
        <tr>
            <td>&lt;?php echo $row->course_title ?&gt;</td>
            <td>&lt;?php echo ucfirst($row->course_type) ?&gt;</td>
            <td>&lt;?php echo $row->course_expected_end_date ?&gt;</td>
        </tr>
        &lt;?php } ?&gt;
    <tbody>
</table>

<h4>Recent Completed CPD Activity</h4>

<table class="wide-table margin-50btm">
    <tbody>
        <tr>
            <th width="50%">Course Title</th>
            <th width="30%">Course Type</th>
            <th width="20%">End Date</th>
        </tr>
        &lt;?php foreach ($cpd as $row) {?&gt;
        <tr>
            <td>&lt;?php echo $row->course_title ?&gt;</td>
            <td>&lt;?php echo ucfirst($row->course_type) ?&gt;</td>
            <td>&lt;?php echo $row->course_expected_end_date ?&gt;</td>
        </tr>
        &lt;?php } ?&gt;
    <tbody>
</table>

<h3 class="overview-heading">2010 CPD Hours</h3>

&lt;?php $this->load->view('includes/footer') ?&gt;

Controller
Code:
&lt;?php

class Cpd_zone extends Controller {

    function Cpd_zone()
    {
        parent::Controller();
        $this->is_logged_in();    
    }
    
    function is_logged_in()
    {
        $is_logged_in = $this->session->userdata('is_logged_in');
        if(!isset($is_logged_in) || $is_logged_in != true)
        {
            redirect('login');
        }        
    }
    
    function index()
    {
        $data['page_title'] = 'Continuous Professional Development';
        
        $this->load->model('Update_model');
        $data['details'] = $this->Update_model->get_data();
        $data['cpd'] = $this->Update_model->get_cpd_activity();
        
        $this->load->view('includes/head', $data);
        $this->load->view('home_view');
        $this->output->enable_profiler(TRUE);
    }

Can someone tell me how I can set it in the controller?

Cheers
#2

[eluser]WanWizard[/eluser]
Nothing wrong with this code.

Are you using database sessions? If not, I see that you do a redirect after setting session data. This interrupts the output to the browser, which caused the cookie not to be send, so you will loose your session data.
#3

[eluser]bcarter[/eluser]
Sorry I forgot something! When a user logs in it checks the username and password. If there's a match it returns 'true'. The username and password are then set as session variables (that's because I 'borrowed' the script. What I really want to do is set the staff members ID as the session variable so that I can use it later on. The way I do it here is add the staff members ID to the session data once they are logged on (well actually it only happens if they refresh that particular page at the moment, but the principle is there. What I want to do is just assign the ID to the session variable as soon as they log in.

Any ideas?

Cheers
#4

[eluser]WanWizard[/eluser]
You didn't answer my question.




Theme © iAndrew 2016 - Forum software by © MyBB