How to make my text view get id from my session? - php

I want to show session data in a text box. I use the following model to get the id and show the data in a drop-down menu. But I don't know how to get [id_dosen] from the session from my database. How can I do that?
This is my _dosen_model.php_:
public function getDosen(){
$this->db->order_by('id_dosen','ASC');
$dosen= $this->db->get('dosen');
return $dosen->result_array();
}
This is my controller for my login:
public function login() {
$user = $_POST['user'];
$pass = $_POST['pass'];
$QuerySaya = $this->db->query(
"SELECT * FROM user LEFT JOIN mahasiswa ON user.id_user=mahasiswa.id_user LEFT JOIN dosen ON user.id_user=dosen.id_user
WHERE user.username='$user' AND user.password='$pass';"
);
if ($QuerySaya->num_rows() == 0) {
$this->load->view('login');
} else {
$data = $QuerySaya->row();
$this->session->set_userdata('id_admin_ti', $data->id_user);
$this->session->set_userdata('username', $data->username);
$this->session->set_userdata('password', $data->password);
$this->session->set_userdata('level_user', $data->level_user);
$this->session->set_userdata('id_kelas', $data->id_kelas);
$this->session->set_userdata('id_dosen', $data->id_dosen);
$this->session->set_userdata('id_mhs', $data->id_mhs);
$level = $this->session->userdata('level_user');
if ($level == 'mahasiswa') {
$this->load->view('dashboard_mhs', $data);
}else if($level == 'admin'){
$this->load->view('dashboard', $data);
}else if($level == 'dosen'){
$this->load->view('dashboard_dosen', $data);
}
}
}
And this is my view:
<select id="dosen" name="dosen" class="required form-control input-xs" placeholder="Mata Kuliah" type="text" required="required">
<option>-Nama Dosen-</option>
<?php
foreach ($dosen as $a) {
echo "<option value=".$a['id_dosen'].">".$a['nama_dosen']."</option>";
}
?>
</select>

Related

Updating value 0 instead of array in the table of multiple checkbox in codeigniter

The form updates the value 0 in the database, it should update the array of check boxes but it's not working.
I've updated the controller and view, please take a look and check if possible.
Controller
function type()
{
if($this->session->userdata("user_id"))//If already logged in
{
$userID = $this->session->userdata("user_id");
$data['user'] = $this->m_user->getRows($userID);
$data['title'] = 'Type | Categories';
$type=$this->form_validation->set_rules('type[]', 'Type', 'required');
if($this->form_validation->run() === FALSE){
$data['categories'] = $this->category_model->get_categories();
$data['main_view']="users/v_type";
$this->load->view('layouts/main',$data);
$this->load->view('layouts/sidebar',$data);
$this->load->view('layouts/footer',$data);
} else {
// Encrypt password
$type = $this->input->post('type');
$this->m_user->type($type);
}else
{
redirect(base_url().'user/');
}
}
View
<form action="<?= base_url()?>user/type" method="post">
<label><input type="checkbox" value="Textile " name="type[]">Textile </label>
<label><input type="checkbox" value="Textile2 " name="type[]">Textile2 </label>
</form>
Model
public function type(){
if($this->session->userdata("user_id"))//If already logged in
{
$userID = $this->session->userdata("user_id");
$data['user'] = $this->m_user->getRows($userID);
$type = $this->input->post('type');
$data=array('type'=>json_encode(implode(",", $type)),);
$this->db->where('user_id', $userID);
return $this->db->update('users', $data);
}
}
What I understand is, issue is in $data parameter
$data['user'] = $this->m_user->getRows($userID); //fetching in parameter data
$type = $this->input->post('type');
$data=array('type'=>json_encode(implode(",", $type)),); //this should have some column like $data['type'] maybe
}
Try this your Updated Model Function code, and tell me what you find
public function type(){
//If already logged in
if($this->session->userdata("user_id"))
{
$userID = $this->session->userdata("user_id");
$data['user'] = $this->m_user->getRows($userID);
$type = $this->input->post('type');
echo'<pre>';print_r($type);
$data=array('type'=>json_encode(implode(",", $type)),);
echo'<pre>';print_r($data);
$this->db->where('user_id', $userID);
$flag = $this->db->update('users', $data);
echo $flag;die;
}
}

