Send notification to multiple users function - php

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

Related

retrieve session value and connect to different db in logout function

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');

Codeigniter : update records if records exists there or then insert new records

In my Table i have four columns .
r_id
id (User id)
v_id (company id)
rate
All i am doing is to rate the company (v_id) from user.
Suppose, if user one is rate the 1st Company (v_id),Again when same user rate the same Company Then rate column automatically updated. if user one wants to rate another company then a new row will be added in the table. But in my case
when same user again rate the company then new row is inserted in the table.
Model
I don't know where i am doing wrong in this Model.
function ratings($insert,$id,$update,$v_id) {
// $this->db->where('id',$id);
// $this->db->where('v_id',$v_id);
// $run = $this->db->get('user_ratings');
// if ($run->num_rows() >= 1) {
// $this->db->where('id',$id);
// $this->db->update('user_ratings',$update);
// }else {
// // $this->db->set('user_id', $id);
// $this->db->insert('user_ratings',$insert);
// }
$query = $this->db->query('select id, v_id from user_ratings where id = "'.$id.'" and v_id = "'.$v_id.'"')->num_rows();
if ($query > 0) {
$this->db->query('update user_ratings set rate ="'.$update.'" where id = "'.$id.'"');
}else {
$this->db->insert('user_ratings',$insert);
}
}
Controller
function ratings() {
$id = $this->session->userdata('id');
$v_id = $this->uri->segment(3);
$insert= array (
'rate' => $this->input->post('click_val'),
'date' => date('Y-m-d H:i:s'),
'v_id' => $this->input->post('company_id'),
'id' => $this->input->post('id')
);
$update = array (
'rate' => $this->input->post('click_val'),
'date' => date('Y-m-d H:i:s'),
);
$this->Visa_mdl->ratings($value,$id,$update,$v_id);
}
Your on the right track. These simple changes should do the job.
Model
function ratings($data, $id, $v_id)
{
$query = $this->db->query("select r_id from user_ratings where id = $id and v_id = $v_id");
if($query->num_rows() > 0)
{
$data['r_id'] = $query->row()->r_id;
return $this->db
->where('id', $id)
->where('v_id', $v_id)
->update('user_ratings', $data);
}
else
{
return $this->db->insert('user_ratings', $data);
}
}
The method returns true or false to indicate the outcome.
Your controller modified to work with the above
function ratings()
{
$id = $this->session->userdata('id');
$v_id = $this->input->post('company_id');
$data = array(
'rate' => $this->input->post('click_val'),
'date' => date('Y-m-d H:i:s'),
'v_id' => $v_id,
'id' => $id
);
$this->Visa_mdl->ratings($data, $id, $v_id);
}
You have putted static value of v_id column try by replacing v_id ="2" with v_id = "'.$v_id.'" hope this will help you.
<?php
function ratings($insert,$id,$update,$v_id) {
$query = $this->db->query('select id, v_id from user_ratings where id = "'.$id.'" and v_id = "'.$v_id.'"')->num_rows();
if ($query > 0) {
$this->db->query('update user_ratings set rate ="'.$update.'" where id = "'.$id.'"');
}else {
$this->db->insert('user_ratings',$insert);
}
}

Get the newest data from sql but not working

