Welcome Guest, Not a member yet? Register   Sign In
data pass through session
#6

[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') : ''; ?>


Messages In This Thread
data pass through session - by El Forum - 08-23-2010, 04:52 AM
data pass through session - by El Forum - 08-23-2010, 09:18 AM
data pass through session - by El Forum - 08-25-2010, 12:47 AM
data pass through session - by El Forum - 08-25-2010, 02:58 AM
data pass through session - by El Forum - 08-26-2010, 02:50 AM
data pass through session - by El Forum - 08-26-2010, 04:20 AM
data pass through session - by El Forum - 08-27-2010, 11:19 PM
data pass through session - by El Forum - 08-28-2010, 04:50 AM



Theme © iAndrew 2016 - Forum software by © MyBB