Codeigniter: Get userdata from another table using session

I'm trying to print userdata from another table with session. The table with the session is "userlogin" while the table with the data is "userinfo"
Here's what I've done so far
Controller
public function index()
{
if ( $this->session->userdata('logged_in') )
{
$session_data = $this->session->userdata('userinfo');
$id = $session_data['id'];
$this->load->model('Userdata_model');
$result = $this->Userdata_model->get_user_data3($id);
if($result==0)
{
echo 'No user Found';
}
else
{
$data['id']=$result;
$this->load->view("userdata_view", $data);
}
}
}
Model
public function get_user_data3($id)
{
if (isset($this->session->userdata['logged_in'])) {
$userid = ($this->session->userdata['logged_in']['id']);
} else {
header("location: http://localhost/test");
}
$query = $this->db->query("SELECT * FROM userinfo WHERE id='$id'");
foreach ($query->result_array() as $row)
{
echo $row['fName'];
echo $row['phone'];
}
}
View
<?php foreach ( $query->result_array as $new_user )
{
?>
<h4>Your name: <?php echo $new_user['fName'] ?> </h4>
<?php
}
?>
change function in controller :
public function index()
{
if ( $this->session->userdata('logged_in') )
{
$session_data = $this->session->userdata('userinfo');
$id = $session_data['id'];
$this->load->model('Userdata_model');
$data['result'] = $this->Userdata_model->get_user_data3($id);
$this->load->view("userdata_view", $data);
}
}
change model function like:
public function get_user_data3($id)
{
$query = $this->db->query("SELECT * FROM userinfo WHERE id='$id'");
return $query;
}
and view :
<?php if($result->num_rows() == 0){
echo 'No user found';
}
else {
foreach ( $result->result_array() as $new_user ){ ?>
<h4>Your name: <?php echo $new_user['fName'] ?> </h4>
<?php }
}
?>

passing user information to view in codeigniter

I am trying to get users details from the database and put it in session so that it can be used in the view. I have tried all I can but I keep getting error. Undefined Variable Email.
MODEL:
function login($username, $password)
{
$this->db->select('authTbl.id, authTbl.password, authTbl.username, authTbl.email, authTbl.mobile');
$this->db->from('users as authTbl');
$this->db->where('authTbl.username', $username);
$this->db->where('authTbl.isDeleted', 0);
$query = $this->db->get();
$user = $query->result();
if(!empty($user)){
if(verifyHashedPassword($password, $user[0]->password)){
return $user;
} else {
return array();
}
} else {
return array();
}
}
CONTROLLER:
function isLoggedIn()
{
$isLoggedIn = $this->session->userdata('isLoggedIn');
$data['title'] = 'Login';
if(!isset($isLoggedIn) || $isLoggedIn != TRUE)
{
$this->load->view('templates/header');
$this->load->view('users/login', $data);
$this->load->view('templates/footer');
}
else
{
redirect('posts');
}
}
/**
* This function used to logged in user
*/
public function login()
{
$this->load->library('form_validation');
$data['title'] = 'Login';
$this->form_validation->set_rules('username', 'Username', 'required|max_length[128]|trim');
//$this->form_validation->set_rules('password', 'Password', 'required|max_length[32]|');
if($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header');
$this->load->view('users/login', $data);
$this->load->view('templates/footer');
}
else
{
$username = $this->input->post('username');
$password = $this->input->post('password');
$result = $this->user_model->login($username, $password);
if(count($result) > 0)
{
foreach ($result as $res)
{
$sessionArray = array('id'=>$res->id,
'username'=>$res->username,
'email'=>$res->email,
'mobile'=>$res->mobile,
'isLoggedIn' => TRUE
);
$this->session->set_userdata($sessionArray);
$this->session->set_flashdata('user_login', 'Welcome');
redirect('posts');
}
}
else
{
$this->session->set_flashdata('login_fail', 'Email or password mismatch');
redirect('users/login');
}
}
}
VIEW:
<?php echo $email; ?>
You are creating the session for user information
Try to fetch the session data :
echo $this->session->userdata('email');
or trying to pass the data in views $data
$email will not work because writing $email means ordinary variable but in your case you have to write like :
<?php echo $this->session->userdata('email'); ?>
If You are using flashdata, then You can get it using below code:
Controller
if (empty($this->session->userdata('UserID'))) {
$this->session->set_flashdata('flash_data', 'You don\'t have access!');
$this->load->view("initialHeader");
$this->load->view("login");
redirect('Login');
}
View
<?php if(!empty($this->session->flashdata('flash_data'))) {
?>
<div class="alert alert-danger">
<?php echo $this->session->flashdata('flash_data'); ?>
</div>
<?php } ?>
If You are using tmp data as a session, please refer below code:
Controller
<?php
if (!empty($data)) {
$this->session->set_userdata('permission_error_msg', $data);
$this->session->mark_as_temp('permission_error_msg', 10); // For 10 Seconds
$this->load->view('dashboard', array($title, $data));
} ?>
View
<?php if ($this->session->tempdata('permission_error_msg')) { ?>
<b class="login_error_msg"><?php
if (!empty($this->session->tempdata('permission_error_msg'))) {
echo $this->session->tempdata('permission_error_msg');
}
?></b>
<?php } ?>
Thank You.

