I need select Name from databese where Email = $email;
if ($result) {
$Name = $this->db->select('Name');
$this->db->from('users');
$this->db->where('Email',$Email);
$sess_array = array(
'Name' => $Name
);
$this->session->set_userdata('logged_in',$sess_array);
print_r($sess_array);
}
You need to fetch Name from database then assign to your session
if ($result) {
$this->db->select('Name');
$this->db->from('users');
$this->db->where('Email',$Email);
$query=$this->db->get();
$result=$query->row(); // fetch single data Name
$sess_array = array(
'Name' => $result->Name // set Name to array
);
$this->session->set_userdata('logged_in',$sess_array);
}
Try this,
if ($result) {
$this->db->select('Name');
$query = $this->db->get_where('users',array('Email' => $Email));
$result = $query->row();
$sess_array = array(
'Name' => $result->Name
);
$this->session->set_userdata('logged_in',$sess_array);
print_r($sess_array);
}
also refer CodeIgniter Select Query
Related
I am trying to create a function that gets the name and clientid. What is the proper way to return an array? so that I can have multiple values returned.
function get_client_name($conn, $clientid){
$response = array();
$query = "SELECT NAME, CLIENTID FROM LCCLIENT WHERE CLIENTID = '". $clientid ."'";
$sql = oci_parse($conn, $query);
$exec = oci_execute($sql);
if($exec){
$row = oci_fetch_array($sql);
$response[] = array(
'CLIENTID' => trim($row['CLIENTID']),
'NAME' => trim($row['NAME'])
);
}
return json_encode($response);
}
echo get_client_name($conn, '2000000800')[0]['CLIENTID'];
echo get_client_name($conn, '2000000800')[0]['NAME'];
You just return your array, without encoding it in JSON format. Also don't call this function two times, because it's redundant to call DB two times for the same data. Second thing, you shouldn't pass $conn in this function. You should develop some structure for database connections.
function get_client_name($conn, $clientid)
{
$response = array();
$query = "SELECT NAME, CLIENTID FROM LCCLIENT WHERE CLIENTID = '" . $clientid . "'";
$sql = oci_parse($conn, $query);
$exec = oci_execute($sql);
if ($exec) {
$row = oci_fetch_array($sql);
$response[] = array(
'CLIENTID' => trim($row['CLIENTID']),
'NAME' => trim($row['NAME'])
);
}
return $response;
}
$clientName = get_client_name($conn, '2000000800');
echo $clientName[0]['CLIENTID'];
echo $clientName[0]['NAME'];
I am trying to set the company name in a session and retrieve the same to connect to a different database, i am setting the session using $this->session->set_userdata($newdata); and retrieving the session using $companyName = $this->session->userdata['newdata']['company']; but somehow retrieval is not happening and i am unable to load the correct db for updating logout information i.e it simply does not update the pr_system_attendance table by connecting to a different db. I am getting the correct value if i echo $company; after $company = $row1->company; this is FYI
My model code is as follows:
function check_admin_login(){
$this->db->where('username', trim($this->input->post('username')));
$this->db->where('userpass ', sha1(trim($this->input->post('userpass'))));
$this->db->where('status', '1');
$this->db->where('deleted', '0');
$this->db->select('*');
$query = $this->db->get($this->myTables['users']);
if($query->num_rows() > 0){
$row = $query->row();
$this->db->where('userid', $row->id);
$this->db->select('firstname,lastname,profileimage,company');
$query1 = $this->db->get($this->myTables['users_details']);
$row1 = $query1->row();
$newdata = array(
'is_admin_logged_in' => true,
'admin_user_name' => $row->username,
'admin_userpass' => $row->userpass,
'admin_id'=>$row->id,
'admin_lastlogin'=>date("d-m-Y H:i:s",$row->lastlogin),
'admin_lastloginip'=>$row->lastloginip,
'lastrefresh'=>time(),
'company'=>$row1->company
);
$company = $row1->company;
$this->session->set_userdata($newdata);
$companyName = $this->session->userdata['newdata']['company'];
$this->update_admin_login_time($this->session->userdata('admin_id'));
$this->admin_init_elements->set_global_user($row->username,$row->userpass);
if($this->input->post('remember'))
{
$cookie = array('name' => 'username','value' => $row->username,'expire' => time()+7600,'secure' => false);
$this->input->set_cookie($cookie);
}
$name = $row1->firstname.' '.$row1->lastname;
$cookie1 = array('name' => 'name','value' => $name,'expire' => time()+7600,'secure' => false);
$this->input->set_cookie($cookie1);
$cookie2 = array('name' => 'image','value' => $row1->profileimage,'expire' => time()+7600,'secure' => false);
$this->input->set_cookie($cookie2);
return 'Login Successful';
}else{
return 'Incorrect Username or Password.';
}
}
function logout()
{
global $USER;
$companyName = $this->session->userdata['newdata']['company'];
$otherdb = $this->load->database("$companyName", TRUE);
$this->db->from("$companyName"."pr_users");
$query1 = $this->db->query("Select * from pr_system_attendance where userid = '".$USER->id."' and DATE(`login_time`) = CURDATE()");
date_default_timezone_set('Asia/Calcutta');
if($query1->num_rows() > 0)
{
$row = $query1->row();
$sql1 = "UPDATE pr_system_attendance set logout_time = '".date('Y-m-d H:i:s')."' where userid = '".$USER->id."' and DATE(`login_time`) = CURDATE()";
$query2 = $this->db->query($sql1);
}
$sql="UPDATE `".$this->myTables['users']."` SET
`if_online` = '0'
WHERE `id` = '".$USER->id."'" ;
$query=$this->db->query($sql);
}
Get session data with below line of code
//$companyName = $this->session->userdata['newdata']['company'];
$companyName = $this->session->userdata('company');
I have two different databases with table Phpfox_user , manual_userid and mobile table in them.There can be 3 users with same user_id but different mobile numbers stored in phpfox_user table and manual_userid and there GCM id in table mobile.(with mobile no column as well). Now i want to send notification to all mobile numbers with same user id .
Currently notification is only sent to a single manager only
function request_block($data){
//print_r($data);
$tower=new Tower;
$project=new Project;
$note=new Notification;
$fields=array('user_id', 'pay_opt', 'sale_type', 'hold_status', 'cost', 'is_manager_blocked', 'is_action_taken');
$params=array(
':user_id' => $data['user_id'],
':pay_opt' => $data['buy_opt'],
':sale_type' => $data['sale_type'],
':hold_status' => '1',
':cost' => $data['cost'],
':is_manager_blocked' => '0',
':is_action_taken' => '0',
':id' => $data['flat_id']
);
$where = 'where id= :id';
$tower->block_flat($fields, $params, $where);
$user_details=$tower->get_user_details($data['user_id']);
$flat_details=$tower->get_flat_details_cost($data['flat_id']);
$tower_main=$tower->get_tower_main_details($flat_details['tower_id']);
$project_details=$project->get_project_details($tower_main['project_id']);
$manager_details=$tower->get_user_details($project_details['manager_id']);
$manager_details=$tower->get_manualuser_details($project_details['manager_id']) ;
$mob_details=$tower->get_gcmid($manager_details['cf_mobile']);
$par['mobile']=$manager_details['cf_mobile'] ;
//print_r($flat_details); print_r($tower_main); print_r($project_details);
//$manager_details=$tower->get_user_details($project_details['manager_']);
$data['send_by']='user';
$data['user_id']=$data['user_id'];
$data['manager_id']=$project_details['manager_id'];
$data['message']='You have received a Booking request - by '.$user_details['full_name'].' '.$user_details['cf_mobile'].' for Flat no. '.$flat_details['flat_no'].' in '.$tower_main['tower_name'].' tower of '.$project_details['project_name'].' Project';
$data['msg_details']=$data['message'];
$data['notification_type']='user-to-manager';
$note->insert_notification($data);
$par['msg']=$data['message'];
if(count($mob_details)>=1 && $mob_details['gcmid']!='')
{
$fuck['notification_type']='user-to-manager';
$fuck['notification_text']=$par['msg'];
$fuck['user_id']=$data['user_id'];
$tt['message']=$fuck;
$tt['gcmid']=$mob_details['gcmid'];
send_notification($tt);
}
if(count($tower_images)>=1)
{
$result['data']=$tower_images;
} else {
$result['message']='No result found';
}
$result['message']='Flat block requested';
//print_array($par);
$result['status']='success';
send_sms($par);
$res=json_encode(array($result));
echo $res;}
These are the two functions that gets the user mobile numbers
public function get_user_details($user_id)
{
$params = array( ':user_id' => $user_id);
$sql = "SELECT * FROM phpfox_user, phpfox_user_custom where phpfox_user.user_id=phpfox_user_custom.user_id AND phpfox_user.user_id=:user_id";
$stmt = parent::query($sql, $params, '', 'main');
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) :
return $result[]=$row;
endwhile;
return $result;
}
public function get_manualuser_details($user_id)
{
$params = array( ':user_id' => $user_id);
$sql = "SELECT * FROM manual_userid where user_id=:user_id";
$stmt = parent::query($sql, $params);
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) :
return $res=$row;
endwhile;
return $result;}
public function get_gcmid($data)
{
$mobile=str_replace('+','0',$data);
$params = array( ':mobile' => $mobile);
$sql = "SELECT * FROM mobile where mobile=:mobile";
$stmt = parent::query($sql, $params);
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) :
return $res=$row;
endwhile;
}
I am using code igniter, how can I give the output of my query to the function returnQuery? Then pass that value to my controller.
public function returnQuery() {
$sql = "SELECT USERCODE FROM tbl_users where USERNAME =? AND PASSWORD = ?";
$data = array(
'USERNAME' => $this->input->post('USERNAME'),
'PASSWORD' => $this->input->post('PASSWORD')
);
$query = $this->db->query($sql, $data);
}
You can return any function's value just like core PHP in CodeIgniter.
Modify line:
$query = $this->db->query($sql, $data);
To:
return $this->db->query($sql, $data);
Notice that I have removed $query assignment as it is not used anywhere.
We can return data without it.
After reading your comments you can return data as:
public function returnQuery()
{
$sql = "SELECT USERCODE FROM tbl_users where USERNAME =? AND PASSWORD = ?";
$data = array( 'USERNAME' => $this->input->post('USERNAME'), 'PASSWORD' => $this->input->post('PASSWORD') );
$query = $this->db->query($sql, $data);
$result = $query->result_array();
if(count($result) > 0){
return $result[0]["USERCODE"];
}
else{
return 0;
}
}
And now how can you use redirection:
//call function
$q = $this->login_model->returnQuery();
if ($q == 1){
redirect('login/adminIndex');
}
elseif ($q == 2) {
redirect('login/userIndex');
}
else{
redirect('login/restricted');
}
Note that in your code you are not comparing the values you are assigning
if ($q = 1) this should be if ($q == 1)
Hi All I'm new to Codeigniter, I'm trying to get all data relating to the account_id passed from a previous page.
I'm passing the account_id but not passing the name field associated with the account_id. The name field is blank.
I'm getting an error:
Here is my code for the controller:
function input($account_id = '', $name = ''){
if((int)$account_id > 0){
$query = $this->db->select('name', $name);
$query = $this->db->get_where('db_accounts', array('account_id' => $account_id));
$data['account'] = $query;
$data['fname']['value'] = $name;
$data['faccount_id']['value'] = $account_id;
$data['name'] = '';
$data['account_id'] = '';
}
$this->load->view('manage/input',$data);
}
Here is my input view form:
<?php
$data = array(
'name' => $fname['value'],
'account_id' => $faccount_id['value']
);
echo '<form action="/manage/edit" method="post" accept-charset="utf-8">';
echo form_hidden($data);
echo $account_id .' Account ID'.
form_input($faccount_id);
echo $name .' Name'.
form_input($fname);
$data = array('name' => 'submit', 'value' => 'Update Account', 'class' => 'submit');
echo form_submit($data);
?>
<?php echo form_close(); ?>
i believe get_where just preps your query
$query->row_array() should return your result as an array
$query = $this->db->get_where('db_accounts', array('account_id' => $account_id));
$result = $query->row_array();
For the second part of your question it looks like there is a lot going on. What is the value of $name in your input function? Are you actually passing a value to input? Make sure that name is set in your input function or else it will just be an empty string.
Occupied from documentation:
$this->db->select() accepts an optional second parameter. If you set
it to FALSE, CodeIgniter will not try to protect your field or table
names with backticks. This is useful if you need a compound select
statement.
So replace
$query = $this->db->select('name', $name);
with
$this->db->select('name', $name); // No need to assign it to a variable
Then $this->db->get_where(); executes the query and return the entire query object you need to fetch the result from it.
$query = $this->db->get_where('db_accounts', array('account_id' => $account_id));
$result = $query->row_array(); //For single row
$result = $query->result_array(); //For more than one row
Found the solution with the help of djjjuk.
$query = $this->db->get_where('db_accounts', array('account_id' => $account_id));
if ($query->num_rows() > 0)
{
$row = $query->row_array();
}
$data['account'] = $row;
$data['fname']['value'] = $row['name'];
$data['faccount_id']['value'] = $account_id;
$data['name'] = '';
$data['account_id'] = '';
}
$this->load->view('manage/input',$data);
}