mysql liking you and others [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I wonder how to do this
I mean the "You and 19,389 others" How do you identify YOU from others. If a user is logged in and like a status or favorite a status or something like facebook. I wanna catch the "you" part. But I don't seemed to understand the logic behind it.
I'm using php, and jquery. How do you sort this out in sql? or is it really sql? how do you define the user from others?
If I'm doing it the wrong way or asking it the wrong way, please tell me the right way and the answer guys I badly need your help.

With only one query you can do like this:
SELECT COUNT(user_id) AS everybody, SUM(user_id = "your_user_id") AS you FROM fb_likes WHERE post_id = 4;
Then if you is bigger than zero print (in fact it must be 0 or 1 only):
You and [everybody - you] others like this.
Else
[everybody] like this.

First of all, when you click on "Like" there's an entry going inside a database assigned to the post. So let's say the post has ID 1234567890 so you going to see something like that inside your database table :
PostID UserID
1234567890 54543534
1234567890 75231415
1234567890 78653421
1234567890 99653221
// Query example
SELECT COUNT(*) FROM LikeTable WHERE PostID = 1234567890
// Return 4
First of all you count all like, there's 4 here.
After, you check if the UserID of the current logged user is in the database.
Yes. He liked.
No, he didn't liked.
Let's say we are user 54543534. I am in the database so I liked the post.
// Query example
SELECT PostID FROM LikeTable WHERE PostID = 1234567890 AND UserID = 54543534 LIMIT 1
// Return 1
Let's say we are user 8748977777. I am NOT in the database so I didn't liked the post.
// Query example
SELECT PostID FROM LikeTable WHERE PostID = 1234567890 AND UserID = 8748977777 LIMIT 1
// Return 0
So I say the IF/ELSE statement would be :
IF I liked
You and COUNT - 1 others like this.
ELSE
COUNT like this. Click to like.
EDIT :
I think something like that will make the job :
SELECT COUNT(PostID) AS All, SUM(UserID = X) AS Liked FROM LikedTable WHERE PostID = X

Query for others:
SELECT COUNT(userid) FROM likeTable WHERE postid = $postid
Query for you:
SELECT COUNT(userid) FROM likeTable WHERE postid = $postid AND userid = $_SESSION['userid']
Then do:
if ($youCount == 1) {
echo "Liked by you and " . $othersCount - 1 . " others";
}
else {
echo "Liked by $othersCount people";
}

I don't know much about optimal database schemas, but the way I see this is that every single like in Facebook terms is a node on FB's social object graph. Which probably means that every single like is an individual database record on some table. To count the number of likes, you just do a COUNT on like records associated with a post.
Now if that's the case, then it's trivial to include some identifying information on the like record on who performed it. Top-of-head, it would be an FK on a user's PK.
So if you're logged in on Facebook, it knows who you are and what your associated user ID is. For every post that it aggregates the likes to, it can counter-check your user ID to the user ID FKs on the likes, and determine which of those posts you've actually liked. Hence, it can conditionally display either You and 14,000 others like this or 14,001 like this.

Struggling to understand the question but I think you might be trying to do too much in one go.
Either Run two queries, one to check if 'you' (your session id etc.) are in the table that records this or pull the whole lot and use in_array or something similar to see if 'you' are in the list.

Related

how to stop same data display second time in php query?

i try to development exam management system. I do not want same
question or same id not showing second time or other any time exam a
user. How can this condition be given?
my function is:
public function qustionShow($question, $limit=4){
$show = $this->conn->query("select * from question where cat_id ='$question' ORDER BY RAND() LIMIT $limit");
while ($row=$show->fetch_array(MYSQLI_ASSOC)){
$this->qus[]=$row;
}
return $this->qus;
}
You would need to keep track of which questions have already been asked. you can save the question id's to the Session if it's you don't want the questions to be selected again only for the session.
you could initialize an array to the session
$_SESSION['questions_asked'] = array();
and then once a question is asked you would
array_push([THE QUESTION ID], $_SESSION['questions_asked']);
of course you need to replace [THE QUESTION ID] with the sql id for whatever question was asked
keep in mind you would need to modify your query to account for anything saved in the session.
If you don't want them to ever be shown again you would need to record which questions a user has seen and would need to store that persistently in the database probably.
possibly you can have a table to store those in for each user
user_question_asked
with at least these 2 columns
[user_id][question_id]
so each time a question is asked you insert the current user id and question id
then your query could be
SELECT * FROM question where cat_id ='$question' AND [QUESTION_ID] NOT IN
(SELECT question_id from user_question_asked where user_id [CURRENT USER's ID]
) ORDER BY RAND() LIMIT $limit;`
Hope that helps, I'm not sure what your table / column structure is, but those are a couple suggestions I have for addressing this problem.

Implementing voting or "likes" using MySQL [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I am creating a system where users (who are identified by a user id number) will be allowed to vote on posts (think Reddit, StackOverflow, etc).
Users can vote a post up or not vote at all on it.
The number of votes on a given post can easily be stored within the table containing the posts.
Keeping track of who has voted, however, is a different task entirely that I'm not sure how to approach.
I was thinking I could have a table that would have two columns: user id and post id.
When they vote on a post, I add their user id and post id to that table. If they unvote, I remove that entry from the table.
EG:
User ID | Post ID
1 | 3949
1 | 4093
2 | 3949
etc...
Is this a reasonable solution?
Yes this is reasonably simple and easy solution to the problem. You can do the same for your comments(if you like to). In your MAIN_POST table assign a post_id and use this same post_id in other tables (comments(post_id, user_id, post_comment, comment_time) and votes(post_id, user_id, vote_status(you can use 1 for vote up and 0 for vote down))). It will complicate your sql queries, to retrieve data, a little but you can do it. And on android side there are alot of tricks to handle and furnish this data in application and you can make this vote(like) and comments idea just like facebook (YOU for your comments and likes and NAMES for others).
I wouldn't remove rows from the table. I understand why you would want to do that, but why lose the information? Instead, keep a +1/-1 value for each entry and then sum up the values for a post:
select sum(vote)
from uservotes
where postid = 1234;
And, I agree with Rick that you should also include the creation date/time.
Using an 'in between' or 'joining' table is a perfectly acceptable solution in this case. If relevant you could even add a timestamp to the relation and show to the user when a user has upvoted something.
Also it is important to take care of proper Indexes and Keys to have your table structure also perform properly once the dataset grows.

PHP - Grabbing info from the database (complicated) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need a little help. I need to grab all rows which contain their logged in ID from a database. Basically, I have a column in the database called "owners" and in it it has a few IDs. Lets say "1, 6, 8", If user ID 6 is logged in, I need to grab that row. If it doesn't contain 6, say "2, 5, 7" then don't grab it. My wording probably isn't good.
--------------------------------
ID |Name |Other Info |Owners|
---|--------|-----------|------|
1 |Testing |Testing 123|1,4,6 |
---|--------|-----------|------|
2 |Testing1|Catz |5,7,8 |
---|--------|=-----------------|
3 |Testing2|Woof |6,9,10|
---|--------|-----------|------|
In other words:
User ID 6 logs in.
Goes to page which needs the rows to be grabbed.
Script grabs rows 1 and 3 (because the owners list contains 6).
PHP foreach, showing the resutls in a table. (Don't need help with
this).
The reason I want this is I don't want ownerid2, ownerid3, ownerid4 taking up loads of space in separate columns in the database.
You could try this:
SELECT * FROM table_name WHERE FIND_IN_SET(?, Owners);
where ? has to be replaced with the user id, for example
SELECT * FROM table_name WHERE FIND_IN_SET(6, Owners);
as other pointed out, you might also want to read about database normalization, and here is the docs for FIND_IN_SET
A cleaner way to accomplish this is to use a separate table for the owner relationship.
Table = Owner, has ID.
Table = Test, what you are currently trying to get
Table = OwnerTestRel, has a foreign key to Owner and one to Test.
Then your query can become something like
Select test.*
FROM test
JOIN OwnerTestRel ON test.id=OwnerTestRel.testId
WHERE OwnerTestRel.ownerId = {owner's Id}
could this possibly be what you're after?
SELECT * FROM table_name WHERE Owners LIKE '%6%';
????
EDIT: doing it this way would pull up other owners like 68, 600, 6123. you should look into database normalization.
EDIT2:
just thought of this, since there's no spaces between your commas you could use the explode(); function like so:
$ownerid = //WHATEVER THE OWNERS ID IS
$sql = "SELECT * FROM table_name WHERE Owners LIKE '%$ownerid%'";
$result = mysqli_query($con, $sql);
while($data = mysqli_fetch_array($result)){
$owners = $data['Owners'];
$owners = explode(',',$data);
if(in_array($ownerid, $owners){
//SHOW ROW DATA HERE
}
}

"likes for comments" database designing? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
i have database tables for Posts and comments.
i want to allow users to put a like or dislike for each comments and posts.
so..i have few ideas of doing this. please tell me either i am correct or not.
create two additional columns in comments table.
likes | liked_uids
if a person clicks on like button then add +1 for the current value in likes field, else add -1 for current value.
and add user's id to liked_uids field as a sting separated by "-" dashes.
then next time i can get that string to an array and check that,
either current user id has recorded or not. if user id is, then can decide that user have participated for liking.
but i have little problem on this structure, that what will if more than one user going to like at once for a post ? then i may lose some data from liked_uids string (one last uid).
so please tell me what is the correct way of doing this?
You can create like this->
id type ('comment/like') uid comment post_id
1 comment 1 good post 100
2 like 2 null 101
3 like 1 null 102
4 comment 3 bad post 104
It is not recommended to store like count.If you want to count the likes for a particular post:
select count(*) from tableName where post_id = 100
Storing user id separted by any delimiter will land you on problems, Hence not recommended. It will be tidies job to update or retrieve if your store user id using delimiter.
If you want to see if particular user is liked a particular post or not, use below query:
select count(*) from tableName where post_id = 100 AND uid =1
One way is to use a separate Likes table with columns Likes, DisLikes, Likes_UID, DisLikes_UID and mapping table for comments and likes ex: Comments_Likes and posts and likes Posts_Likes
I'm typing way too slow on my mobile ^^ Everything already answered.
I never did anything that is similar, but I wouldn't add the two columns in the comment table. I would rather create a new table like "votes" and it would have following columns.
comment_ref | like | user_ref
Every time someone likes a comment you insert a new line there. You could also make the combination comment_ref and user_ref as a key, so you can't insert it twice.
In the end you would just make a query as such to get the votes of a single comment.
SELECT COUNT(*) FROM votes WHERE comment_ref = 123

Database structure for "follow" feature similar to twitter?

I want to create a similar feature as Twitters "follow" feature but wondering a bit about how you would create a database structure for something like that. Say we have a user table like this:
id, username
And then a posts table that looks like this:
id, userid, post, date
What i want to do is to let a user follow other users, and then in a stream get their posts. So i'm guessing there should be a follow table. Where you can insert which user follows which user. But then how do i query a stream out of this? If user 1 follows user 2 and 3. I want to make a query like
SELECT posts FROM $peopleIfollow ORDER BY date
But as you see the above example is not a valid query, how can i gather "follow"-information and query the results? Or more what is the structure i should be looking into to achieve this? Should followers be put in an array and is it possible to have an array in a query?
Quick and simple solution that I can think of...
following(follower_id, following_id)
Then you'd get posts something like
SELECT *
FROM post_table
LEFT JOIN following
ON following.following_id = post_table.userid
WHERE following.follower_id = ?
LIMIT 0, 20;
Where ? is the ID of the current user. This would then join the tables and pull the posts of the person that the follower is following. You could also throw in an order by date.
This SQL statement could be vastly improved, but that's a quick and simple idea for ya.

Categories