user5876173
user5876173

Reputation:

store session id in another table in codeigniter

i have one login page for vendor..after vendor logged in they redirect to vendor_dashboard..there i am getting vendor id with dashboard link..and i am getting that particular vendor name in my dashboard page..

In vendor dashboard..vendor has one tab called candidates..when they click on that tab they redirect to add_candidate page..so when vendor submit the form i want to store that vendor_id in candidates table..

In candidates_table there is one column called vendor_id..after submit i want to store that vendor_id for that particular candidate..

I am trying to do this..from past 6 hours..still it's not storing.. The problem is i didn't get anyidea on how to do this..

Login page:

if($this->input->post())
{
$user = $this->LoginModel->login($this->input->post());
if(count($user)>0)
{
$data = array(
'user_id' => $user['user_id'],
'first_name' => $user['first_name'],
'email' => $user['email'],
'password' => $user['password']
);                   
$this->session->set_userdata($data);
if($user['user_type_id'] == '1')
{
// redirect(base_url('index.php/Login/vendor_dashboard/'.$data['first_name']));
redirect(base_url('index.php/Login/vendor_dashboard/'.$this->session->user_id));                
} 
elseif($user['user_type_id'] == '2')
{
// (base_url('index.php/Login/user_dashboard/'.$this->session->user_id));
}
else
{
redirect(base_url('index.php/Login/dashboard'));
}
}

Dashboard:

<?= ($this->session->first_name)? $this->session->first_name : "" ?>

Can anyone help me..

Thanks in advance..

Upvotes: 0

Views: 1100

Answers (1)

Mudassar Khani
Mudassar Khani

Reputation: 1479

Your questions is some how related to this post. Anyway here is what you can do. Start with Authentication using User Controller or name it login or whatever you are comfortable with

class Users extends CI_Controller
{

public function __construct()
{
    parent::__construct();
    $this->load->model('user_model');
}
public function index()
{
    $data['title']='Vendors Application';
    if($_POST)
    {
        $user=$this->user_model->checkUser($_POST);
        if(count($user)>0)
        {
            $this->session->set_userdata($user);
            redirect(base_url().'dasboard/vendor');
        }
    }
    else
    {
        $this->load>view('login',$data);
    }

 }
}

The function to check your user in database in User Model is as follows

public function checkUser($data)
{
    $st=$this->db->select('*')->from('users')
        ->WHERE('email',$data['email'])
        ->WHERE('password',md5(sha1($data['password'])))
        ->get()->result_array();
    if(count($st)>0)
    {
        return $st[0];
    }
    else
    {
        return false;
    }
}

Once you are logged in and redirected to dashboard, add the candidate using Dashboard Controller or whatever you are comfortable to call it.

class Dashboard extends CI_Controller
{

public function __construct()
{
    parent::__construct();
    $this->load->model('user_model');
}
public function vendor()
{
    $data['title']='Vendor Dashboard';

    if($_POST)
    {
        //validation
        $this->user_model->addCandidate($_POST);
        $data['success']='Candidate Added Successfully';
        $this->load->view('vendor_dashboard',$data);
    }
    else
    {
        $this->load->view('vendor_dashboard',$data);
    }

 }
}

The function in user model to add the candidate is as follows.

public function addCandidate($data)
{
    $candidate=array(
        'user_id'=>$this->session->userdata['id'],
        'first_name'=>$data['first_name'],
        'last_name'=>$data['last_name'],
        'address'=>$data['address'],
        'contact'=>$data['contact']
    );
    $this->db->insert('candidates',$candidate);
    return $this->db->insert_id();
}

Remember you already have the vendor id in your session when you logged him in and stored his record in the session so there is no need to send the controller url any id to load the dashboard.

I hope you know about CI Form Validation if not please find it here

Upvotes: 1

Related Questions