Welcome Guest, Not a member yet? Register   Sign In
Fetching data using unique ID from the database.
#1

Hello I'm new to both Codeigniter and PHP I have created a system for employees. I want that when an employee login to the system he/she will only see his/her own data related to his/her unique employee ID in the database. To be specific every employee has it's own employee id which populated with data like leave balance, personal datasheets and a lot more. Now what I want to do is to pull those data's and display it when they login to their own account. My login is working but when I try pull out the data it seems it kinda hard. I hope you could help me.

Here's my code for your reference.

Model: Users_model.php (This is for login)

Code:
<?php
    class Users_model extends CI_Model {
        function __construct(){
            parent::__construct();
            $this->load->database();
        }

        public function login($email, $password){
            $query = $this->db->get_where('ats_users', array('email'=>$email, 'password'=>$password));
            return $query->row_array();
        }

    }
?>


View: login.php

Code:
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Employee Login</title>
    <link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>bootstrap/css/bootstrap.min.css">
</head>
<body>
<div class="container">
    <h1 class="page-header text-center">iHRMIS Employee Panel</h1>
    <div class="row">
        <div class="col-sm-4 col-sm-offset-4">
            <div class="login-panel panel panel-primary">
                <div class="panel-heading">
                    <h3 class="panel-title"><span class="glyphicon glyphicon-lock"></span> Login
                    </h3>
                </div>
                <div class="panel-body">
                    <form method="POST" action="<?php echo base_url(); ?>index.php/user/login">
                        <fieldset>
                            <div class="form-group">
                                <input class="form-control" placeholder="Email" type="email" name="email" required>
                            </div>
                            <div class="form-group">
                                <input class="form-control" placeholder="Password" type="password" name="password" required>
                            </div>
                            <button type="submit" class="btn btn-lg btn-primary btn-block"><span class="glyphicon glyphicon-log-in"></span> Login</button>
                        </fieldset>
                    </form>
                </div>
            </div>
            <?php
                if($this->session->flashdata('error')){
                    ?>
                    <div class="alert alert-danger text-center" style="margin-top:20px;">
                        <?php echo $this->session->flashdata('error'); ?>
                    </div>
                    <?php
                }
            ?>
        </div>
    </div>
</div>
</body>
</html>

Views: home.php
Code:
<div class="page-wrapper">
           <!-- ============================================================== -->
           <!-- Bread crumb and right sidebar toggle -->
           <!-- ============================================================== -->
            <div class="page-breadcrumb">
               <div class="row">
                   <div class="col-12 d-flex no-block align-items-center">
                       <h4 class="page-title">Dashboard</h4>
                       <div class="ml-auto text-right">
                           <nav aria-label="breadcrumb">
                               <ol class="breadcrumb">
                                   <li class="breadcrumb-item"><a href="#">Home</a></li>
                                   <li class="breadcrumb-item active" aria-current="page">Library</li>
                               </ol>
                           </nav>
                       </div>
                   </div>
               </div>
           </div>
           <!-- ============================================================== -->
           <!-- End Bread crumb and right sidebar toggle -->
           <!-- ============================================================== -->
               <!-- ============================================================== -->
               <!-- Sales chart -->
               <!-- ============================================================== -->
               <div class="row">
                   <div class="col-12">
                       <div class="card">
                           <div class="card-body">
                               <div class="row">
                                   <!-- column -->
                                   <div class="col-lg-9">
                                   <?php
                                    $user = $this->session->userdata('user');
                                            extract($user);
                                    ?>
                                    <h2>Personal Information </h2>
                                    <h4>User Info:</h4>
                                    <p>Employee ID: <?php echo $employee_id; ?></p>
                                    <p>Office ID: <?php echo $office_id; ?></p>
                                    <p>Fullname: <?php echo $fname.$mname."".$lname; ?></p>
                                    <p>Email: <?php echo $email; ?></p>
                                    <p>Password: <?php echo $password; ?></p>
                                    <a href="<?php echo base_url(); ?>index.php/user/logout" class="btn btn-danger">Logout</a>
                                   </div>

                                   <!-- column -->
                               </div>
                           </div>
                       </div>
                   </div>
               </div>
               <!-- ============================================================== -->
               <!-- Sales chart -->
               <!-- ============================================================== -->
               <!-- ============================================================== -->
               <!-- Recent comment and chats -->
               <!-- ============================================================== -->
</div>

Controllers: Users.php
Code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class User extends CI_Controller {

    function __construct(){
        parent::__construct();
        $this->load->helper('url');
        $this->load->model('users_model');
    }

    public function index(){
        //load session library
        $this->load->library('session');

        //restrict users to go back to login if session has been set
        if($this->session->userdata('user')){
            redirect('home');
        }
        else{
            $this->load->view('login_page');
        }
    }

    public function login(){
        //load session library
        $this->load->library('session');

        $email = $_POST['email'];
        $password = $_POST['password'];

        $data = $this->users_model->login($email, $password);

        if($data){
            $this->session->set_userdata('user', $data);
            redirect('home');
        }
        else{
            header('location:'.base_url().$this->index());
            $this->session->set_flashdata('error','Invalid login. Email or Password is incorrect.');
        }
    }

    public function home(){
        //load session library
        $this->load->library('session');

        //restrict users to go to home if not logged in
        if($this->session->userdata('user')){
            $data['main_content'] = 'home';
            $this->load->view('includes/template', $data);
            //$this->load->view('templates/header');
            //$this->load->view('home');
           //$this->load->view('templates/footer');    
        }
        else{
            redirect('/');
        }
        
    }

    public function logout(){
        //load session library
        $this->load->library('session');
        $this->session->unset_userdata('user');
        redirect('/');
    }

}