how to query two tables from database and show the result in a single row using codeigniter

I have two tables One for users and the other for project listing
My problem is that i want to display each project(from project_table)and email belonging to user who listed the project(from user_table) on a single row
The project_table has a row for user_id(i.e id from user_table that identifies the user who posted the project)
here's my view(project_view):
I this case im displaying data from project_table but i want to display email for a particular user from user_table
<?php
foreach($query as $row)
{
?>
<p> <?echo $row->pro_name ?></p>
<p> <?echo $row->duration ?></p>
<p> <?echo $row->budget ?></p>
<p>User email will be displayed here</p>
<?
}
my model:
function get_projects()
{
$query = $this->db->get('project_table');
return $query->result();
}
my controller:
function show_projects()
{
$data['query']=$this->project_model->get_projects();
$this->load->view('project_view', $data);
}
Any ideas on how to implement this will be much appreciated
You can use joined query in your model
function get_projects()
{
$query =$this->db->select('p.*,u.email')
->from('project_table p')
->join('user_table u','p.user_id = u.id')
->get();
return $query->result();
}
And in your view you can do so
<?php
foreach($query as $row)
{
?>
<p> <?php echo $row->pro_name; ?></p>
<p> <?php echo $row->duration; ?></p>
<p> <?php echo $row->budget; ?></p>
<p><?php echo $row->email;?></p>
<?php
}
?>
I would rewrite controller and model function to allow for a user parameter:
function get_projects($user=null)
{
if ($user == null){
$query = $this->db->get('project_table');
}else{
$query = $this>db->get('project_table')->where('user_id',$user);
}
return $query->result();
}
function show_projects($user)
{
$data['query']=$this->project_model->get_projects($user);
$this->load->view('project_view', $data);
}
Now you can call your controller with a user parameter
http://..../show_user/1
1st approach is JOINs
$this->db->select('project_table.* as project, user.id as user_id, user.email as email')
->from('project_table')
->join('user', 'project.id = user_id');
$result = $this->db->get();
2nd not recommended
function get_projects()
{
$query = $this->db->get('project_table');
return ($query->num_rows() > 0) ? $query->result() : FALSE;
}
function get_email($id = FALSE) {
if ($id === FALSE) return FALSE;
$query = $this->db->get_where('user', array('user_id' => $id));
return ($query->num_rows() > 0) ? $query->result() : FALSE;
}
somewhere in controller...
if (!$projects = $this->model->get_projects()) {
foreach ($projects as $key => $project) {
# code...
$projects[$key]->user_email = $this->model->get_email($project->user_id);
}
//projects is now with user_email field
$this->load->view()...
} else {
//no data recieved
redirect(); //redirect to default_controller
}
Try join this -
1) Model
function get_projects()
{
$this->db->select('*');
$this->db->from('project_table');
$this->db->join('user_table', 'project_table.user_id = user_table.id');
$query = $this->db->get();
if($query->num_rows() > 0)
{
return $query->result();
}
}
2) Controller
$data['user_project'] = $this->project_model->get_projects();
$this->load->view($view, $data);
3) View
foreach($user_project as $row)
{
echo "<p>" .$row->pro_name. "</p>";
echo "<p>" .$row->duration. "</p>";
echo "<p>" .$row->budget. "</p>";
echo "<p>" .$row->email. "</p>";
}

