I would like to make it able for my users to add each other as friends. I just don't know exactly how to do it.
I have a table called "members" where the users have (ofc) and ID,username,pass etc etc. and then I was thinking of creating another table called "friends", where I was planning to have the rows -> username (the friend added) and friend_to (who the friend 'belongs' to).
But; I just don't know - how I should make the "add friend" link, and make it INSERT INTO the table? Can I make an onClick on the link, or what should I do? :-s
Thanks in advance.
Have a table called friends have rows, (id, user_id, friend_id, status, time)
id is the index, user_id is the one requesting friendship, friend_id is the receiver, status is the status of friendship like pending or declined, and time is the timestamp of the time when the request was sent.
Then in a php code check if the users aren't friends then let them add each other as friends. One way you could check was like this
(SELECT COUNT(*) AS total WHERE (`user_id` = ".$_SESSION['user_id']." AND `friend_id` = ".$_GET['friend_id'].") OR (`user_id` = ".$_GET['friend_id']." AND `friend_id` = ".$_SESSION['user_id']."))
the above code will check if they are users, and if they are you would not let them re add each other, if they aren't the user gets a button to add them to friends, where it inserts into a database new row, with user_id being the user sending and friend_id the user's page the sender is submitting the button from
On the backend, you will need separate PHP functionality (easiest way is to simply have a separate PHP page) to handle the result of the "add friend" link. Your table layout seems adequate from what you have describe. The "Add Friend" link will need to send a request back to the add_friend PHP handler which includes ID of the user and the ID of the user they added. And this handler will be where you include the MySQL code for performing the insert, based on the data that is provided to it.
On the front end, you can have the link send them to a new page, or you can use an onclick event to issue an AJAX request and update things behind the scenes without requiring a page reload. That choice is up to you and what fits your design best. The later is more complicated and will require some Javascript and/or jQuery to handle the AJAX parts, but it often results in a more pleasant user experience.
Related
Just a question about what the best way is to send a message to a group (Like whatsapp group messaging).
I save the members per group into a separate database table and once a user sends a message to the group it retrieves the users from this table. I'm a bit stuck on part two:
As I see it there would be three methods.
Insert the message into a database for the entire group. Send the message back to the different users based on groups they are member of.
Insert the message in the database per user of the group. So loop the members and insert for every member. This has the advantage over the first method that downloaded, read and deleted status can be tracked.
Create a table for messages and one for the message-recipient link. But I'm not really sure how a would query in the most optimum way as to retrieve all the data (downloaded, read, etc) for a group of users. Is that possible in a single query?
Would anyone know what the best method would be? I can imagine that method 2 will fill up a database pretty fast, but method 1 doesn't have the ability of tracking status for a message.
At the moment I am using method 1, but I run into the problem that when a user deletes a message. It would still be returned on other devices because there would be no easy way of setting a certain deleted flag for group messages.
Does anyone know how apps like telegram, whatsapp and so on do this? And would method 2 give problems later on when millions of messages are sent?
Regards,
Ron
I would seperate it like this.
Messages
id, message, owner_id, deleted
Groups
id, name
Message_Group
message_id, group_id
User_Group
user_id, group_id
Then you can don't need to add a message to all users in that group. When you insert a new message you first inserts it into Messages, catch the id and inserts it into Message_Group with the message id and group id. To get the messages of a group just join Message_Group and Messages and you are pretty much done. Of course you also needs to join the user table to catch who wrote that messages.
im making a site thats a news/blog kind of site where people can leave comments to the posts that are made. then logged in users are able to give a comment a thumb up or down. it works fine at the moment i click the thumb and it uses ajax to add the count to the database and update the number and it also stops the person from being able to click the thumb again but if you press f5 to refresh the page you can click the thumb up again. how can i stop this from happening?
adding this to a database is an option i was thinking of but the site needs to be able to handle lots of comments and users there could be thousands of thumbs made to comments since its an easy action to perform the database table would be huge after a short amount of time which would surely slow down page loads since it will be querying a massively long table every time you view a page with comments.
currently i keep count of the thumbs up and down in the comment table so it querys the comments table and will display the numbers. are you suggesting i add a new table that contains userid and a commentid if someone makes a thumb up so i can query that table and if there is a row where userid == the logged in user and commentid == the comment dont allow? if so this is the thought i had on how to do it but as i said above it will lead to a massive table that will surely slow down the loading of the page
One way to go is when you load the comments also load if that user has already casted a vote on each of them, disable the thumb up button if that user already voted.
You shouldnt store the click as a simple counter but store the actual event
Create a seperate table called 'clicklog' or something and add the fields like userid, commentid, IsThumbsUp. Then in your ajax page you can add the thumbs up to this table (IsThumbsUp is a boolean. True for thumbsup and false for thumbs down.
then instead of using something like SELECT comment, thumbsUp, thumbsDown you could use
SELECT id, comment,
(SELECT count(commentid) FROM clicklog WHERE commentID=id AND IsThumbsUp=1) AS thumbsUp,
(SELECT count(commentid) FROM clicklog WHERE commentID=id AND IsThumbsUp=0) AS thumbsDown
FROM comments
And also in your AJAX page you can check if the user has voted before. If he has, dont allow it. Or even better. When he has voted UP, he can change it to vote DOWN (change the IsThumbsUp from 1 to 0) or visa versa.
Just make sure each user only has one connection with commentid and userid.
Also make sure you put an index on the fields in clicklog to get the information quickly. I'd put the primary key on commentid and userid combined and then a seperate index on commentid and IsThumbsUp combined
So I'm a bit lost at this part.
On my site I would like the give the ability for users to like profile pages.
So my logic is this.
I have a database table named user_likes, this contains 3 rows, id, user_id, liked_by.
The actual profile page has a like button on it with a class like.
When the user clicks on it in inserts the data in to the user likes table and changes the button text to unlike and the class to unlike too with ajax.
And this is the part where I'm stuck.
So the profile has a has many relation likes, I can count the likes received, but I'm stuck at that part how to keep the button unliked after page refresh.
Because my logic was this (but its a fail):
onclick the button, data insert with ajax, button change with ajax, grab the liked by id and if that equals to the logged in users id keep the button unliked.
But since it returns a list of array I can't do that.
So I don't want anybody to code it for me, I would just like a hint for this if its not a big request.
I saw that you posted the same question on the FuelPHP forum, so here I will try to give you another solution.
Why your relation needs "id, user_id, liked_by"? I think you can improve you database modifying a little your tabel.
Reading your question I think you have a structure like that:
// USER TABLE
id
username
... something more
If you create a table named "LIKE" you'll have this situation:
// LIKE
id
created_at
modified_at
... something more
So now you can create a many-to-many relation with a table named "users_likes" with:
// USERS_LIKES
profile_id // it wil be related to the user_id, is the user's profile being liked
liker_id // it will be the user_id of the user that likes the profile
So if you want to retrieve the number of the like you'll have all the information in the current user model or in the profile user model.
So I'm wondering how sites like YouTube remember when a user has "Liked" a video and prevents them from liking it again. Similarly, how a site like Reddit remembers upvotes and downvotes and prevents a user from upvoting a piece of content they already upvoted.
Is it as simple as a database table where the ID of the content is stored along with the ID of the user and their response? I feel like that would become a massively huge table. Is there something trickier going on?
The sites require you to login before clicking on "like" and "upvote". The content document in the DB will have a field which will store the number of likes received. Before the like button is rendered, the sites will check against the logged in user's records in the DB, to check if he has already liked it- accordingly a "like" or "unlike" option is displayed.
If you check here in Stackoverflow, if you click "upvote" on your own question or answer, a message is displayed, that you can't upvote our own post. What happens is when you click "upvote", ajax request will be sent to server with the user ID and other information like question id etc. and the server code will check if you are allowed to upvote i.e your user ID should not be the same as the post creator's ID.
I have a like/dislike system on my page.
Database tables:
1.) One that contains your posts with a unique ID for each post and a user_id of who created it (along with other info like content, tags, etc).
2.) table called likes with AT LEAST the following fields, ID, post_id (corresponds to the post in the post table that was liked or disliked), user_id (corresponds to the user in the users table that did the liking/disliking), status (0 or 1, 0 being liked the post, 1 being disliked the post).
When a user likes a post, insert the row into the likes table with their user_id and the post_id, set the status as 0 (or just leave empty because 0 is the default). When a user dislikes a post, do the same but set the status as 1.
That way, on a post page you can get the count of all the users that liked or disliked a post. On a users's profile page, you can get all of the posts a user either likes or dislikes. You can also rank posts by which has the most likes or dislikes. Or even rank specific users by who has posted content with the most likes or dislikes.
Do not allow a users to like/dislike a post if they already have a record in the database. (Basically just check the count of records in the likes table where the post_id is equal to the current post and user_id is equal to the logged in user)
Cross reference the post table to get the post's author's user_id. If the post author user_id is the same as the logged in user, or the user is NOT currently logged in, do not allow them to vote.
The queries for doing all of those are simple (simply SELECT * or SELECT user_id) but that is the basic idea.
Yes, it's that simple. Generally, sites use ip addresses though, so that users don't vote twice on different accounts.
edit: Sorry, I was wrong. According to Quentin, websites don't base it off IP because of the possibility that multiple users have the same IP and aren't trying to exploit the system. Smaller scale sites (at least some of the ones I've used) have implemented a voting system based on IP because it would otherwise be easier to manipulate content ranking.
I want to create a user history function that allows shows users what they done.
ex: commented on an ad, posted an ad, voted on an ad, etc.
How exactly do I do this?
I was thinking about...
in my site, when they log in it stores their user_id ($_SESSION['user_id'])
so I guess whenever an user posts an ad(postad.php),
comments(comment.php), I would just
store in a database table
"userhistory" what they did based on
whenever or not their user_id was
activate.
When they comment, I store the user_id in the comment dbc table, so
I'll also store it in the
"userhistory" table.
And then I would just queries all the rows in the dbc for the user to
show it
Any steps/improvements I can make? :)
Look at the statistics and logging section of media wiki schema. You can implement something similar to this.
http://upload.wikimedia.org/wikipedia/commons/4/41/Mediawiki-database-schema.png
Similarly, what you could do is have mySQL based logging, ie every page hit is logged in the mySQL database, which tracks the IP, userid, datetime and page requested.
Then for the page that you view history, you could have a script like this. This is just pseudo code, so take it at face value.
<?php
$actions = array('comment.php' => 'posted a comment', "postedad.php" => "posted an ad");
$query = mysql_query("SELECT * FROM logHits JOIN users ON users.id = logHits.userid WHERE loghits.userid = $userid");
while ($row = mysql_fetch_array($query)) {
echo $row['username']." ".$actions[$row['pagename']."<br />";
}
?>
Again, this is just pseudo code and you can most certainly improve the concept by it. You could possibly use a combination of printf();/sprintf(); and make it so, for example, "a comment" in the action text hyperlinks to the actual comment.
There are quite a few ways to go about this. This probably isn't the best way.
You could also just do an insert query to a table like userHistory whenever the persons does an action like you specified. This is probably the best way to go about it. You could have that able with fields like id, userid, actiontype, actionid
actiontype would be "comment" or so, and actionid would be the id of the posted entry. Then you could easily map actiontypes to the comment page (passing the actionid) to view the actual posted comment.