I have one table named as user
USER(Table)
user_id name status_privilege(0=child,1=parent) added_user_id
2 abc 1
13 child_1 0 2
14 child_2 0 2
I want display user_id = 2(parent's) data in all user_id = 14,15 (all child's login). And also I want to display user_id = 14 , 15's data in user_id = 2's data. (Viceversa)
So for that what to do ?
I tried this query.
SELECT *,child.user_id as child_user_id,parent.user_id as parent_user_id
FROM user as parent
INNER JOIN user as child ON parent.user_id = child.user_added_id
WHERE child.user_id = '14'
so I got child_user_id = 14 & parent_user_id = 2
But I want that when I put below query it give me blank result. (changed child.user_id = '2')
SELECT *,child.user_id as child_user_id,parent.user_id as parent_user_id
FROM user as parent
INNER JOIN user as child ON parent.user_id = child.user_added_id
WHERE child.user_id = '2'
So I want is that when I fire query single query then if user is parent than also I want it's child's all data in user_id ='2' means data of 14 & 15 user's. And 2nd user_id's data in all childs.
Note : All the data in other tables are from user_id so is it possible ?
It's a requirement of existing project so I can not change database structure otherwise I can follow MySQL hierarchical of parent child structure.
the answer is based on what i understand from the question is:
you want the full array of child user ids if parent user logged in and only one parent id if child user logged in.
so in php you can derived that via
<?php
$user=array(); // this array will contains the user info
$res=$db->query("select * from user where user_id='$id'"); //$id the user id with which you want to check whether it is parent or child.
if($res->num_rows > 0)
{
$row=$res->fetch_assoc();
if($row['status_privilege']==0)
{
$res_main=$db->query("SELECT *,child.user_id as child_user_id,parent.user_id as parent_user_id FROM user as parent INNER JOIN user as child ON parent.user_id = child.user_added_id WHERE child.user_id = '".$row['user_id']."'");
$user=$res_main->fetch_assoc();
}
else
{
$res_main=$db->query("SELECT * from user where added_user_id='".$row['user_id']."'");
$user=$row;
$user["child_array"]=array();
while($row_child=$res_main->fetch_assoc())
{
array_push($user['child_array'],$row_child['user_id']);
}
}
}
//you can further check that if user is parent or child by using this condition.
if($user['status_privilege']==1)
{
//parent login it also contain the all child user ids
}
else
{
//child login it also contain the parent id as you have done it before.
}
?>
Related
I have a table with users and one with labels
A label can have many users and a user can have many labels, so a Many to Many relationship
A joining table is needed, that's why I have label_user
Below you can see pictures of what they contain with example data:
Users:
https://i.stack.imgur.com/E5E6O.png
Labels:
https://i.stack.imgur.com/1NFjq.png
label_user:
https://i.stack.imgur.com/tW2Uo.png
Let's say I have 5000 users and I can sort them by gender. Let's say 2800 of them are males, how can I assign them all to a label?
Here's some things I tried:
public function add_users_to_label($label_id, $condition, $value)
{
$db = new Database();
$conn = $db->db_connect();
$label_id = escape_string($conn, $label_id);
$query = $conn->query("INSERT INTO `label_user`(`label_id`, `user_id`) SELECT :label_id, psid FROM `iris_messenger_users` WHERE $condition = $value");
$query->bind_param("iss", $label_id, $condition, $value);
if ($query->execute()) {
return true;
}
else {
return "Error inserting data: " . $conn->error . "\n";
}
}
On the user side I have a simple form with select that let's you select a label and then this code:
if(isset($_POST['label-select'])) {
if ($_GET['show_only_gender'] == 'male') {
$condition = 'gender';
$user->add_users_to_label($_POST['label-select'], $condition, $_GET['show_only_gender']);
}
}
Basically, I want to get all users that are male and assign them to a label and put that into label_user with respectively the label_id and the user_id(psid)
Even if this worked I'd still have to do it 2699 times more. What can I do here to optimize and make it to run with 1 query if possible?
I don't think using foreach and running it as much times as there are users is the best option, is it?
Is there any better approach I can take to make this possible?
Although what you are describing does not make sense to have a "label" associated with a person for this specific component, the gender is already on the user table you should be able to get all male based on
select * from user where gender = 'male'
no need to JOIN to a label table on this field. Similarly if you were trying to find people based on a name starting with something... you would not create a label for the name either. Query directly from the table that has that specific component association.
Now, to answer your question, how to insert into the label table for each instance in bulk, you could do something like... I am doing this based on some label ID = 123 as just an example in your labels table that represents gender.
I am doing a LEFT-JOIN in the select so we dont try to add for any user IDs that are already on file do not try to get re-added.
insert into label_user
( label_id,
user_id )
select
123 as label_id,
U.id as user_id
from
users U
left join label_user LU
on U.id = LU.user_id
AND LU.label_id = 123
where
U.gender = 'male'
AND LU.user_id IS NULL
You obviously need to adjust for php.
$queryread = "SELECT Properties.ID, Properties.Name,
Properties.Description, County.County
FROM Properties
INNER JOIN County ON Properties.County_ID = County.ID
AND Properties.Ban = 0";
This is the query to display properties on the public website. When an owner is signed in how would i display only the properties that the owner owns.
Owner table is called "RegisteredOwners" and id is "id", there is also a "Owner_ID" in the Properties table.
Thanks
You will simply need to add a WHERE clause to your query. To keep the answer simple and contained, I'll just use a subquery for the where clause. The query below is identical to yours, with an added line for the where clause.
SELECT Properties.ID, Properties.Name, Properties.Description, County.County
FROM Properties INNER JOIN County ON (Properties.County_ID = County.ID AND Properties.Ban = 0)
WHERE Properties.Owner_ID = (SELECT id FROM RegisteredOwners WHERE Name=?)
You will need to replace the Name=? portion with the appropriate method of identifying the user's RegisteredOwners row.
i have a table named request where i store name, last name,user_id, category amount and points. its a money request i made so amount can vary all the time.
and i have a table named user. that stores user id, category, name, last name
what i want to accomplish is create an event that triggers everyday at 23:00 and search for all user based on there category and update the table user if it finds any user category with he following condition.
category id 1,2,3 all user at the begin are 1, if user category is 1 and points are = 4000 then update table user by changing user category to 2 example
<?php
$points = data->info_from_db;
$user_category = data->info_from_db;
if ($user_category == 1 && $points == 400)
{
///update users set category ='2'
}
else {
echo "no update today";
}
?>
this can be done by php? all i need is to first get all sum(points) of each user and their category and update if the condition is true.
I have a posts table like this:
post_id user_id title date
12 1 abc 7/20/2014
13 1 cde 7/21/2014
14 2 fgh 7/22/2014
And a users table like this:
user_id username email password
1 name1 email1#domain.com ******
2 name2 email2#domain.com ******
The user_id in the posts table is the foreign key of the users table.
Note:
Assume that I store the user's session successfully by using $_SESSION['user_id'] .
Assume that I can echo all 3 post titles along with their usernames successfully too.
Now I would like to echo the edit hyperlink (Edit) for the post titles of a certain user, for example, user_id 1 or 2 after he's logged in. It means that if the user named name1 signs into his account and browses the post whose id is 14, he cannot see the edit hyperlink because it belongs to the another user named name2, whose session id, if any, should be unableable now, and vice versa.
For me, this is my code:
$q = "SELECT title, post_id, p.user_id AS currentuser
FROM posts AS p
INNER JOIN users AS u USING (user_id)
ORDER BY date ASC
";
$r = mysqli_query ($dbc, $q);
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$uid = isset($_SESSION['user_id']);// variable for the seesion
$current_user = $row['currentuser'];// variable for the certain user ID
if($uid && $uid == $current_user){
echo " Edit </div>";
}
Then I test it, you know, it shows the edit hyperlink in every post title, but not what i expected.
Can you help? Thanks
$uid = isset($_SESSION['user_id']);
This results to either true or false. try
$uid = isset($_SESSION['user_id']) ? $_SESSION['user_id'] : -1;
What I did was use a ternary operator so if the session variable is set, you set it to it otherwise you set it to -1, which most probably does not exsist in a table id column
I have a table
id,name,parent_id,designation columns,
i want create tree through recursive function in php.
every parent_id is looking in id column and if user login then user can see own and all below records according parent_id.
like
A
|
B
|
c
|
D
|
E
|
F
if A user login then he can all(A,B,C,D,E,F) details.and if B login then see (B,c,D,E,F) and like all... if F login then he can see only own records..
Thanks for advance
create a function fetch_parent;
function fetch_parent($parent_id) {
$query = 'SELECT * FROM `my_table` WHERE `parent_id`='. $parent_id;
// use your own sql class/function whatever to retrieve the record and store it in variable $parent
if($parent->parent_id !== null) { // asuming a 'root' record will have null as it's parent id
fetch_parent($parent->parent_id); // here you go with your recursion
}
return;
}
Then just call the function with the record you want it's parents from:
$first_parent_id = 8;
fetch_parent($first_parent_id);
Notes:
the $parent var can also be an array, depending on the mysql result set
PLEASE PLEASE PLEASE check $parent_id in the query for mysql injection etc.