简体   繁体   中英

make session variable from database value in codeigniter

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'];

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM