Get a specific list of users excluding few users - php

I have 2 table users and user_request
user
id name
1 U1
2 U2
3 U3
4 U4
5 U5
user_request
id userid name status
1 1 U1 rejected
2 3 U3 rejected
I need list of users from user table but those users should not be there which are present in user_request table
So according to above example i need the following list of users
U2
U4
U5
From the user table i am using the following code to fetch the result
$query = $this->db->get('user');
return $query->result();
From the user_request table i am using the following code to fetch the result
$this->db->where('status','rejected');
$query = $this->db->get('user_request');
return $query->result();
Can anyone please help me to get the desired result?

Use a simple query using NOT IN:
SELECT name FROM user WHERE id NOT IN (SELECT userid FROM user_request)
SQLFIDDLE
UPDATE: For Codeigniter:
$this->db->select('*')->from('user');
$this->db->where('`id` NOT IN (SELECT `userid` FROM `user_request`)', NULL, FALSE);

You can do a join:
$this->db->select('name');
$this->db->from('user');
$this->db->join('user_request', 'user.id = user_request.userid');
$this->db->where('status !=','rejected');
return $this->db->get();

Fetch the id from first table, fetch the userid from second table. remove the common values from the 2 arrays and you will get the desired result
$this->db->select('id');
$query = $this->db->get('user');
$first = $query->result();
foreach ($first as $key)
{
$user_one = $key->id;
$first_array[] = $user_one;
}
$this->db->where('status','rejected');
$query_two = $this->db->get('user_request');
$two = $query_two->result();
foreach ($two as $value)
{
$userid_two = $value->userid;
$second_array[] = $userid_two;
}
$arr_1 = array_diff($first_array, $second_array);
$std = array();
foreach ($arr_1 as $value)
{
$this->db->where('id',$value);
$main_query = $this->db->get('user');
$main = $main_query->result();
$std = array_merge($std,$main);
}
return $std;

Related

SELECT a value multiple time in SQL

I have a code in PHP where I want to display multiple times values, and so, even if these values are the same between them. My code is simple :
$sql = "SELECT photo from table WHERE username IN ('1','2','2') ORDER BY id DESC ";
$res = array();
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result)){
array_push($res, $row['photo']);
}
echo json_encode($res);
But this code only display (in json) an array of two values (because the values of photo of the username 2 are the same).
What I want to achieve is to make an array with the exact same number of values of the number of username I defined WHERE username IN ('1','2','2') (so here, 3 values).
I hope you understood me, thanks for helping me !
I think what you're after is to list even the duplicates in the end result. As your SQL will only retrieve the unique items, the idea would be to include the username in the SQL result set. Then use the original list of user names ($userNames) and add in the photo for each of them.
I've used mysqli_fetch_all() to simplify the process of fetching all of the data, then used array_column() to make the username the key for the photos.
$userNames = array(1,2,2);
$sql = "SELECT username, photo
from table
WHERE username IN ('".implode("','", $userNames)."')
ORDER BY id DESC ";
$res = array();
$result = mysqli_query($con,$sql);
$photos = mysqli_fetch_all($result, MYSQLI_ASSOC);
$photos = array_column($photos, "photo", "username");
foreach ( $userNames as $user ) {
if ( isset($photos[$user])) {
$res[] = $photos[$user];
}
else {
$res[] = '';
}
}
echo json_encode($res);
You would use left join:
select t.photo
from (select '1' as username union all select '2' union all select '3'
) u left join
table t
on t.username = u.username
order by t.id desc;
Note this will return rows, even when the user name does not exist. If you want to filter those rows, remove the left so you are doing an inner join.

PHP - user membership list using mysql two tables

I want to make a membership list to show from two tables.
I want to join the two tables on there id's
database table - user
,
database table - user_data
Currently I'm getting only the id=1 from user table with user data as well.
I know I want to loop it to see all the records from the user table but I don't how too. I am new to coding.
{
$result = mysql_query("SELECT * FROM user WHERE user_active='Yes' AND user_id!=0 ") or die(mysql_error());
$row = mysql_fetch_array($result,MYSQL_ASSOC);
foreach ($row as $key => $value) {
$output[$key] = $value;
}
$user_id=$output['user_id'];
// now we get the user information and add it to the $output array.
$result = mysql_query("SELECT data_type,data_value FROM user_data WHERE data_user_id=$user_id ") or die(mysql_error());
while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
$output['user_data'][$row['data_type']] = $row['data_value'];
}
print_r($output);
}
Only one request is needed in this context you have to rewrite your SQL call.
SELECT u.user_id, u.user_active, ud.data_type, ud.data_value FROM user u
JOIN user_data ud
ON u.user_id = ud.user_id
WHERE u.user_active='Yes' AND data_user_id!=0
I JOIN the 2 tables with ALIAS tables u and ud (user,user_data) with the same id in your context user_id and data_user_id :
// now we get the user information and add it to the $output array.
$result = mysql_query("SELECT u.user_id, u.user_active, ud.data_type, ud.data_value FROM user u
JOIN user_data ud
ON u.user_id = ud.user_id
WHERE u.user_active='Yes' AND data_user_id!=0") or die(mysql_error());
while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
$output['user_data'][$row['data_type']] = $row['data_value'];
}
print_r($output);
Regards.

Mysql get all rows but join if possible [duplicate]

I have 3 mysql tables.
Table 1 user
id | name
Table 2 emails
id | email
Table 3 user_email
user_id | email_id
I have no experience in query multi tables.
Using codeigniter active record, i want to find out the user email address based on the user id, pls advise if the below code is correct ?
$CI->db->select('email');
$CI->db->from('emails');
$CI->db->where('id', $userid);
$CI->db->join('user_email', 'user_email.user_id = emails.id', 'left');
$query = $CI->db->get();
You have wrong where clause you need to compare user_id from your table ,you are comparing the id of email to the provided $user_id
$CI->db->select('email');
$CI->db->from('emails');
$CI->db->where('user_id', $userid);
$CI->db->join('user_email', 'user_email.user_id = emails.id', 'left');
$query = $CI->db->get();
A more useful way is to give aliases to your tables so the tables with same columns will not have any confusion
$CI->db->select('e.email');
$CI->db->from('emails e');
$CI->db->join('user_email ue', 'ue.user_id = e.id', 'left');
$CI->db->where('ue.user_id', $userid);
$query = $CI->db->get();
#m khalid 's answer is correct but I have created a dynamic function to achieve join with multiple tables. Check this out.
function join_records($table, $joins, $where = false, $select = '*', $orderBy = false, $direction = 'DESC'){
$CI->select($select);
$CI->from($table);
foreach($joins as $join){
$CI->join($join[0], $join[1], $join[2]);
}
if($where) $CI->where($where);
if($orderBy) $CI->order_by($orderBy, $direction);
$query = $CI->get();
return $query->result_array();
}
Applying your question to this.
$table = 'emails';
$joins[0][0] = 'user_email';
$joins[0][1] = 'user_email.user_id = emails.id';
$joins[0][2] = 'left';
$where['user_id'] = $userid;
You may add more join like $join1[0]..
If you need to select some specific column you can define in following manner
$select = 'table1.column1, table1.column2, table2.column1, table2.column2'
Or if you want all the columns put a *
$this->join_records($table, $joins, $where, $select);
You may also find it here.

When i use if else condition in query based on condition, how can i get that value on view in PHP CODEIGNITER

I am confuse with this problem in mysql, I have two table, "A" and "B"
TableA:
S.No contact1 contact2 status
1 Blbh eeee 1
TAbleB:
S.No Phone1 phone2
1 ddd ssss
From this table i am going to get value, from TableA ia m going to check
if (status == 1)
{
run tableA;
}
else
{
run table b;
}
I am gone a return result of this query. In view, how to get value with respected column name. I have no idea of this, help me to get value in view.
public function contDetails($id){
$check = $this->db->query("SELECT contact_status FROM account WHERE id = '$id' ");
$str = $check->row();
$chk = $str->contact_status;
if($chk == 1){
$query = $this->db->query("SELECT * FROM account WHERE id = '$id'");
}else{
$query = $this->db->query("SELECT * FROM contact_details WHERE user_id = '$id'");
}
$run = $query->num_rows();
print_r($run);
}
You can use in your model
$query = $this->db->get(); //--- run the query ---//
return $query->result() //--- to get result in array of object ---//
and then in your view use foreach loop
foreach($results as $result){
echo $result->columnName;
}
have you already written the query in mysql? If yes and if you are concerned about whether the column name to use exist or not you can use isset...
in your view you can use like the following:
<?php
foreach($results as $result)
{
echo (isset($result['col1']))?$result['col1']:$result['col1_2'];
}
?>
And don't forget to use result_array() instsead of result in the controller

codeigniter active record left join

I have 3 mysql tables.
Table 1 user
id | name
Table 2 emails
id | email
Table 3 user_email
user_id | email_id
I have no experience in query multi tables.
Using codeigniter active record, i want to find out the user email address based on the user id, pls advise if the below code is correct ?
$CI->db->select('email');
$CI->db->from('emails');
$CI->db->where('id', $userid);
$CI->db->join('user_email', 'user_email.user_id = emails.id', 'left');
$query = $CI->db->get();
You have wrong where clause you need to compare user_id from your table ,you are comparing the id of email to the provided $user_id
$CI->db->select('email');
$CI->db->from('emails');
$CI->db->where('user_id', $userid);
$CI->db->join('user_email', 'user_email.user_id = emails.id', 'left');
$query = $CI->db->get();
A more useful way is to give aliases to your tables so the tables with same columns will not have any confusion
$CI->db->select('e.email');
$CI->db->from('emails e');
$CI->db->join('user_email ue', 'ue.user_id = e.id', 'left');
$CI->db->where('ue.user_id', $userid);
$query = $CI->db->get();
#m khalid 's answer is correct but I have created a dynamic function to achieve join with multiple tables. Check this out.
function join_records($table, $joins, $where = false, $select = '*', $orderBy = false, $direction = 'DESC'){
$CI->select($select);
$CI->from($table);
foreach($joins as $join){
$CI->join($join[0], $join[1], $join[2]);
}
if($where) $CI->where($where);
if($orderBy) $CI->order_by($orderBy, $direction);
$query = $CI->get();
return $query->result_array();
}
Applying your question to this.
$table = 'emails';
$joins[0][0] = 'user_email';
$joins[0][1] = 'user_email.user_id = emails.id';
$joins[0][2] = 'left';
$where['user_id'] = $userid;
You may add more join like $join1[0]..
If you need to select some specific column you can define in following manner
$select = 'table1.column1, table1.column2, table2.column1, table2.column2'
Or if you want all the columns put a *
$this->join_records($table, $joins, $where, $select);
You may also find it here.

Categories