Now this is the code I used to pull out the employee data

Model: user.php

PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
User extends CI_Model{

    var 
$fields = array();
    
    
// --------------------------------------------------------------------
    
    /**
     * Constructor
     *
     * @return User
     */
    
function __construct()
    {
        
parent::__construct();
        
        
$this->load->helper('security');
        
$this->load->database();
    }
    
    
// --------------------------------------------------------------------
    
    /**
     * Add new user
     *
     * @param unknown_type $info
     * @return unknown
     */
    
function add_user($data)
    {
        
$this->db->insert('users'$data);
    }
    
    
// --------------------------------------------------------------------
    
    /**
     * Delete user
     *
     * @param varchar $email
     */
    
function delete_user($email)
    {
        
$this->db->delete('users', array('email' => $email)); 
    }
    
    
// --------------------------------------------------------------------
    
    
function get_current_password($email)
    {
        
$current_password '';
        
        
$this->db->select($this->fields);
        
$this->db->where('email'$email);
        
$this->db->limit(1);
        
$q $this->db->get('users');
        
        if (
$q->num_rows() > 0)
        {
            foreach (
$q->result_array() as $row)
            {
                
$current_password $row['password'];
            }
        }
        
        return 
$current_password;
        
        
$q->free_result();
    }
    
    
// --------------------------------------------------------------------
    
    /**
     * Get the maximum user
     *
     * @return varchar
     */
    
function get_max_user()
    {
        
$max_user '';
        
        
$this->db->select_max('email''max_user');
        
$q $this->db->get('users');
        
        if (
$q->num_rows() > 0)
        {
            foreach (
$q->result_array() as $row)
            {
                
$max_user $row['max_user'] + 1;
            }
        }
        
        return 
$max_user;
        
        
$q->free_result();
    }
    
    
// --------------------------------------------------------------------
    
    /**
     * THE FUNCTIONS BELOW ARE FOR TIMEKEEPER
     * Get the data of a user
     *
     * @param varchar $email
     * @return array
     */
    
function get_user_data($email)
    {
        
$data = array();
        
        
$this->db->where('email'$email);
        
$this->db->limit(1);
        
$q $this->db->get('users');
        
        if (
$q->num_rows() > 0)
        {
            foreach (
$q->result_array() as $row)
            {
                
$data $row;
            }
        }
        
        return 
$data;
        
        
$q->free_result();
    }
    
    
// --------------------------------------------------------------------
    
    /**
     * Get users
     *
     * @param array $fields
     * @return array
     */
    
function get_users($fields = array())
    {
        
$data = array();
        
        
$this->db->select($this->fields);
        
$this->db->from('users');
        
$this->db->join('office''office.office_id = user.office_id');
        
$this->db->join('user_type''user_type.id = user.user_type');
        
$this->db->order_by('lname');
        
$q $this->db->get();
        
        if (
$q->num_rows() > 0)
        {
            foreach (
$q->result_array() as $row)
            {
                
$data[] = $row;
            }
        }
        
        return 
$data;
        
        
$q->free_result();
    }
    
    
// --------------------------------------------------------------------
    
    /**
     * Check if email exists
     *
     * @param string $email
     * @return boolean
     */
    
function is_email_exists($email)
    {
        
$this->db->select('email');
        
$this->db->where('email'$email);
        
$q $this->db->get('users');
        
        
        if (
$q->num_rows() > 0)
        {
            return 
TRUE;
        }
        else
        {
            return 
FALSE;
        }
        
        
$q->free_result();
    }
    
    
// --------------------------------------------------------------------
    
    /**
     * Tells whether a user is valid or not
     *
     * @param varchar $email
     * @param varchar $password
     * @return boolean
     */
    
function is_valid_user($email$password)
    {
        
$this->db->where('email'$email);
        
$this->db->where('password'$password);
        
$this->db->where('stat'1);
        
        
$q $this->db->get('users');
        
        
        
        if (
$q->num_rows() > 0)
        {
            return 
TRUE;
        }
        else 
        {
            return 
FALSE;
        }
        
        return 
$data;
        
        
$q->free_result();
    }
    
    
// --------------------------------------------------------------------
    
    /**
     * Update or edit user account
     *
     * @param array $data
     * @param int $user_id
     */
    
function update_user($data$id '')
    {
        
$this->db->where('id'$id);
        
$this->db->update('users'$data); 
    }
    
    
// --------------------------------------------------------------------
    
    
function update_user_pass($pass$email)
    {
        
$data = array('password' => $pass);
        
        
$this->db->where('email'$email);
        
$this->db->update('users'$data); 
    }
    
    
// --------------------------------------------------------------------
    
    /**
     * Validate the email and password
     *
     * @param mixed $email
     * @param mixed $password
     * @return array
     */
    
function validate_user($email$password)
    {
        
$data = array();
        
        
$data['system_message'] = '';
        
        
//Encript password
        
$password do_hash($password'md5');
        
        
$options = array('email' => $email'password' => $password'stat' => 'Active');
        
$q $this->db->get_where('users'$options1);
            
        if (
$q->num_rows() > 0)
        {
            
$row $q->row();
            
            
$fname         $row->fname;
            
$lname         $row->lname;
            
$mname         $row->mname;
            
$user_type     $row->user_type;
                
            
$email $email;
                
            
$session_data = array(
                            
'email'    => $row->email
                            
'lname'     => $row->lname,
                            
'office_id' => $row->office_id,
                            
'user_type' => $row->user_type
                            
);

            
$this->session->set_userdata($session_data);
                
            
$data['system_message'] = 'valid';
                
        }
            
        return 
$data;
    }
    
    
// --------------------------------------------------------------------
    


Models: options.php
PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
Options extends CI_Model {

    
// --------------------------------------------------------------------
    
    /**
     * Constructor
     *
     * @return Options
     */
    
function __construct()
    {
        
parent::__construct();
        
$this->load->database();
    }
    
    
// --------------------------------------------------------------------
    
    /**
     * Options for dropdown list
     * day 1-31
     *
     * @return array
     */
    
function days_options()
    {
        
$day 1
        
        while(
$day != 32)
        {
            
//Add leading zero to month
            
$x sprintf("%02d"$day);
            
            
$options[$x] = $x;
        
            
$day ++; 
        }
        
        return 
$options;
        
    }
    
    
// --------------------------------------------------------------------
    
    
function budget_expenditures_options($e '')
    {
        
$this->load->model('budget_expenditure_m');
            
        
$options  = array();
        
        
$b = new Budget_expenditure_m();
        
        
$expenditures $b->order_by('expenditures')->get();
        
        
$options[0] = '---ALL---';
        
        foreach(
$expenditures as $expenditure)
        {
            
$options[$expenditure->id] = $expenditure->expenditures;
        }
        
        return 
$options;
        
    }
    
    
    
// --------------------------------------------------------------------
    
    /**
     * Options for dropdown list
     * hour 01-24
     *
     * @return array
     */
    
function hour_options$add_blank FALSE)
    {
        
$hour 1
        
        if ( 
$add_blank == TRUE)
        {
            
$options[0] = '';
        }
        
        while(
$hour != 25)
        {
            
//Add leading zero to month
            
$x sprintf("%02d"$hour);
            
            
$options[$x] = $x;
        
            
$hour ++; 
        }
        
        return 
$options;
        
    }
    
    
// --------------------------------------------------------------------
    
    
function group_options($add_select FALSE)
    {
        
$options = array();
        
        
$g = new Group_m();
        
$g->order_by('name');
        
$groups $g->get();
        
        if (
$add_select == TRUE)
        {
            
$options[] = 'SELECT GROUP';    
        }
        
        foreach(
$groups as $group)
        {
            
$options[$group->id] = $group->name;
        }
        
        return 
$options;
    }
    
    
// --------------------------------------------------------------------
    
    /**
     * Options for leave type
     *
     * @param boolean $add_select
     * @return array
     */
    
function leave_type_options($add_select FALSE)
    {
        
        
$leave_types $this->Leave_type->leave_type_list();
        
        
        if (
$add_select == TRUE)
        {
            
$options[] = 'SELECT OFFICE';    
        }
        
        foreach(
$leave_types as $leave_type)
        {
            
$options[$leave_type['id']] = $leave_type['leave_name'];
        }
        
        
// Updated 3.1.2012 since version 2.00.00
        // add Undertime in type of leave
        // for laguna use
        
        
$lgu_code $this->Settings->get_selected_field('lgu_code'); 
        
        
// Laguna Province
        
if ( $lgu_code == 'laguna_province' )
        {
            
$options['undertime'] = '-- Undertime --';
        }
        
// end update
        
        
return $options;
    }
    
    
// --------------------------------------------------------------------
    
    /**
     * Options for month
     *
     * @param boolean $add_select
     * @return array
     */
    
function month_options($add_select FALSE)
    {
        
$month 1;
        
        while(
$month != 13)
        {
            
//Add leading zero to month
            
$x sprintf("%02d"$month);
            
            
$options[$x] = $this->Helps->get_month_name($month);
            
            
$month ++;
        }
        
        return 
$options;
    }
    
    
// --------------------------------------------------------------------
    
    /**
     * Options for office
     *
     * @param boolean $add_select
     * @return array
     */
    
function office_options($add_select FALSE)
    {
        
$options = array();
        
        
$this->Office->fields = array('office_id''office_name');
        
        
$offices $this->Office->get_offices();
        
        
        if (
$add_select == TRUE)
        {
            
$options[] = 'SELECT OFFICE';    
        }
        
        foreach(
$offices as $office)
        {
            
$options[$office['office_id']] = $office['office_name'];
        }
        
        return 
$options;
    }
    
    
// --------------------------------------------------------------------
    
    /**
     * Options for divisions
     *
     * @param boolean $add_select
     * @return array
     */
    
function division_options($office_id 1)
    {
        
$options  = array();
        
        
$d = new Division();
        
        
$divisions $d->where('office_id'$office_id)->order_by('order')->get();
                        
        foreach(
$divisions as $division)
        {
            
$options[$division->id] = $division->name;
        }
        
        return 
$options;
    }
    
    
// --------------------------------------------------------------------
    
    /**
     * Options for salary grade
     *
     * @return array
     */
    
function salary_grade()
    {
        
$x 1;
        while(
$x != 31)
        { 
            
$sg_options[$x] = $x;
            
$x++;
        }
        
        return 
$sg_options;
    }
    
    
// --------------------------------------------------------------------
    
    /**
     * Options for shift
     *
     * @return array
     */
    
function shift()
    {
        
$shifts $this->Shift->shift_list();
    
        foreach(
$shifts as $shift)
        {
            
$shifts_options[$shift['shift_id']] = $shift['name'];
        }
        
//$shifts_options[0] = 'Other...';
        
        
return $shifts_options;
    }
    
    
// --------------------------------------------------------------------
    
    /**
     * Options for step
     *
     * @return array
     */
    
function step()
    {
        
$x 1;
        while(
$x != 9)
        { 
            
$step_options[$x] = $x;
            
$x++;
        }
        
        return 
$step_options;
    }
    
    
// --------------------------------------------------------------------
    
    /**
     * Options for type of employment
     *
     * @param boolean $all
     * @return array
     */
    
function type_employment($all FALSE)
    {
        if (
$all == TRUE)
        {
            
$type_employment['all'] = 'All';
        }    
        
        
$type_employment['1'] = 'Permanent';
        
$type_employment['2'] = 'Casual';
        
$type_employment['3'] = 'Contract of Service';
        
$type_employment['4'] = 'Job Order';
        
$type_employment['5'] = 'Co Terminous';
        
$type_employment['6'] = 'Elected Official';
        
$type_employment['7'] = 'Temporary';
        
$type_employment['8'] = 'Contractual Plantilla';
                            
        return 
$type_employment;
    }
    
    
// --------------------------------------------------------------------
    
function training_type_options($training_type '')
    {
        
$this->load->model('training_type');
            
        
$options  = array();
        
        
$t = new Training_type();
        
        
$types $t->order_by('training_type')->get();
        
        
$options[0] = '---ALL---';
        
        foreach(
$types as $type)
        {
            
$options[$type->id] = $type->training_type;
        }
        
        return 
$options;
        
    }
    
    
// --------------------------------------------------------------------
    
function training_contact_type_options($training_type '')
    {
        
$this->load->model('training_contact_type');
            
        
$options  = array();
        
        
$t = new Training_contact_type();
        
        
$types $t->order_by('contact_type')->get();
        
        
$options[0] = '---ALL---';
        
        foreach(
$types as $type)
        {
            
$options[$type->id] = $type->contact_type;
        }
        
        return 
$options;
        
    }
    
    
// --------------------------------------------------------------------
    
    /**
     * Options for user type
     *
     * @return array
     */
    
function user_type()
    {
        
$user_type = array(
                            
'1' => 'Super System Administrator',
                            
'2' => 'System Administrator',
                            
'3'    => 'Time Keeper',
                            
'4'    => 'OB Encoder',
                            
'5'    => 'Leave Manager',
                            
'6' => 'Leave Administrator',
                            
'7' => 'Records Administrator',
                            );
                            
        
// If leave training or hris training            
        
if ( $this->config->item('active_apps') == 'leave_only' || $this->config->item('active_apps') == 'hris')
        {
            
$user_type = array(
                            
                            
'2' => 'System Administrator'
                            
);
        }
        
        return 
$user_type;
        
        
    }
    
    
// --------------------------------------------------------------------
    
    /**
     * Options for years. We need to specify the 
     * start and end of the year for dropdown list
     *
     * @param int $start
     * @param int $end
     * @param boolean $add_select
     * @return array
     */
    
function year_options($start 2010$end 2020$add_select FALSE)
    {
        
$year $start;
        
        while(
$year <= $end)
        {
            
$options[$year] = $year;
            
            
$year ++;
        }
        
        return 
$options;
    }
    
    
// --------------------------------------------------------------------
    
    



View: my_account.php
PHP Code:
<html>
<
h1>
    <
p>Email: <?php echo $email?></p>
    <p>Password: <?php echo $password?></p>
</h1>
</html> 


Controllers: Account.php
PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

class 
Account extends CI_Controller {

    function 
__construct()
    {
        
parent::__construct();
        
$this->load->helper('url');
        
$this->load->model('user');
    }
    
    function 
my_account()
    {
        
$data['page_name'] = '<b>My Account</b>';
        
        
$data['msg'] = '';
        
$this->load->library('session');
        
$email $this->session->userdata('email');
        
        
        
$op $this->input->post('op');
        
        if(
$op == 1)
        {
            
            
$hidden_password $this->input->post('hidden_password');
            
            
$new_pass         $this->input->post('new_pass');
            
$re_new_pass     $this->input->post('re_new_pass');
            
            
$this->form_validation->set_rules('password2''Current Password''required|callback_current_password');
            
$this->form_validation->set_rules('new_pass''New Password''required|matches[re_new_pass]');
            
$this->form_validation->set_rules('re_new_pass''Re - type new password''required');
            
            if (
$this->form_validation->run($this) == TRUE)
            {
                
$this->User->update_user_pass(do_hash($re_new_pass'md5'), $email);
            }
        }

        
$user $this->User->get_user_data($email);

        
$data['office_name'] = $this->Office->get_office_name($user['office_id']);
        
        
$data['user_type'  $this->User_type->get_user_type($user['user_type']);
        
        
$data['user'] = $user;
                
        
$data['main_content'] = 'my_account';
        
        
$this->load->view('includes/template'$data);
    }

 
I got this error.
   
Reply
#2

All the initiated models available under $this are lower case, regardless of what the model name actually is.

Try lower case $this->user->get_user_data($email);
Reply
#3

Also PHP var is depreciated use public private or protected.

If your always using the sessions then autoload it.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#4

(10-04-2018, 11:45 PM)jelz2018 Wrote: Hello I'm new to both Codeigniter and PHP I have created a system for employees. I want that when an employee login to the system he/she will only see his/her own data related to his/her unique employee ID in the database. To be specific every employee has it's own employee id which populated with data like leave balance, personal datasheets and a lot more. Now what I want to do is to pull those data's and display it when they login to their own account. My login is working but when I try pull out the data it seems it kinda hard. I hope you could help me.

Here's my code for your reference.

Model: Users_model.php (This is for login)

Code:
<?php
    class Users_model extends CI_Model {
        function __construct(){
            parent::__construct();
            $this->load->database();
        }

        public function login($email, $password){
            $query = $this->db->get_where('ats_users', array('email'=>$email, 'password'=>$password));
            return $query->row_array();
        }

    }
?>


View: login.php

Code:
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Employee Login</title>
    <link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>bootstrap/css/bootstrap.min.css">
</head>
<body>
<div class="container">
    <h1 class="page-header text-center">iHRMIS Employee Panel</h1>
    <div class="row">
        <div class="col-sm-4 col-sm-offset-4">
            <div class="login-panel panel panel-primary">
                <div class="panel-heading">
                    <h3 class="panel-title"><span class="glyphicon glyphicon-lock"></span> Login
                    </h3>
                </div>
                <div class="panel-body">
                    <form method="POST" action="<?php echo base_url(); ?>index.php/user/login">
                        <fieldset>
                            <div class="form-group">
                                <input class="form-control" placeholder="Email" type="email" name="email" required>
                            </div>
                            <div class="form-group">
                                <input class="form-control" placeholder="Password" type="password" name="password" required>
                            </div>
                            <button type="submit" class="btn btn-lg btn-primary btn-block"><span class="glyphicon glyphicon-log-in"></span> Login</button>
                        </fieldset>
                    </form>
                </div>
            </div>
            <?php
                if($this->session->flashdata('error')){
                    ?>
                    <div class="alert alert-danger text-center" style="margin-top:20px;">
                        <?php echo $this->session->flashdata('error'); ?>
                    </div>
                    <?php
                }
            ?>
        </div>
    </div>
</div>
</body>
</html>

Views: home.php
Code:
<div class="page-wrapper">
           <!-- ============================================================== -->
           <!-- Bread crumb and right sidebar toggle -->
           <!-- ============================================================== -->
            <div class="page-breadcrumb">
               <div class="row">
                   <div class="col-12 d-flex no-block align-items-center">
                       <h4 class="page-title">Dashboard</h4>
                       <div class="ml-auto text-right">
                           <nav aria-label="breadcrumb">
                               <ol class="breadcrumb">
                                   <li class="breadcrumb-item"><a href="#">Home</a></li>
                                   <li class="breadcrumb-item active" aria-current="page">Library</li>
                               </ol>
                           </nav>
                       </div>
                   </div>
               </div>
           </div>
           <!-- ============================================================== -->
           <!-- End Bread crumb and right sidebar toggle -->
           <!-- ============================================================== -->
               <!-- ============================================================== -->
               <!-- Sales chart -->
               <!-- ============================================================== -->
               <div class="row">
                   <div class="col-12">
                       <div class="card">
                           <div class="card-body">
                               <div class="row">
                                   <!-- column -->
                                   <div class="col-lg-9">
                                   <?php
                                    $user = $this->session->userdata('user');
                                            extract($user);
                                    ?>
                                    <h2>Personal Information </h2>
                                    <h4>User Info:</h4>
                                    <p>Employee ID: <?php echo $employee_id; ?></p>
                                    <p>Office ID: <?php echo $office_id; ?></p>
                                    <p>Fullname: <?php echo $fname.$mname."".$lname; ?></p>
                                    <p>Email: <?php echo $email; ?></p>
                                    <p>Password: <?php echo $password; ?></p>
                                    <a href="<?php echo base_url(); ?>index.php/user/logout" class="btn btn-danger">Logout</a>
                                   </div>

                                   <!-- column -->
                               </div>
                           </div>
                       </div>
                   </div>
               </div>
               <!-- ============================================================== -->
               <!-- Sales chart -->
               <!-- ============================================================== -->
               <!-- ============================================================== -->
               <!-- Recent comment and chats -->
               <!-- ============================================================== -->
</div>

Controllers: Users.php
Code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class User extends CI_Controller {

    function __construct(){
        parent::__construct();
        $this->load->helper('url');
        $this->load->model('users_model');
    }

    public function index(){
        //load session library
        $this->load->library('session');

        //restrict users to go back to login if session has been set
        if($this->session->userdata('user')){
            redirect('home');
        }
        else{
            $this->load->view('login_page');
        }
    }

    public function login(){
        //load session library
        $this->load->library('session');

        $email = $_POST['email'];
        $password = $_POST['password'];

        $data = $this->users_model->login($email, $password);

        if($data){
            $this->session->set_userdata('user', $data);
            redirect('home');
        }
        else{
            header('location:'.base_url().$this->index());
            $this->session->set_flashdata('error','Invalid login. Email or Password is incorrect.');
        }
    }

    public function home(){
        //load session library
        $this->load->library('session');

        //restrict users to go to home if not logged in
        if($this->session->userdata('user')){
            $data['main_content'] = 'home';
            $this->load->view('includes/template', $data);
            //$this->load->view('templates/header');
            //$this->load->view('home');
           //$this->load->view('templates/footer');    
        }
        else{
            redirect('/');
        }
        
    }

    public function logout(){
        //load session library
        $this->load->library('session');
        $this->session->unset_userdata('user');
        redirect('/');
    }

}

Now this is the code I used to pull out the employee data

Model: user.php

PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
User extends CI_Model{

    var 
$fields = array();
    
    
// --------------------------------------------------------------------
    
    /**
     * Constructor
     *
     * @return User
     */
    
function __construct()
    {
        
parent::__construct();
        
        
$this->load->helper('security');
        
$this->load->database();
    }
    
    
// --------------------------------------------------------------------
    
    /**
     * Add new user
     *
     * @param unknown_type $info
     * @return unknown
     */
    
function add_user($data)
    {
        
$this->db->insert('users'$data);
    }
    
    
// --------------------------------------------------------------------
    
    /**
     * Delete user
     *
     * @param varchar $email
     */
    
function delete_user($email)
    {
        
$this->db->delete('users', array('email' => $email)); 
    }
    
    
// --------------------------------------------------------------------
    
    
function get_current_password($email)
    {
        
$current_password '';
        
        
$this->db->select($this->fields);
        
$this->db->where('email'$email);
        
$this->db->limit(1);
        
$q $this->db->get('users');
        
        if (
$q->num_rows() > 0)
        {
            foreach (
$q->result_array() as $row)
            {
                
$current_password $row['password'];
            }
        }
        
        return 
$current_password;
        
        
$q->free_result();
    }
    
    
// --------------------------------------------------------------------
    
    /**
     * Get the maximum user
     *
     * @return varchar
     */
    
function get_max_user()
    {
        
$max_user '';
        
        
$this->db->select_max('email''max_user');
        
$q $this->db->get('users');
        
        if (
$q->num_rows() > 0)
        {
            foreach (
$q->result_array() as $row)
            {
                
$max_user $row['max_user'] + 1;
            }
        }
        
        return 
$max_user;
        
        
$q->free_result();
    }
    
    
// --------------------------------------------------------------------
    
    /**
     * THE FUNCTIONS BELOW ARE FOR TIMEKEEPER
     * Get the data of a user
     *
     * @param varchar $email
     * @return array
     */
    
function get_user_data($email)
    {
        
$data = array();
        
        
$this->db->where('email'$email);
        
$this->db->limit(1);
        
$q $this->db->get('users');
        
        if (
$q->num_rows() > 0)
        {
            foreach (
$q->result_array() as $row)
            {
                
$data $row;
            }
        }
        
        return 
$data;
        
        
$q->free_result();
    }
    
    
// --------------------------------------------------------------------
    
    /**
     * Get users
     *
     * @param array $fields
     * @return array
     */
    
function get_users($fields = array())
    {
        
$data = array();
        
        
$this->db->select($this->fields);
        
$this->db->from('users');
        
$this->db->join('office''office.office_id = user.office_id');
        
$this->db->join('user_type''user_type.id = user.user_type');
        
$this->db->order_by('lname');
        
$q $this->db->get();
        
        if (
$q->num_rows() > 0)
        {
            foreach (
$q->result_array() as $row)
            {
                
$data[] = $row;
            }
        }
        
        return 
$data;
        
        
$q->free_result();
    }
    
    
// --------------------------------------------------------------------
    
    /**
     * Check if email exists
     *
     * @param string $email
     * @return boolean
     */
    
function is_email_exists($email)
    {
        
$this->db->select('email');
        
$this->db->where('email'$email);
        
$q $this->db->get('users');
        
        
        if (
$q->num_rows() > 0)
        {
            return 
TRUE;
        }
        else
        {
            return 
FALSE;
        }
        
        
$q->free_result();
    }
    
    
// --------------------------------------------------------------------
    
    /**
     * Tells whether a user is valid or not
     *
     * @param varchar $email
     * @param varchar $password
     * @return boolean
     */
    
function is_valid_user($email$password)
    {
        
$this->db->where('email'$email);
        
$this->db->where('password'$password);
        
$this->db->where('stat'1);
        
        
$q $this->db->get('users');
        
        
        
        if (
$q->num_rows() > 0)
        {
            return 
TRUE;
        }
        else 
        {
            return 
FALSE;
        }
        
        return 
$data;
        
        
$q->free_result();
    }
    
    
// --------------------------------------------------------------------
    
    /**
     * Update or edit user account
     *
     * @param array $data
     * @param int $user_id
     */
    
function update_user($data$id '')
    {
        
$this->db->where('id'$id);
        
$this->db->update('users'$data); 
    }
    
    
// --------------------------------------------------------------------
    
    
function update_user_pass($pass$email)
    {
        
$data = array('password' => $pass);
        
        
$this->db->where('email'$email);
        
$this->db->update('users'$data); 
    }
    
    
// --------------------------------------------------------------------
    
    /**
     * Validate the email and password
     *
     * @param mixed $email
     * @param mixed $password
     * @return array
     */
    
function validate_user($email$password)
    {
        
$data = array();
        
        
$data['system_message'] = '';
        
        
//Encript password
        
$password do_hash($password'md5');
        
        
$options = array('email' => $email'password' => $password'stat' => 'Active');
        
$q $this->db->get_where('users'$options1);
            
        if (
$q->num_rows() > 0)
        {
            
$row $q->row();
            
            
$fname         $row->fname;
            
$lname         $row->lname;
            
$mname         $row->mname;
            
$user_type     $row->user_type;
                
            
$email $email;
                
            
$session_data = array(
                            
'email'    => $row->email
                            
'lname'     => $row->lname,
                            
'office_id' => $row->office_id,
                            
'user_type' => $row->user_type
                            
);

            
$this->session->set_userdata($session_data);
                
            
$data['system_message'] = 'valid';
                
        }
            
        return 
$data;
    }
    
    
// --------------------------------------------------------------------
    


Models: options.php
PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
Options extends CI_Model {

    
// --------------------------------------------------------------------
    
    /**
     * Constructor
     *
     * @return Options
     */
    
function __construct()
    {
        
parent::__construct();
        
$this->load->database();
    }
    
    
// --------------------------------------------------------------------
    
    /**
     * Options for dropdown list
     * day 1-31
     *
     * @return array
     */
    
function days_options()
    {
        
$day 1
        
        while(
$day != 32)
        {
            
//Add leading zero to month
            
$x sprintf("%02d"$day);
            
            
$options[$x] = $x;
        
            
$day ++; 
        }
        
        return 
$options;
        
    }
    
    
// --------------------------------------------------------------------
    
    
function budget_expenditures_options($e '')
    {
        
$this->load->model('budget_expenditure_m');
            
        
$options  = array();
        
        
$b = new Budget_expenditure_m();
        
        
$expenditures $b->order_by('expenditures')->get();
        
        
$options[0] = '---ALL---';
        
        foreach(
$expenditures as $expenditure)
        {
            
$options[$expenditure->id] = $expenditure->expenditures;
        }
        
        return 
$options;
        
    }
    
    
    
// --------------------------------------------------------------------
    
    /**
     * Options for dropdown list
     * hour 01-24
     *
     * @return array
     */
    
function hour_options$add_blank FALSE)
    {
        
$hour 1
        
        if ( 
$add_blank == TRUE)
        {
            
$options[0] = '';
        }
        
        while(
$hour != 25)
        {
            
//Add leading zero to month
            
$x sprintf("%02d"$hour);
            
            
$options[$x] = $x;
        
            
$hour ++; 
        }
        
        return 
$options;
        
    }
    
    
// --------------------------------------------------------------------
    
    
function group_options($add_select FALSE)
    {
        
$options = array();
        
        
$g = new Group_m();
        
$g->order_by('name');
        
$groups $g->get();
        
        if (
$add_select == TRUE)
        {
            
$options[] = 'SELECT GROUP';    
        }
        
        foreach(
$groups as $group)
        {
            
$options[$group->id] = $group->name;
        }
        
        return 
$options;
    }
    
    
// --------------------------------------------------------------------
    
    /**
     * Options for leave type
     *
     * @param boolean $add_select
     * @return array
     */
    
function leave_type_options($add_select FALSE)
    {
        
        
$leave_types $this->Leave_type->leave_type_list();
        
        
        if (
$add_select == TRUE)
        {
            
$options[] = 'SELECT OFFICE';    
        }
        
        foreach(
$leave_types as $leave_type)
        {
            
$options[$leave_type['id']] = $leave_type['leave_name'];
        }
        
        
// Updated 3.1.2012 since version 2.00.00
        // add Undertime in type of leave
        // for laguna use
        
        
$lgu_code $this->Settings->get_selected_field('lgu_code'); 
        
        
// Laguna Province
        
if ( $lgu_code == 'laguna_province' )
        {
            
$options['undertime'] = '-- Undertime --';
        }
        
// end update
        
        
return $options;
    }
    
    
// --------------------------------------------------------------------
    
    /**
     * Options for month
     *
     * @param boolean $add_select
     * @return array
     */
    
function month_options($add_select FALSE)
    {
        
$month 1;
        
        while(
$month != 13)
        {
            
//Add leading zero to month
            
$x sprintf("%02d"$month);
            
            
$options[$x] = $this->Helps->get_month_name($month);
            
            
$month ++;
        }
        
        return 
$options;
    }
    
    
// --------------------------------------------------------------------
    
    /**
     * Options for office
     *
     * @param boolean $add_select
     * @return array
     */
    
function office_options($add_select FALSE)
    {
        
$options = array();
        
        
$this->Office->fields = array('office_id''office_name');
        
        
$offices $this->Office->get_offices();
        
        
        if (
$add_select == TRUE)
        {
            
$options[] = 'SELECT OFFICE';    
        }
        
        foreach(
$offices as $office)
        {
            
$options[$office['office_id']] = $office['office_name'];
        }
        
        return 
$options;
    }
    
    
// --------------------------------------------------------------------
    
    /**
     * Options for divisions
     *
     * @param boolean $add_select
     * @return array
     */
    
function division_options($office_id 1)
    {
        
$options  = array();
        
        
$d = new Division();
        
        
$divisions $d->where('office_id'$office_id)->order_by('order')->get();
                        
        foreach(
$divisions as $division)
        {
            
$options[$division->id] = $division->name;
        }
        
        return 
$options;
    }
    
    
// --------------------------------------------------------------------
    
    /**
     * Options for salary grade
     *
     * @return array
     */
    
function salary_grade()
    {
        
$x 1;
        while(
$x != 31)
        { 
            
$sg_options[$x] = $x;
            
$x++;
        }
        
        return 
$sg_options;
    }
    
    
// --------------------------------------------------------------------
    
    /**
     * Options for shift
     *
     * @return array
     */
    
function shift()
    {
        
$shifts $this->Shift->shift_list();
    
        foreach(
$shifts as $shift)
        {
            
$shifts_options[$shift['shift_id']] = $shift['name'];
        }
        
//$shifts_options[0] = 'Other...';
        
        
return $shifts_options;
    }
    
    
// --------------------------------------------------------------------
    
    /**
     * Options for step
     *
     * @return array
     */
    
function step()
    {
        
$x 1;
        while(
$x != 9)
        { 
            
$step_options[$x] = $x;
            
$x++;
        }
        
        return 
$step_options;
    }
    
    
// --------------------------------------------------------------------
    
    /**
     * Options for type of employment
     *
     * @param boolean $all
     * @return array
     */
    
function type_employment($all FALSE)
    {
        if (
$all == TRUE)
        {
            
$type_employment['all'] = 'All';
        }    
        
        
$type_employment['1'] = 'Permanent';
        
$type_employment['2'] = 'Casual';
        
$type_employment['3'] = 'Contract of Service';
        
$type_employment['4'] = 'Job Order';
        
$type_employment['5'] = 'Co Terminous';
        
$type_employment['6'] = 'Elected Official';
        
$type_employment['7'] = 'Temporary';
        
$type_employment['8'] = 'Contractual Plantilla';
                            
        return 
$type_employment;
    }
    
    
// --------------------------------------------------------------------
    
function training_type_options($training_type '')
    {
        
$this->load->model('training_type');
            
        
$options  = array();
        
        
$t = new Training_type();
        
        
$types $t->order_by('training_type')->get();
        
        
$options[0] = '---ALL---';
        
        foreach(
$types as $type)
        {
            
$options[$type->id] = $type->training_type;
        }
        
        return 
$options;
        
    }
    
    
// --------------------------------------------------------------------
    
function training_contact_type_options($training_type '')
    {
        
$this->load->model('training_contact_type');
            
        
$options  = array();
        
        
$t = new Training_contact_type();
        
        
$types $t->order_by('contact_type')->get();
        
        
$options[0] = '---ALL---';
        
        foreach(
$types as $type)
        {
            
$options[$type->id] = $type->contact_type;
        }
        
        return 
$options;
        
    }
    
    
// --------------------------------------------------------------------
    
    /**
     * Options for user type
     *
     * @return array
     */
    
function user_type()
    {
        
$user_type = array(
                            
'1' => 'Super System Administrator',
                            
'2' => 'System Administrator',
                            
'3'    => 'Time Keeper',
                            
'4'    => 'OB Encoder',
                            
'5'    => 'Leave Manager',
                            
'6' => 'Leave Administrator',
                            
'7' => 'Records Administrator',
                            );
                            
        
// If leave training or hris training            
        
if ( $this->config->item('active_apps') == 'leave_only' || $this->config->item('active_apps') == 'hris')
        {
            
$user_type = array(
                            
                            
'2' => 'System Administrator'
                            
);
        }
        
        return 
$user_type;
        
        
    }
    
    
// --------------------------------------------------------------------
    
    /**
     * Options for years. We need to specify the 
     * start and end of the year for dropdown list
     *
     * @param int $start
     * @param int $end
     * @param boolean $add_select
     * @return array
     */
    
function year_options($start 2010$end 2020$add_select FALSE)
    {
        
$year $start;
        
        while(
$year <= $end)
        {
            
$options[$year] = $year;
            
            
$year ++;
        }
        
        return 
$options;
    }
    
    
// --------------------------------------------------------------------
    
    



View: my_account.php
PHP Code:
<html>
<
h1>
    <
p>Email: <?php echo $email?></p>
    <p>Password: <?php echo $password?></p>
</h1>
</html> 


Controllers: Account.php
PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

class 
Account extends CI_Controller {

    function 
__construct()
    {
        
parent::__construct();
        
$this->load->helper('url');
        
$this->load->model('user');
    }
    
    function 
my_account()
    {
        
$data['page_name'] = '<b>My Account</b>';
        
        
$data['msg'] = '';
        
$this->load->library('session');
        
$email $this->session->userdata('email');
        
        
        
$op $this->input->post('op');
        
        if(
$op == 1)
        {
            
            
$hidden_password $this->input->post('hidden_password');
            
            
$new_pass         $this->input->post('new_pass');
            
$re_new_pass     $this->input->post('re_new_pass');
            
            
$this->form_validation->set_rules('password2''Current Password''required|callback_current_password');
            
$this->form_validation->set_rules('new_pass''New Password''required|matches[re_new_pass]');
            
$this->form_validation->set_rules('re_new_pass''Re - type new password''required');
            
            if (
$this->form_validation->run($this) == TRUE)
            {
                
$this->User->update_user_pass(do_hash($re_new_pass'md5'), $email);
            }
        }

        
$user $this->User->get_user_data($email);

        
$data['office_name'] = $this->Office->get_office_name($user['office_id']);
        
        
$data['user_type'  $this->User_type->get_user_type($user['user_type']);
        
        
$data['user'] = $user;
                
        
$data['main_content'] = 'my_account';
        
        
$this->load->view('includes/template'$data);
    }

 
I got this error.

Hello,

you can see you got error undefined property so you have to set a proper variable for it.

you have to replace your code :

$this->db->get_where();   

with following code

$this->user->get_user_data();

Execetley you have to use like this

$this->db->get_Where('users', array('email'=>'[email protected]'));
echo $this->db->last_query();
$result_2 = $this->db->query->result();
Reply
#5

try

SELECT number from numberTable nt
JOIN idTable it ON  it.ID = nt.ID
WHERE it.ID = `your given id`

as i think your both tables are referenced with id
Reply




Theme © iAndrew 2016 - Forum software by © MyBB