0

I need get id from database and make it as a session variable

here is my code,

model

function validate(){
    $this->db->where('username', $this->input->post('username'));
    $this->db->where('password', md5($this->input->post('password')));
    $query = $this->db->get('userdetails');

    if($query->num_rows == 1){
        $username = $this->input->post('username');
        $this->db->select('id');
        $this->db->from('userdetails');
        $this->db->where('username',$username);
        $userid = $this->db->get()->row()->id;
        echo "userid".$userid;
        $this->session->set_userdata('userid', $userid);
        return true;    
    }   
}

view I can't echo it in view

 <?php echo form_open_multipart('site/upload');?>
    <?php
        foreach($company_result as $res){?>
    <label>Code : </label> <?php echo form_input('code',$res->code);?><br/><br/>
    <label>Name : </label> <?php echo form_input('name',$res->name);?><br/><br/>
    <label>Logo : </label><input type="file" name="userfile"/><br/><br/>
    <div><?php echo $res->url; ?></div><br/></br/>
    <label>URL : </label> <?php echo form_input('url',$res->url);?><br/><br/>

    <input type="submit" name="submit" value="Save"/>
    <?php echo $session_id = $this->session->userdata('userid'); ?>

<?php
        }
?>
    </form>

can I make session variables in model?? and how can I echo session variable in view

edited view

<?php $user_id = $this->session->userdata('userid');
          echo $user_id; ?>
    <?php //echo $session_id = $this->session->userdata('userid'); ?>
    <?php //$session_data = $this->session->all_userdata();
    //echo "<pre>"; print_r($session_data); ?>

    <?php var_dump($this->session->userdata('userid')); ?>

    // or access array indexes like so.
    //$post_array = $this->session->userdata('newdata');
    //echo $post_array['index'];

1 Answer 1

1

You can create session variables anywhere you can.

But try avoiding to create in models, instead do it in controllers since models must be used only for database activities.


Using sessions in Codeigniter:

1) Before using session, make sure you have initialized the session class.

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

Note: You can also autoload it in config/autoload.php.


2) Creating a session variable.

$this->session->set_userdata('some_name', 'some_value');


3) Retrieving session data.

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


4) Retrieving all session data.

$this->session->all_userdata();

Note: Above will return an array having all session data.


Documentation:

http://ellislab.com/codeigniter/user-guide/libraries/sessions.html


In case of your code:

Try to debug by printing all the session variables using the below,

$session_data = $this->session->all_userdata();
echo "<pre>"; print_r($session_data);

// will print an array with all session data
// check here whether you are getting you session variable (userid) here


Edit:

$user_id = $this->session->userdata('userid');
echo $user_id;
Sign up to request clarification or add additional context in comments.

5 Comments

@Parag Tyagi how can I echo session variable in view
can't echo session variables in method that you have mentioned
@user3840485: Check edits. Its very simple. And what you have done in your view seems correct to me.
Can you see userid in the array after doing $session_data = $this->session->all_userdata(); echo "<pre>"; print_r($session_data);
@Parag Tyagi no I can't see,but I have create new file,Then I could view user id, but when I change the id I can't see new one, but can see previous one,I have clear cache data also but I couldn't handle it

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.