How to join two table id and validate a particular field? - sql

Sorry for the fuorviant title but I'll trying to explain what I'm try to do. So, I've two table ea_users and ea_user_settings.
ea_users - STRUCTURE
id | data|
5 | 0
ea_user_settings - STRUCTURE
|id | username|
5 Sandokan
Now what I'm trying to do is validate if certain username already exists in the system. The table ea_users contain the id, that rapresents the id of a single user, and the data field (0 user not removed, also 1 user deleted).
In the ea_user_settings table I've all settings of my users. Now a possible idea for check if a certain username already exist is this:
public function validate_username($username, $user_id)
{
$num_rows = $this->db->get_where('ea_user_settings',
array('username' => $username, 'id_users <> ' => $user_id))->num_rows();
return ($num_rows > 0) ? FALSE : TRUE;
}
This function have two parameters: username and user_id, the username that's going to be inserted and the user_id (if this is valorized means that I'm actually stay update the user so I don't need to check on it row if the username already exists).
Actually all working good but I want to check if the username exists for only the user with the data equal to 0, if the data is 1 the username is available 'cause it's deleted. I don't know if the situation is more clear. How I can achieve that in a single query?

You can try something like this
public function validate_username($username, $user_id)
{
$num_rows =
$this->db->select('*')
->from('ea_user_settings')
->join('ea_users', 'ea_user_settings.id_users = ea_users.id', 'left')
->where(array('ea_user_settings.username' => $username, 'ea_user_settings.id_users <> ' => $user_id, 'ea_users.data' => 1))
->get();
return ($num_rows->num_rows() > 0) ? 'We have this username and it was deleted!' : 'This username was not deleted';
}

Related

how only display other user ID on the two columns user 1 and user 2 after filtering authenticated user ID from the table and adding it to a new column

I'm creating a one-on-one chat, but there are two columns: user 1 and user 2.
The user may be in user 1 or user 2, and I want to show him or her the chats from people who have contacted him or him, but the table returned as
and I just want to retrieve the other user id not the authenticated user, who is 51
so l want to select either user 1 or user 2 which has the other user id.
the code I implemented
$room = Chat::where([
['user_1',Auth::id()]
])->orWhere([
['user_2',Auth::id()]
])->latest()->get();
// dd($room);
return view('users.chats',[
'room_id'=>$room,
// 'messages'=>Message::oldest()->where('chat_id',$room->chat_id)->get(),
'selectedUser' => false
]);
How can I create a new column called "other user" and only retrieve the other user's ID?
Running it through php on array filter fixed the problem.
$room = Chat::where([
['user_1',Auth::id()]
])->orWhere([
['user_2',Auth::id()]
])->latest()->get();
$users = array();
foreach($room as $item){
array_push($users,$item->user_1,$item->user_2);
}
$fillterdUsers = array_filter($users,function($val){
return $val !== auth()->id();
});
return view('users.chats',[
'room_id'=>$fillterdUsers,
// 'messages'=>Message::oldest()->where('chat_id',$room->chat_id)->get(),
'selectedUser' => false
]);

Get user data from database codeignter

I have two tables in one database, table one called: ci_admin, and table two called active_ingredient.
I want to get user data from the active_ingredient table where user_id equals admin_id from ci_admin table using the current user session.
this code, get data for all user, I need the data inserted by the user only:
$wh =array();
$SQL ='SELECT * FROM active_ingredient';
$wh[] = "SELECT 1
FROM ci_admin
WHERE ci_admin.admin_id = active_ingredient.user_id";
if(count($wh)>0)
{ $WHERE = implode(' and ',$wh);
return $this->datatable->LoadJson($SQL,$WHERE);
}
else
{
return $this->datatable->LoadJson($SQL);
}
Try this, it will help you.
$this->db->select('ai.*,ca.*');
$this->db->from('active_ingredient ai');
$this->db->join('ci_admin ca', 'ca.admin_id = ai.user_id', 'left');
$this->db->where($data); //pass the session data for exact filter
$query = $this->db->get();
return $query->result();

Codeigniter populate nested associations

this is the results i need from the database
{comment: 'hello there', user: [{name: 'sahan', id: 1}], id: 2}
function used to get comments
public function get_comments(){
$query = $this->db->get('comments');
return $query->result_array();
}
I have a comments table and a users table, When a user comments on something the
comment is saved as follows
comment > Comment text
user: userid
So when the data is shown I need codeigniter to populate the user field with the user data found from the users table
Does anyone know how to do this ?
I used this functionality in SailsJS but dont know how to do it here in CodeIG
Sails.js populate nested associations
Codeigniter 's active record is not as advanced as SailJS active record, but you can achieve what you are asking for with two queries within the same method.
I'm assuming the comments table has a foreign key to the users table through a user_id field.
public function get_comments() {
/* Get all the comments */
$query = $this->db->get('comments');
$comments = $query->result_array();
/* Loop through each comment, pulling the associated user from the db */
foreach $comments as &$comment {
$query = $this->db->get_where('users', array('id' => $comment['user_id']));
$user = $query->result_array();
/* Put the user's data in a key called 'user' within the comment array */
$comment['user'] = $user;
/* Remove the unnecessary user_id and $user variable */
unset($comment['user_id']);
unset($user);
}
return $comments;
}

how to update custom profile field data in buddypress

I want to update custom profile field data in buddypress. I've been searching on google but still no luck. is there anyway to update it with a custom code?
You can use the default buddypress function
xprofile_set_field_data( $field, $user_id, $value, $is_required = false )
$fields = field id
$user_id = current login user id
$value = inserted value
$is_required = boolean
Here you have passed the specific insert fields data.
OR
You can insert your custom field data in usermeta table.
Like this:
update_user_meta($user_id, '$key', $_POST['post_value']);
$key = unique name of the fields

Get data from another column where first column id is equals other and other column equals X

My problem is:
I would like to get all advertistments where id of Description column is equals with Advertistment's column.
Let's say that Advertistment column is connected with Description column.
I would like to gain all description's id where one of its column called type_of_house is equals m.
Then show all advertistment where advertistment's id is equals with description's id.
In short way: advertistment shows info about houses, descriptions store houses type D and M and I want show all advertistments with houses type of M.
This is correct sql:
SELECT * FROM advertistment, description WHERE advertistment.id_advertistment = description.id_description AND description.type_of_house = "m"
I have no idea how write it into zend. I tried something like that. This function I wrote in model folder.
public function takeAll() {
$select = $this->_db->select();
$select->from(array('a' => 'advertistment', 'd' => 'description'));
$select->where('a.id_advertistment = d.id_description AND d.type_of_house = m');
return $select->query()->fetchAll();
}
You're actually quite close. This should work:
public function takeAll() {
$select = $this->_db->select();
$select->from(array('a' => 'advertistment'));
$select->join(array('d' => 'description'), 'a.id_advertistment = d.id_description');
$select->where('d.type_of_house = ?', 'm');
return $this->_db->fetchAll($select);
}