PHP to SQL on-change not updating

I have a fairly general problem. I have a small form
<form action="<?=base_url();?>ticket/close_ticket/<?=$ticket_details['id'];?>" method="post" id="close_ticket" name="close_ticket">
<ul>
<li><label for="frm_status">Status<span class="req">*</span></label>
<span class="input">
<select id="frm_status" name="status" onchange="this.form.submit()">
<option value="<? if ($ticket_details['status'] == "Open") $status= "1"; else $status= "2"; echo $status;?>"><? if ($ticket_details['status'] == "Open") $status= "Open"; else $status= "Closed"; echo $status;?></option>
<option value="<? if ($ticket_details['status'] == "Open") $status= "2"; else $status= "1"; echo $status;?>"><? if ($ticket_details['status'] == "Open") $status= "Closed"; else $status= "Open"; echo $status;?></option>
</select>
</span>
</li>
</ul>
</form>
This form contains the drop list option box, that on change submits the form to the close ticket controller......
public function close_ticket()
{
$this->load->model('ticket_model');
$ticket_id = mysql_real_escape_string($this->uri->segment(3));
if($_POST)
{
//save ticket
unset ($_POST['id']);
$_POST['id'] = $ticket_id;
$this->ticket_model->close_ticket($_POST);
redirect(base_url().'ticket/edit/'.$ticket_id.'/');
return;
}
redirect(base_url().'ticket/edit/'.$ticket_id.'/');
}
which it does. This controller is to post the information to the model to update the SQL.....
public function close_ticket($ticket_post)
{
$query = $this->db->query("SELECT id FROM ".$this->tables_ticket." WHERE id = '".mysql_real_escape_string($ticket_post['id'])."';");
if($query->num_rows() > 0)
{
$row = $query->row();
$query = $this->db->query("UPDATE ".$this->tables_ticket."
SET
status = '".mysql_real_escape_string($ticket_post['status'])."'
WHERE
id = '".mysql_real_escape_string($ticket_post['id'])."'
");
}
if($this->db->affected_rows() > 0) return true;
else return false;
}
then after all this, redirect back to the form. I am assuming that on redirect the form will then populate the drop list with the updated data. This is where I am struggling, as It sends the changed data, and somewhere it is not registering the change and returning the page, unchanged.
Question, would this work with a confirmation/secondary submission page, followed by redirect, and is it that I am trying to return the changed data in the same function where it is failing?
$body_data['ticket_list'] = $this->ticket_model->list_ticket();
$body_data['ticket_details'] = $this->ticket_model->get_ticket($ticket_id);
$body_data['ticket_summary'] = $this->ticket_model->list_ticket_summary($ticket_id);
$body_data['customer_list'] = $this->ticket_model->get_customer_details($ticket_id);
$body_data['precan_list'] = $this->ticket_model->list_messages();
$body_data['users_list'] = $this->ticket_model->list_users();
$foot_data['accordian_active'] = 5;
$this->load->view('head',$head_data);
$this->load->view('sidebar/service',$head_data);
$this->load->view('ticket/edit',$body_data);
$this->load->view('foot',$foot_data);
return;
The edit function simply returns a range of population query lists.
unless i need a new query to repopulate the ticket_details list?

Categories