I dont know if I lost the information in my brain, or if I never had it, but I have created a tweet so a user can follow other users.
The problem now is that every user display the same information.
I Was trying to add WHERE $_SESSION['uid'] to my query but that doesn't help do the trick.
Still shows the same info.
Any tips ?
<?php
foreach(fetch_follotweets() as $tweet){
echo $tweet['firstname'];
echo $tweet['lastname'];
echo $tweet['username'];
echo $tweet['date'];
echo $tweet['profile_img'];
$tweet['message'];
}
function fetch_follotweets(){
global $db;
$query = $db->query(" SELECT
user.email,
user.username,
tweets.message,
tweets.date,
userdetails.profile_img,
userdetails.firstname,
userdetails.lastname,
following.id,
following.user_id,
following.follow_id
FROM user
JOIN userdetails
ON user.id = userdetails.user_id
JOIN tweets
ON userdetails.user_id = tweets.user_id
JOIN following
ON following.follow_id
WHERE following.follow_id = tweets.user_id AND
user.id='{$_SESSION['uid']}
ORDER BY tweets.date DESC");
$tweet = array();
while(($row = $query->fetch(PDO::FETCH_ASSOC)) !==FALSE) {
$tweet[] = $row;
}
return $tweet;
}
?>
Without the WHERE clause, the SQL would get the details of all the users
Also, I think that the JOIN on tweets & following should be a LEFT JOIN ( to get the records even if none of them is present )
SELECT
user.email,
user.username,
tweets.message,
tweets.date,
userdetails.profile_img,
userdetails.firstname,
userdetails.lastname,
t.ids_of_users_followed
FROM
user
JOIN userdetails
ON user.id = userdetails.user_id
LEFT JOIN (
SELECT
GROUP_CONCAT( following.follow_id ) AS ids_of_users_followed
following.user_id
FROM
following
GROUP BY
following.user_id
) as t ON ( t.user_id = user.id )
JOIN tweets
ON ( userdetails.user_id = tweets.user_id OR FIND_IN_SET( tweets.user_id, t.ids_of_users_followed ) )
WHERE
user.id = :? => Make sure to add the logged in user_id here
ORDER BY
tweets.date DESC
Related
I want to view data from mysql in codeigniter based on user who is logged in
but I got an error "A Database Error Occurred". I think I wrote the wrong code
t.USER = $this->session->userdata('user_id');
Here's the code:
function get_rekomen()
{
$query = $this->db->query("SELECT
c.*,
t.produk_id,
t.id_transdet,
t.kategori_id,
t.total_qty,
t.USER
FROM
transaksi_detail AS t
LEFT JOIN
(
SELECT
g.id_produk,
p.slug_produk,
p.foto,
p.foto_type,
p.harga_diskon,
p.diskon,
p.harga_normal,
p.judul_produk,
g.kat_id,
k.judul_kategori
FROM
(
SELECT
MAX(m.id_produk) AS id_produk,
m.kat_id
FROM
produk AS m
GROUP BY
m.kat_id
)
AS g
INNER JOIN
produk AS p
ON p.id_produk = g.id_produk
LEFT JOIN
kategori AS k
ON k.id_kategori = g.kat_id
)
AS c
ON c.kat_id = t.kategori_id
WHERE
t.USER = $ this -> session -> userdata('user_id');
<<// i think here's the problem
ORDER BY
total_qty DESC limit 1")->result();
return $query;
}
and the question is what is the correct code?
Here's one solution, but first make sure on your controller the $this->load->library('session'); is loaded or you can just add in on autoload.php.
Also you must add parameter to your model that will handle the user_id in session, so for example here's your model looks like:
function get_rekomen($user_id) { ...your query here... }
Then on condition using the user_id will be
t.USER = $user_id;
So on your controller, you can call your model like:
$user_id = $this->session->userdata('user_id');
$this->ModelClass->get_rekomen($user_id);
Additional: On your login phase, if the user success from logging in, the you must set the user data on session such as this $this->session->set_userdata('user_data', $session_data);
Hope this helps!
Maybe you can use this. You need to define $user_id first before adding them to WHERE statement...
$user_id = $this->session->userdata('user_id');
$sql = "SELECT c.*, t.produk_id, t.id_transdet, t.kategori_id, t.total_qty, t.USER
FROM transaksi_detail AS t
LEFT JOIN(SELECT g.id_produk, p.slug_produk, p.foto, p.foto_type, p.harga_diskon, p.diskon, p.harga_normal, p.judul_produk, g.kat_id, k.judul_kategori
FROM(SELECT MAX(m.id_produk) AS id_produk, m.kat_id
FROM produk AS m
GROUP BY m.kat_id)
AS g
INNER JOIN produk AS p ON p.id_produk = g.id_produk
LEFT JOIN kategori AS k ON k.id_kategori = g.kat_id)
AS c ON c.kat_id = t.kategori_id
WHERE t.USER = '$user_id'
ORDER BY total_qty DESC limit 1";
$query = $this->db->query($sql);
return $query->result();
Hope this can help you...
I am using a post table and two type of entities school and user can add something to this table. post table has a post_from field to differentiate whether post is from user or school.
i want to write one query with join to user table or school table depending on post_from field.
SELECT *
FROM post
LEFT JOIN `user`
ON (user.id = post.uid AND post.post_type = 'user' )
LEFT JOIN school_profile
ON (school_profile.id = post.school_id AND post.post_type = 'school')
You can do this with a union :
SELECT post.*
FROM post
INNER RJOIN `user`
ON (user.id = post.uid AND post.post_type = 'user' )
union
SELECT post.*
FROM post
INNER JOIN school_profile
ON (school_profile.id = post.school_id AND post.post_type = 'school')
Alternatively, you can do this :
SELECT *
FROM post
LEFT JOIN `user` ON (user.id = post.uid)
LEFT JOIN school_profile ON (school_profile.id = post.school_id)
WHERE post.post_type IN ('user', 'shcool')
Here's my current query:
$q = "SELECT u.uid,CONCAT_WS(' ', u.first_name, u.last_name) name, CASE WHEN t.name IS NULL THEN 0 ELSE t.name END AS teamname ";
$q .= "FROM {$users} ";
$q .= "LEFT JOIN {$schedule} ON u.uid = s.uid ";
$q .= "LEFT JOIN {$products} ON p.admitting_id = u.uid ";
$q .= "LEFT JOIN {$teams} ON t.id = s.team ";
$q .= "WHERE (u.uid = {$current_user_id} AND u.roles = 'hoa') OR ";
$q .= "(p.id={$pid} AND p.admitting_id != 0) OR ";
$q .= "(t.admitting = 1 AND s.inactive != 1 AND s.role = 'hoa' AND (s.date = '{$today}' OR (s.date = '{$yesterday}' AND t.overnight = 1))) ";
$q .= "ORDER BY u.last_name,u.first_name,t.name";
What I need to get is:
Current User, if Current User has role hoa
User where uid matches admitting_id from products table
All users with role hoa who are scheduled today or were scheduled yesterday on an overnight shift. With those scheduled users, I need the names of the team they were/are scheduled with. Duplicate uids is good because I need all the different team names if they were scheduled on multiple teams.
What I don't need to get is:
Team name for options 1 and 2 above.
What I'm getting is:
Everything perfect for #3 above.
All possible team names for #1 and #2 above, whereas I want the team names to be 0 if they're not on the schedule.
I could split this up into three queries but that's not preferable.
Sometimes formatting the query out a bit helps visualise. It doesn't need to become three queries, I agree.
Can you provide some sample schema and data, ideally both in the question description and on something like sqlfiddle for us to play with?
It's hard to visualise your structure and data, but I'm speculating the issue is every record in users table is joining to schedule with the on clause you've used.
You could move the logic of condition 3 into the on clause (it's a left join, so nothing else will break) or duplicate it in the CASE statement in the SELECT:
$q = <<<SQL
SELECT
u.uid,
CONCAT_WS(' ', u.first_name, u.last_name) name,
IF(
WHEN t.name IS NULL THEN
0
ELSE
t.name
END AS teamname
FROM {$users} AS u
LEFT JOIN {$schedule} AS s ON (u.uid = s.uid)
LEFT JOIN {$products} AS p ON (p.admitting_id = u.uid)
LEFT JOIN {$teams} AS t ON
(
t.id = s.team AND
t.admitting = 1 AND
s.inactive != 1 AND
s.role = 'hoa' AND
(
s.date = '{$today}'
OR
(
s.date = '{$yesterday}' AND
t.overnight = 1
)
)
)
WHERE
( -- 1: Current User, if Current User has role hoa
u.uid = {$current_user_id} AND
u.roles = 'hoa'
)
OR
( -- 2: User where uid matches admitting_id from products table
p.id={$pid} AND
p.admitting_id != 0
)
OR
t.name IS NOT NULL
ORDER BY
u.last_name,
u.first_name,
t.name
SQL;
I'm absolutely not certain that'll fix your problem without something real to play with... Happy to continue looking if you can provide a working example sandpit.
I have two tables like below pictures :
users table :
offer_comments
offer_comments table stores comments and answer to comments.
by using below function I can get comments and answer to comments depending on $id.
function getCommentsAnItem($id){
mysql_query("SET CHARACTER SET utf8");
$result_comments = mysql_query("select e.comment as comment,m.comment as answer_to_comment
from offer_comments e
inner join offer_comments m on e.id = m.quet
where e.offer_id=$id and e.confirm=1");
$comments = array();
while($a_comment=mysql_fetch_object($result_comments)){
$comment = array(
'comment'=>$a_comment->comment,
'answer'=>$a_comment->answer_to_comment
);
array_push($comments,$comment);
}
return $comments ;
}
How do I can get name and family from users table depending on user_id in offer_comments table ?
updated:
admin is where user_id equal = 1 .
user_id 14690 is a user and leaves a comment and admin answer to him.
in the users table :
I would like to get an array of comments and answer to comments depending on $id :
"comment=>how a u ?","answer=> I am fine","comment.name=>name","comment.family=>family"
Join your user table with your current query with offer_comments aliased as m so it will show the name and family of user who belongs to m.comment or the one who added m.comment,if you want to show the user of offer_comments aliased as e then join on e.user_id
SELECT
e.comment AS `comment`,
m.comment AS answer_to_comment,
u.name,
u.family
FROM
offer_comments e
INNER JOIN offer_comments m
ON e.id = m.quet
INNER JOIN users s
ON s.id = m.user_id
WHERE e.offer_id = $id
AND e.confirm = 1
I'm having a problem trying to count the number of user records according to the user's id, however I'm using a subquery to join 2 tables that one has a count parameter but I get an error saying duplicate column name 'user_id.
The query:
$sql = "SELECT loc.location_id,
COUNT(loc.location_id) AS total_records
FROM locations loc
LEFT JOIN
(
SELECT usr.*,
loc.*
FROM
(
members usr
INNER JOIN locations loc
)
WHERE usr.user_id = " . $user_id . "
AND usr.account_disabled = 0
ORDER BY loc.submit_date DESC
) usr ON (loc.user_id = usr.user_id)";
All I need it is to return the user's info and the total_records count done by the COUNT function.
Cheers.
EDIT:
This is what I get returned for this SQL:
SELECT loc.location_id,
loc.street_name,
loc.city,
loc.state,
loc.county,
loc.country,
usr.user_id,
usr.username,
COUNT(loc.location_id) AS total_records
FROM locations loc
INNER JOIN members usr ON (loc.user_id = usr.user_id)
WHERE loc.user_id = $user_id
AND usr.account_disabled = 0
GROUP BY loc.location_id
It's not exactly clear why you've got the derived resultset or the LEFT JOIN. Try this:
SELECT loc.location_id,
COUNT(loc.location_id) AS total_records
FROM LOCATIONS_TABLE loc
INNER JOIN MEMBERS_TABLE usr
ON (loc.user_id = usr.user_id)
WHERE loc.user_id = $user_id
AND usr.account_disabled = 0
GROUP BY loc.location_id
I think the problem is this part:
SELECT usr.*,
loc.*
Both tables have a user_id