I have a website where people can make posts and follow other users. I have a sidebar that has a value that keeps track of the number of posts that have been posted since your last visit.
I'm stuck thinking of how I should handle this. Should I create an entirely new table in the database called notifications that would hold the user's id and the number of posts since last visit, should I just add a column in the existing user table for this value, or should I use an entirely different method?
Thanks.
First of all: Think, which object this is a property of. In your case, the count will differ from user to user, so we might assume, it is a user property.
We could hang it on the last login, but this would give us a wrong count, if the user is logged in for a long period (The user doesn't want to know the count since his last login, but since his last activity!).
So the easiest way could be to add a field to the users table, that holds the last post ID - We just SELECT MAX(id) FROM posts and update users.lastSeenPost with the result on every user action. We can then display MAX(post.id)-users.lastSeenPost as the new post count.
Every post has a date recording when it was made.
Every user will have a date keeping track of when he/she logged in the last time.
By the following SQL statement you could ask the database to return the number of posts since the user logged in last:
SELECT COUNT(*) FROM `posts` WHERE `posts.post_date` > `user.lastlogin_date`
I suggest that you will create a cookie ($_COOKIE['lastPostId']) in each customer webbrowser with the LAST ID of your posts, and, when the user return, you will read $_COOKIE['lastPostId'] and query your database as SELECT * FROM posts WHERE id>lastPostId
Related
I am working on a social network website project. I have created database and everything.
The posts table has a preference column which stores the preference value according to the likes and comments that a post gets from the users and also the time at which the post is created.
To retrieve posts for a user's home page from the posts table, I am running a query using joins which sorts using preference column .
Now, suppose I retrieve 10 posts for a user to be shown on the posts table and user scrolls down and one more request is made from the user to retrieve next 10 posts to the server.
If in between of those requests few other users creates a new post or preference value of posts in the database changes in the between, and now if I the second request is run on the server, all the posts will be resorted for the second request (i.e. to show next 10 posts) but since the database is updated , this means in the second request there will be many chances that few of earlier 10 posts are retrieved along in the second request.
I want to know how to avoid these duplicate requests.
How facebook or any other social network solves this problem at the backend when their database is dynamic.
I would rather avoid such unreliable way of sorting at all.
As a user, I'd rather quit that service. Frankly, I hate such too smart a service which decides which posts I have to see and which not. And even dynamically ordered on top of that.
Make it ordered by date, by tags of interest, by something sensible, reliable and constant.
In your script store a record of the rows id returned.
For example, using a basic limit and just storing the latest id when the first select is done, and using the page number to determine the limit of records to return.
SELECT id, somefield
FROM SomeTable
WHERE id < $SOMESTOREDVALUE
LIMIT $PAGENUMBERTIMESTEN, 10
or storing the latest id after each page is returned (which you will need to store each time this is run)
SELECT id, somefield
FROM SomeTable
WHERE id < $SOMESTOREDVALUE
LIMIT 0, 10
If you store the time & date when the user first makes a request in a session, you could use that to filter the posts table.
So your SQL for the second page of results would be along the lines of
SELECT <some fields> FROM <sometables>
WHERE DatePosted <= $timefirstseen LIMIT 10, 10
Where $timefirstseen was loaded from the session variable. This will restrict your results to only posts that existed when the users visit started.
You would of course need to include a feature to allow the user to clear the session or do that automatically when they revisit their homepage to make sure they got to see the new posts eventually!
I have two tables in my database. One is a list of members each, of course, having a different member ID. The second table is a list of items that have been designated to one member.
Please note that each table has a user_id column. Based on user_id I want only certain items displayed on their webpage.
When Joe logs to the members area, he is taken to a welcome page. On that welcome page I want Joe to be able to click on a link which will take him to a page. This page will show items that only Joe is allowed to see. If another member clicks on the link it shows a message that says something like Sorry, not for your eyes.
I really am only learning and have spent the past couple of days looking for help with this issue.
I am thinking, from what I have read, that I may just be able to amend the top of my "special page" with an if statement which would say something like
if user_id from table_members = user_id from table_items
Show all rows from table_items which = user_id
There are huge number of tutorials available on internet on displaying dynamic data. Just try doing a search on "making dynamic webpages using php" like that.
let $user_id be a variable storing userid taken from user logged into the website.
$query="select * from table_items where user_id=".$user_id ;
$result=mysql_query($result);
To fetch a row:
`$row=mysql_fetch_array($result);`
To fetch specific column from that row:
$column_data=$row[$columnNameString];
Execute that query you will only get rows whose user_id column value equals to user_id variable value.
As title, when I have a content list, but not everybody can see them.
For example:
I have group 1 and group 2
I create a post, only the users in group 1 can see it.
I create another post, only the users in group 2 can see it.
User 1 in group 1, User 2 in group 2.
Then, when user 1 looks at the newest content list, he can only see the post 1.
Then, user user 2 looks at the newest content list, he can only see the post 2.
You can see, different account (user) will see different post in his/her post list. Because I store all posts in one table, and I store the group_id in it, when creating the newest post list for a user, I have to search all newest post and check one by one if the user can see it or not, if there are too many posts which he don't have permission, the performance will be very bad.
Also, the group is not static in some cases, such as: my followers, my friends, if so, I still can't store the user id in post table, and have to calculate their relationship to determine he/she can read this post or not. and again, the post can be with multiple-permission groups (my followers and my friends can see it).
How can I improve it?
BTW, why I don't insert the user_id array in post record, because maybe the number of group users is very big, so I can only save group_id in it.
BTW again,
I have one SNS website.
I use mongodb database.
I use php.
When user authorizes, store his group_id in session data. Then you can use that group_id when you generate your menu, make it an extra condition in your database query to get only posts that are allowed to this group to be seen.
If the user is not authorizes, you can use some default group_id value to show only posts, allowed for everyone to view.
For complex conditions like the ones you have I'd recommend using UNION if you had mysql, but looks like you will simply need to do multiple queries. First, separate all possible different conditions on which content groups should be available to a specific user.
For example: if user can only see content from his group, his friends and people he follows, you can simply do 3 requests (get content list of his group, posts of people he follows and posts of his friends) and then combine that data with PHP and output it. Since all the data is the same (as you said - it all is in posts table) you won't need anything fancy here. Just three request and sorting the way you want on php side.
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
Is there a way that will allow me to limit the amount of profiles a user can look at on my site per day. so for instance each user has an id 1, 2, 3 etc. and if the one user views 5 profiles all together in one day then it stops them viewing any more and redirects them to a sign up page to become a paid member where they can view unlimited profiles?
I'm quite new to php and sql but this is primarily what i am working in if there's a way to do it in that.
Thanks
At your database create 1 table called user_views with 3 fields
id (auto increament), user_id (the user who is visiting), visited_user_id (the user id who is visited)..
At your user_details page which users see other user, at the start of your code set 1 function which will add that view to this table , if user has already visited this user it must ignore it and if user has make all allowed visits this function will redirect him ...
And the db table must be truncate each day at 00:00:00..
I think a simple use of SESSION variables and a counting mechanism would suffice.
yes you can limit them in viewing,
you can save it in database and every time they view each profile then save it into ur database, you can make a table,
tbl_user_view
field: user_id, view_count
every profile view make an update function wich
UPDATE tble_user_view set view_count = view_count + 1 WHERE user_id = user_id
and here's some twist, if the user viewed the same profile twice you need to record it as 1,
so save the id of the profile that they viewed in COOKIES or if you do not care on database load you can save it there.
Yes. Each time the user views a profile, store that information (viewer, who they viewed, a time stamp).
Every time they go to view a profile, check how many profiles they have viewed today. If it's over the limit, redirect them.
There's other logic you may want to consider, for example, if they come back to the same profile 5 times, does it count each time?