I would like to get the newest comments in following code, but now only showing the oldest 50 comments, how can I edit code to showing the newest 50 comments? thanks so much
Code here:
<?php
class comments extends db_connect
{
private $requestFrom = 0;
private $language = 'en';
public function __construct($dbo = NULL)
{
parent::__construct($dbo);
}
public function allCommentsCount()
{
$stmt = $this->db->prepare("SELECT max(id) FROM comments");
$stmt->execute();
return $number_of_rows = $stmt->fetchColumn();
}
public function count($postId)
{
$stmt = $this->db->prepare("SELECT count(*) FROM comments WHERE postId = (:postId) AND removeAt = 0");
$stmt->bindParam(":postId", $postId, PDO::PARAM_INT);
$stmt->execute();
return $number_of_rows = $stmt->fetchColumn();
}
public function create($postId, $text, $notifyId = 0)
{
$result = array("error" => true,
"error_code" => ERROR_UNKNOWN);
if (strlen($text) == 0) {
return $result;
}
$post = new post($this->db);
$postInfo = $post->info($postId);
unset($post);
$currentTime = time();
$ip_addr = helper::ip_addr();
$u_agent = helper::u_agent();
$stmt = $this->db->prepare("INSERT INTO comments (fromUserId, postId, comment, createAt, notifyId, ip_addr, u_agent) value (:fromUserId, :postId, :comment, :createAt, :notifyId, :ip_addr, :u_agent)");
$stmt->bindParam(":fromUserId", $this->requestFrom, PDO::PARAM_INT);
$stmt->bindParam(":postId", $postId, PDO::PARAM_INT);
$stmt->bindParam(":comment", $text, PDO::PARAM_STR);
$stmt->bindParam(":createAt", $currentTime, PDO::PARAM_INT);
$stmt->bindParam(":notifyId", $notifyId, PDO::PARAM_INT);
$stmt->bindParam(":ip_addr", $ip_addr, PDO::PARAM_STR);
$stmt->bindParam(":u_agent", $u_agent, PDO::PARAM_STR);
if ($stmt->execute()) {
$result = array("error" => false,
"error_code" => ERROR_SUCCESS,
"commentId" => $this->db->lastInsertId(),
"comment" => $this->info($this->db->lastInsertId()));
if ($this->requestFrom != $postInfo['fromUserId']) {
$gcm = new gcm($this->db, $postInfo['fromUserId']);
$gcm->setData(GCM_NOTIFY_COMMENT, "You have a new comment.", $postId);
$gcm->send();
}
}
return $result;
}
public function remove($commentId)
{
$result = array("error" => true,
"error_code" => ERROR_UNKNOWN);
$commentInfo = $this->info($commentId);
if ($commentInfo['error'] === true) {
return $result;
}
// if ($commentInfo['fromUserId'] != $this->requestFrom) {
//
// return $result;
// }
$currentTime = time();
$stmt = $this->db->prepare("UPDATE comments SET removeAt = (:removeAt) WHERE id = (:commentId)");
$stmt->bindParam(":commentId", $commentId, PDO::PARAM_INT);
$stmt->bindParam(":removeAt", $currentTime, PDO::PARAM_INT);
if ($stmt->execute()) {
$result = array("error" => false,
"error_code" => ERROR_SUCCESS);
}
return $result;
}
public function removeAll($postId) {
$currentTime = time();
$stmt = $this->db->prepare("UPDATE comments SET removeAt = (:removeAt) WHERE postId = (:postId)");
$stmt->bindParam(":postId", $postId, PDO::PARAM_INT);
$stmt->bindParam(":removeAt", $currentTime, PDO::PARAM_INT);
}
public function info($commentId)
{
$result = array("error" => true,
"error_code" => ERROR_UNKNOWN);
$stmt = $this->db->prepare("SELECT * FROM comments WHERE id = (:commentId) LIMIT 1");
$stmt->bindParam(":commentId", $commentId, PDO::PARAM_INT);
if ($stmt->execute()) {
if ($stmt->rowCount() > 0) {
$row = $stmt->fetch();
$time = new language($this->db, $this->language);
$profile = new profile($this->db, $row['fromUserId']);
$fromUserId = $profile->get();
unset($profile);
$lowPhotoUrl = "/img/profile_default_photo.png";
if (strlen($fromUserId['lowPhotoUrl']) != 0) {
$lowPhotoUrl = $fromUserId['lowPhotoUrl'];
}
$post = new post($this->db);
$post->setRequestFrom($this->getRequestFrom());
$postInfo = $post->info($row['postId']);
$result = array("error" => false,
"error_code" => ERROR_SUCCESS,
"id" => $row['id'],
"fromUserId" => $row['fromUserId'],
"fromUserState" => $fromUserId['state'],
"fromUserUsername" => $fromUserId['username'],
"fromUserFullname" => $fromUserId['fullname'],
"fromUserPhotoUrl" => $lowPhotoUrl,
"postId" => $row['postId'],
"postFromUserId" => $postInfo['fromUserId'],
"comment" => htmlspecialchars_decode(stripslashes($row['comment'])),
"createAt" => $row['createAt'],
"notifyId" => $row['notifyId'],
"timeAgo" => $time->timeAgo($row['createAt']));
}
}
return $result;
}
public function get($postId, $commentId = 0)
{
if ($commentId == 0) {
$commentId = $this->allCommentsCount() + 1;
}
$comments = array("error" => false,
"error_code" => ERROR_SUCCESS,
"commentId" => $commentId,
"postId" => $postId,
"comments" => array());
$stmt = $this->db->prepare("SELECT id FROM comments WHERE postId = (:postId) AND id < (:commentId) AND removeAt = 0 ORDER BY id ASC LIMIT 0,38");
$stmt->bindParam(':postId', $postId, PDO::PARAM_INT);
$stmt->bindParam(':commentId', $commentId, PDO::PARAM_INT);
if ($stmt->execute()) {
while ($row = $stmt->fetch()) {
$commentInfo = $this->info($row['id']);
array_push($comments['comments'], $commentInfo);
$comments['commentId'] = $commentInfo['id'];
unset($commentInfo);
}
}
return $comments;
}
public function getPreview($postId)
{
$commentId = $this->allCommentsCount() + 1;
$comments = array("error" => false,
"error_code" => ERROR_SUCCESS,
"commentId" => $commentId,
"postId" => $postId,
"count" => $this->count($postId),
"comments" => array());
$stmt = $this->db->prepare("SELECT id FROM comments WHERE postId = (:postId) AND id < (:commentId) AND removeAt = 0 ORDER BY id ASC LIMIT 3");
$stmt->bindParam(':postId', $postId, PDO::PARAM_INT);
$stmt->bindParam(':commentId', $commentId, PDO::PARAM_INT);
if ($stmt->execute()) {
while ($row = $stmt->fetch()) {
$commentInfo = $this->info($row['id']);
array_push($comments['comments'], $commentInfo);
$comments['commentId'] = $commentInfo['id'];
unset($commentInfo);
}
}
return $comments;
}
public function setLanguage($language)
{
$this->language = $language;
}
public function getLanguage()
{
return $this->language;
}
public function setRequestFrom($requestFrom)
{
$this->requestFrom = $requestFrom;
}
public function getRequestFrom()
{
return $this->requestFrom;
}
}
Try to edit your LIMIT from LIMIT 0,38 to LIMIT 38.

