I'm having an issues with my queried results. I have three tables, a follower table, a user table, and a posts table, while my logged in user = $user_name. My code calls all from follower table where the the follower is = to the logged in user, and calls the name of all the users their following ($followi). Then I call all from the posts table where the posters name is = to the username of the users the user is following. and the code works.
MY PROBLEM: I have tried to order my results by date, but MYSQL is placing precedence on the Followi, and displays in order of user who posted, then order of date. so if one user has 2 posts a month apart, it displays these two, then displays the next user, even if they posted 5 times in the last minute.
how do i display ONLY Based on date while keeping it to where it only displays the posts of users that the logged user is following?
heres my code
<?php
$follow = "SELECT followi from follow where follower = '$user_name'";
$runf = mysqli_query($con,$follow);
while($row=mysqli_fetch_array($runf)){
$followi = $row['followi'];
$sel_msg = "SELECT * from posts where poster_name = '$followi' ORDER by post_time desc";
$run_msg = mysqli_query($con,$sel_msg);
while($row_msg=mysqli_fetch_array($run_msg)){
echo "
<div id='post_box'>
".$row_msg['post_content']."
</div>
";
}}
?>
That's because your loop starts with the followersi data (the while loop). This is the order you are retrieving data and displaying it in the current logic:
Get posts for User A
User A Post 1
User A Post 2
User A Post 3
Get posts for User B
User B Post 1
User B Post 2
User B Post 3
What I believe you are trying to do is get the posts in date order but retrieve the user information to display at the same time. You can do that with a JOIN statement e.g.
$sel_msg = "SELECT posts.*, users.name, users.image
FROM posts
JOIN users ON posts.userId = users.id
ORDER BY post_time DESC";
$run_msg = mysqli_query($con,$sel_msg);
There is no need for the first sql statement in your code
Related
I have this query which works great
$sub_query = "SELECT * FROM posts WHERE post_status = 'published' AND
post_user = '{$out}' ORDER BY post_id DESC ";
The query brings back all of a specific users posts ordered by descending post id. The problem is it loops through every post user and orders them accordingly but I want the newest post overall not just the newest post per user.
Here is an image of the results to hopefully help explain better
You can see the query runs for a specific user and then moves on to the next user where I am trying to get the newest post id first
I have tried to follow this similar question but it brought back the same results.
$sub_query = "SELECT * FROM (SELECT * FROM posts ORDER BY post_id DESC)
T WHERE post_status = 'published' AND post_user = '{$out}' ";
Would I be able to order all of the selected posts first? Then use my where statement?
If you want the newest post per user, then remove the where and add a limit:
SELECT p.*
FROM posts p
WHERE p.post_status = 'published'
ORDER BY post_id DESC
LIMIT 1;
I did not understand if what you are looking for is
1) the list of the last post per user
2) the list of all posts sorted by the newest (regardless the user)
In both case you don't need to loop all users, you can extract data ordered just using the query itself (i strongely advise you on this solution).
option 1:
SELECT *
FROM posts
WHERE post_id IN (
SELECT MAX(post_id)
FROM posts
WHERE post_status = 'published'
GROUP BY post_user
)
ORDER BY post_user ASC
option 2:
SELECT *
FROM posts
WHERE post_status = 'published'
post_id DESC
I want to fetch and display posts of different users as they upload a post.. i can access their posts by userid but the posts are in sequence and not random or new posts. For example if 2 users have posted 4 times, 3 times first user and 1 time 2nd in this sequence 1st user post, 2nd user post, 1 user ,1 user, my query will return the 3 posts of user 1 first and then single post of user 2 irrelevant of the original sequence and time(old/new).
My query is
//this is my table -> user_post(userid,post_id,post,time)
$query = "SELECT * FROM user_post ORDER BY (time) DESC WHERE userid = '$user_id'";
and i want to load only 4 latest posts and when i next refresh the field the next 4 posts must be loaded..
this is a Android Client-Server Based application and i'm using volley to make the http calls and exchange data using JSON.
Did you try to use the limit clause ?
You code would be like :
$query = "SELECT * FROM user_post ORDER BY (time) DESC WHERE userid = '$user_id' LIMIT 4";
You need to include a LIMIT clause inside your query:
$query = "SELECT * FROM user_post ORDER BY (time) DESC WHERE userid = '$user_id' LIMIT 0,4";
On every request you maintain a count of how many rows have loaded in $lastLoadedRow. The next several times you load you would do:
... LIMIT $lastLoadedRow,4;
... LIMIT $lastLoadedRow,4;
... LIMIT $lastLoadedRow,4;
I have created a posting system along with a liking system. I wanted to select the top three posts with the most likes but I don't know how to do that.
I have this code but I can't figure out how to select more than just one row.
$get_pop_posts = mysql_query( "SELECT MAX( likes ) AS popular_posts FROM `posts`;" );
while($fetch_pop_posts = mysql_fetch_array($get_pop_posts)){
$pop_posts = $fetch_pop_posts['popular_posts'];
echo $pop_posts;
}
this piece of code only fetches one row from the database.
SELECT *
FROM posts
order by likes desc
limit 3
SELECT MAX( likes ) AS popular_posts FROM posts
This query return single result because of the function MAX().
Try this
SELECT TOP 3 FROM posts
Firstly give unique id to each post that should be incremented automatically each time a new post comes.
Then,
SELECT id FROM posts order by likes desc limit 3
Pass these id's as arrays to get the complete post.
I have a follow system on my website, where you're able to follow other users.
On the home page of the website, I'm trying to make it show the latest 10 posts from the users you're following (not 10 from each person you're following, but 10 in total).
I have one table called followers with the following structure:
id | user_id | following_id
1 20 52
2 20 55
1 20 75
... ... ...
Where user_id is your id and follow_id is the id of the users you're following.
I then have a table called posts where all the posts from users are collected.
What I'm trying to do now is create a query that gets the latest 10 posts from the users you're following (ordered by date).
Here's what I've made so far:
/* Select all users this person is following */
$stmt = $cxn->prepare('SELECT following_id FROM followers WHERE user_id = ?');
$stmt->bind_param('i', $user_id);
$stmt->execute();
$result = $stmt->get_result();
/* If a result exists, continue. */
if ($result->num_rows) {
while ($row = $result->fetch_assoc()) {
// not sure what to do here, how would the query look?
}
} else {
echo "You aren't following anyone!";
}
I'm not sure what the query would/should be to get a total of 10 of the latest posts from the people you're following.
Please help!
You're on the right track, but your main selection is still the posts, rather than the followers - those are the subquery. You probably want something like this.
SELECT * FROM posts WHERE poster_id IN
(SELECT following_id FROM followers WHERE user_id = ?)
ORDER BY posted_at DESC
LIMIT 10
The query you wrote is still there, but it's been surrounded by the query to get the actual posts. Substitute poster_id and posted_at for what you call them in your posts table.
SELECT *
FROM posts p
JOIN followers f
ON p.author_id = f.following_id
WHERE f.user_id = ?
ORDER BY p.date DESC
LIMIT 10;
I want to display the number of posts a user has made, but I have a few different tables and need to match ids and count them.
Something like this:
Users Table
userid username email regdate
34 mister email#some.tld 2013-10-26 12:01:07
Posts Table
postid creator post_comment post_title status
1 34 This is comment Post 1 published
2 12 This is comment Post 2 published
3 34 This is comment Post 3 pending
4 25 This is comment Post 4 published
5 34 This is comment Post 5 published
Now I already have a query where I select all data about a particular user from the users table:
mysqli_query($conn, "SELECT * FROM users WHERE userid = $userid");
$userid is the id of user that's currently logged in. And this works fine: it selects all relevant information about that user.
But I want to display the number of posts each user makes. I read somewhere on w3schools the SQL COUNT function works something like this:
mysqli_query($conn, "SELECT COUNT(creator) AS userposts FROM posts WHERE creator=$userid");
However, as you can see in table example posted, out of 5 posts, 3 are from user 34 and 1 of these 3 is not published.
I don't know how to make a SQL query that selects all users where userid matches $userid and then selects creator and status from posts and counts the number of posts where creator is $userid and status is published.
This is the output I'm ultimately aiming for:
User mister have 2 posts published.
In order to get only the number of published posts, you should refactor your condition statement by ANDing status = 'published'
mysqli_query($conn, "SELECT COUNT(creator) AS num_posts FROM posts
WHERE creator=$userid AND status = 'published'");
SELECT COUNT(creator) AS userposts FROM posts p
LEFT JOIN users u ON p.creator=u.userid
WHERE creator=$userid AND status='published'
As you asked in the comment, all in one query:
SELECT users.*, COUNT(creator) AS userposts FROM users u
LEFT JOIN posts p ON p.creator = u.userid
WHERE p.creator = $userid AND p.status = 'published'