Fetching data using a unique employee id

Hi, I consider myself as a beginner in PHP programming specially in CodeIgniter framework. Now I have a working login with session for my web app. But what I want now is that when an employee login to the web app only the datas associated with his/her unique employee id will be displayed. Example when he logged in she/he will see his/her own profile picture, full name, address etc. I hope for your timely response. Thank you in advance guys.

Well, normally when you create a database table, you always place the first field as an INT field and name it “id”. Normally, in the user’s table this ID field is locked to a person’s records. In the user’s table, you would have their name (first/middle/last) or just one field for the entire name, email address which you can use as the log in, a password, addresses, phone number, date of birth, etc. Whatever data is needed for any one user. The “id” field is only created once when the user account is created and never changed from then on. It usually is a “auto-increment” and “primary index” field for that table. Then, from there on the “id” field is used to identify the owner in other tables. For instance, if it is a sales site, in the order’s table you would have a field named “customer_id” which you would fill with the “id” of the person logged in when they opened an order up. Or, if it is a gaming site, an on-going game would have a field named “game_owner_id” or something similar that lets you identify who started that one game. In a forum or posting system, you would assign a “post_owner” for the original post like your ID in this thread. Then, you would have a field for comments under that thread name something like “comment_owner” for each response under the main original post.

Using that type of format, you can always trace parts of your data back to who created it or owns it using a unique ID that is created when they register and become a member of your site.

This is also basically how you create all tables in your database. If you think out the logic of your site and create the needed tables thinking ahead on how you use the data, it will make your programming much easier. For instance, in this manner, when a user logs in, the first thing you do is retrieve all of their own data from the user’s table using the email address and password as the base. Once you match both of those in your user’s table, you will have their user table’s “id” number and with that can use their own data throughout the site.

It is actually very easy once you think about it. Ask any further questions and we can help you get started…

Thanks for the explanation it’s highly appreciated. Actually this is kinda of Human resource management system based on Codeigniter framework. I’ve created the login. Now I dont have the idea where to start in creating the controller, model and the view file to display the data of the employee based on her/his unique employee ID on the database.

Well, there are tons of tutorials on how to do this. Here is one that explains most of it. i do not use the Codeigniter, but, this might help: Codeigniter Controller-Model Tut

1 Like

Thank you sir for the information. To explain further my problem. For example if Employee A logged in to the system the information of employee A will be fetch automatically from the database based on his/her employee id in the database. Now I just want some sample codes on how to do it. What codes to be put into the controller, model and view files. Thanks

Well, first an employee would be a user. In creating a user system, you first need a database set up with all of the possible user information. Sometimes it is just their username, email address, an id number to make each user unique. Some other possible fields would be full name, addresses, phone numbers and whatever else you need for your record keeping.
Next, you would need to create a way for them to log in. In that area, you would validate their login with a password system and once logged in, you would use their user id which would be in the user’s table. Once you have their ID number, you would use that to pull other data out of your database and display only their data. If they enter reports or mark items or sell items or do any other such thing that an employee would do, you mark those items with the user-ID number to show that item was created or handled by that one user/employee.
Hope that makes sense so far. Now, I do not use codeigniter, but found a better tutorial for you that talks about all of these parts and ends up with a full user controller. I think if you read this, it will help you get started. User-Authentication Once you read this and start testing, show us your code and we can help you fix it if it does not work correctly. Hope this helps…

Here’s the code:

Controllers : Account.php

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');
	$username = $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'), $username);
		}
	}

	$user = $this->User->get_user_data($username);

	$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);
}

Model: User_model.php

<?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(); } } ?>

This is not pulling the username, so it sends out an error because the variable $username is empty.

$username = $this->session->userdata('email');

This is pulling the email address. Is that what you are using for the username?
If so, verify that this session info contains the userdata. If not, change the email to the username.
Not sure… But, the error messages appear easy to understand.

I change it to email, because the login userdata is using email for login, but still not working.

Okay, that is no problem. The error says that there is nothing in it. So, you did not show the code where it was pulled from. You must make sure that the $this->session->userdata actually contains the email address.

Also, the other error “require_once” means that line is not correct either. So, I am confused why you have two errors on one line. Which of those lines is #43?

Sponsor our Newsletter | Privacy Policy | Terms of Service