select Name from database where emali = $emali?

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

PHP else statement not executing

My else statement is not getting executed when it should. There are no errors in the code. It simply has to do with the flow of my query and foreach statement but I cannot seem to figure this out. Any ideas?
$id = $this->input->post('id');
$session_id = $this->input->post('session_id');
$q1 = $this->db->query("SELECT *
FROM default_cart
WHERE cookie_id = '$session_id'
AND product_id = '$id' LIMIT 1");
foreach($q1->result() as $row) {
if($row->cookie_id != $session_id && $row->product_id != $id) {
echo json_encode(array('error_code' => 'e100'));
} else {
$data = array('product_id' => $id,
'active' => 'Yes',
'cookie_id' => $session_id
);
$this->db->insert('default_cart', $data);
echo json_encode(array('success' => true));
}
}
First, I would like to suggest that there is no sense of looping through the query when there is only one record LIMIT 1 and no sense of if check while you have already checked the all parameters in the query WHERE cookie_id = '$session_id' AND product_id = '$id' just fetch the row returned by the query and check for empty or not.
$id = $this->input->post('id');
$session_id = $this->input->post('session_id');
$q1 = $this->db->query("SELECT * FROM default_cart
WHERE cookie_id = '$session_id' AND product_id = '$id' LIMIT 1");
$row=$q1->row();
if(!empty($row)) {
$data = array('product_id' => $id,
'active' => 'Yes',
'cookie_id' => $session_id);
$this->db->insert('default_cart', $data);
echo json_encode(array('success' => true));
}else{
echo json_encode(array('error_code' => 'e100'));
}

Categories