CodeIgniter Forums
data pass through session - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: data pass through session (/thread-33336.html)



data pass through session - El Forum - 08-23-2010

[eluser]shahmy[/eluser]
i have table call student, the main fields of the student table is stu_id,f_name,L_name,....,username,password.

To login to the site, I am using this student table username and password fields. I want to know, how to retrieve stu_id from the table considering username and password.
Please Help me.


data pass through session - El Forum - 08-23-2010

[eluser]zreedeecom[/eluser]
I'm not quite sure what are you doing in your model Function but.. here is a hint:

Code:
function get_user_id(){
  $this->db->where('username', $this->input->post('username'))->where('password', $this->input->post('password'));
  $q = $this->db->get('users');
  if ( $q->num_rows() > 0 ) {
    //do something with: $q->result() or $q->row()
  }
  else {
    //No records founded
  }
} //end get_user_id()

Note: Try to use more generic names for your tables and rows like "USERS", "USER_ID" etc.. it's easier to remember


data pass through session - El Forum - 08-25-2010

[eluser]shahmy[/eluser]
Thanks zreedeecom, for your replay, I was tried what u post above. It's OK. But I want to know how to get user_id in a session, after that i want to insert that id to another table.
Please Help Me.
Thank You.


data pass through session - El Forum - 08-25-2010

[eluser]zreedeecom[/eluser]
Take a look at: User guide > Database Library > Query Results

Code:
function get_user_id(){
  $this->db->where('username', $this->input->post('username'))->where('password', $this->input->post('password'));
  $q = $this->db->get('users');
  if ( $q->num_rows() > 0 ) {
    //do something with: $q->result() or $q->row()
    $row = $q->row();
    //Now you can store that ID in a session
    $this->session->set_userdata('user_id', $row->id);
    //or you can return it to your controller to perform other actions
    return $row->id;
  }
  else {
    //No records founded
  }
} //end get_user_id()



data pass through session - El Forum - 08-26-2010

[eluser]shahmy[/eluser]
zreedeecom, I do what you post, but i cant pass id form model to my controller, I copy my code with this post, please help me,

"model"
function validate()
{
$this->load->database();
$this->db->where('username', $this->input->post('username'));
$this->db->where('password', md5($this->input->post('password')));
$query = $this->db->get('teacher');


if($query->num_rows > 0)
{

$row = $query->row();

return $row->tech_id;
}

//return true;


}

"controller"
function validate_users($tech_id)
{

$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');

if ($this->form_validation->run() == FALSE)
{
$this->index();
}

else
{


$this->load->model('teachers_model');
$query = $this->teachers_model->validate();

if($query) // if the user's credentials validated...
{
$data = array(
'tech_id'=>$tech_id,
'username' => $this->input->post('username'),
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('main/teachers/memberarea');
}

}
}
Thank You.


data pass through session - El Forum - 08-26-2010

[eluser]zreedeecom[/eluser]
Ok.. I've cleaned a bit your code, please read it carefully and change the things you need to make it work with the rest of your code.
I've also uploaded the file with the code.
Code:
<?php


#MODEL

  #Add a constructor to load the library
  function __construct() {
    parent::Model;
    $this->load->database(); #I assume that you have your config file filled ok
  } //end __construct

  function validate() {
    $u = $this->input->post('username');
    $p = md5($this->input->post('password');

    $this->db->where('username', $u)->where('password', $p); #Chaining method is cleaner
    $q = $this->db->get('teacher');

    if($q->num_rows > 0) {
      $row = $query->row();
      //Better than return one property ($row->tech_id) we gonna return the whole object
      return $row;
    }
    else {
      return false; //If no records found return false
    }
  }//end validate



#CONTROLLER

  function validate_users() {

    $this->form_validation->set_rules('username', 'Username', 'required|trim'); #why not add a min_legth?
    $this->form_validation->set_rules('password', 'Password', 'required|trim'); #why not add a min_legth?

    if( $this->form_validation->run() == false ) {
      $this->index();
    }
    else {
      $this->load->model('teachers_model');
      $result = $this->teachers_model->validate();

      if( $result == true ) {
        // if the user’s credentials validated…
        $data = array(
                       'tech_id'        => $result->tech_id,
                       'username'     => $result->username,
                       'is_logged_in' => true
                      );
        $this->session->set_userdata($data);

        redirect('main/teachers/memberarea'); //Is this link ok??
      }
      else {
        //If the model has returned False show an error
        //Maybe you can do something like...
        $this->session->set_flashdata('error', 'Error: No users found');
        $this->index();
      }

    }

  }//end validate_users



#VIEW

  #Put this where you have the form, is not the best way but it should do the work.
  <?= ($this->session->flashdata('error')) ? $this->session->flashdata('error') : ''; ?>



data pass through session - El Forum - 08-27-2010

[eluser]shahmy[/eluser]
Thanks zreedeecom, It is now working In good way.
Thank You.


data pass through session - El Forum - 08-28-2010

[eluser]zreedeecom[/eluser]
[quote author="shahmy" date="1282990790"]Thanks zreedeecom, It is now working In good way.
Thank You.[/quote]

You are welcome :lol: