I want to display the number of new(unseen) guest entries in the guest book of a user. I was thinking to count it so that, total number of entries in guestbook minus entries in guestbook at time of last login. However, I think that is not a good approach, because if the user logs in but does not go to his guestbook, in that case, the entries should be still "new", also if some new entry is posted in the user while he is online, it does not work in that case too. Any suggested please?
You could save the time when the user last entered the guestbook, and display the number of new entries since that time.
You can have a column which indicates when entry is marked as seen/unseen and group the total number of entries on this column.
Edit after reading the comments: if your messages have an autoincrement id column you may save the max id of the unseen entries in the guestbook when the user visits the book. Initially it would be zero. Further a "new" entry will have an id > of the saved max id. If this is not the case you may consider using timestamps - the last time the user entered the guest-book compared to the message timestamp. I believe that the messages should have at least timestamp.
The question sounds too general to me. but you probably need to have a flag per entry; it gives the client the opportunity to set some entries to unseen, if he wants.
Related
I created a sort of voting website with PHP and a MYSQL database. People can vote on id's that are in the database and the amount of likes goes up.
When you click on a button the likes of that specific id goes up by one. This is done through Ajax and sql.
Is it possible to set a limitation of the amount of likes an id can have for each day. For instance each id can update only 10 times each day. And the next day another 10 times.
Yes, you can put condition in ajax code.
Before inserting record in DB for vote first check the total number of vote received for that ID on current day.
If it is equal to 10 then skip the operation or else increase the vote.
You have to put this conditions in this file /10jaar/views/submissions.php
Not a big deal i think so.
I'm trying to learn how to code a website algorithm like Reddit.com where there are thousands of posts that need to be ranked. Their ranking algorithm works like this (you don't have to read it, its more of a general question that I have): http://amix.dk/blog/post/19588
Right now I have posts stored in a database, I record their dates and they each have an upvotes and downvotes field so I'm storing their records. I want to figure out how do you store their rankings? When specific posts have ranking values, but they change with time, how could you store their rankings?
If they aren't stored, do you rank every post every time a user loads the page?
When would you store the posts? Do you run a cron job to automatically give every post a new value every x minutes? Do you store their value? Which is temporary. Maybe, until that post reaches its minimum score and is forgotten?
I did actually read the explanation of the ranking system and if I'm correct they do not care about the current time, but the time of submission of the post. This means the score will change on two points; 1) when the post is submitted, 2) when someone up- or downvotes the post
So you have to (re-)calculate the score when you post something, and when someone up- or downvotes. To recalculate the score isn't that heavy for the server (not at all actually), so just recalculate on vote-changes!
You would most probably use some sort of cache. In addition to post_time, up_votes, and down_votes, you'd have a current_rank, and last_ranked. If last_ranked would be more than say, 20 minutes, you'd rank it again, and if not, display the cached rank.
A different approach would be to rank and save the rank (i.e. current_rank) every time the post is upvoted or downvoted, as well as periodically (every X minutes), then you can just ask the database for the rank, at it would be (pretty) up to date.
I know, the title does not sound good however I will explain it clearly. In mySQL table there is a column named expires. It will hold a date in the future that will be checked against current time of the server every time a user logs in.
So, when that day is passed and the user logs in, he will get a message. There are two user roles, number 2 and number 3. Number 3 will never have an ending period. Number 2 have "trial" accounts.
I know how to set the value for users with role 2 and make the check against time() on logging, but what is the value I should place in column expires on users with role 3 and how can I make the check against time for them when logging? Maybe a time at 2020 does the trick but isn't it kind of silly ?
I would probably just store NULL for the non-expiring accounts. When validating them, first test if the column value is null, in which case you have a type 3, then check if the date has passed, wherein you have an expired type 2.
You can save some programming on the application side if you stored it as a date far far in the future, but a NULL makes more logical sense to me.
I'm about to create a new competition site, where users can upload pictures/vote.
I want to add some achievement bonuses/badges based on some very easy "achievements".
Achievement examples:
Login for 5 consecutive days --
Login for 10 consecutive days --
Login for 20 consecutive days
Vote 5 pictures (not yours) --
Vote 5 pictures for 5 consecutive days
I want to be able to add more achievements without adding too much code later on. A basic rule engine will be created.
Now.. I am having some troubles trying to think out how to do it. Anyone did something similar and wants to share database structure/coding examples, or someone with a good idea for this?
Read about 50 different threads on the topic here on SO, but couldn't find anything usable.
Make a new field in the table that will contain the date of the last activity (probably name it like lastactivity), and then create another one and name it something like consecutive. After that, each time the user login (or vote), check the last activity date and if it's yesterday, increment the value of the field consecutive by one and update the lastactivity field. Otherwise, reset it to 1.
I'm building an instant win action for a competition draw. Basically at a given randomly selected minute of an hour, the next user to submit their details should be chosen as the winner.
The problem I'm having is that if MySQL is running multiple connections, then how do I stop two or three winners being drawn by mistake? Can I limit the connections, or maybe get PHP to wait until all current MySQL connections are closed?
have a look at lock tables
http://dev.mysql.com/doc/refman/5.0/en/lock-tables.html
Use the starting value of a field that you're updating as the concurrency check in your WHERE clause when you make the update. That way, only the 1st one to be executed will go through, because after that it will no longer match the WHERE clause. You can tell whose went through using the mysql_affected_rows() function, which will return 1 for the successful update and 0 for any others.
Use a timestamp field "registertime" in the user details table. When inserting the data, use the NOW() function to insert into the registertime field.
When you choose a random minute, convert that to a unix time.
The winner is: SELECT * FROM userTable WHERE registertime > -- the timestamp of your random minute -- ORDER BY registertime LIMIT 1
Keep a small status table somewhere that records the hour of the previous draw. If the new record's insert time is at a different hour, do the check if they're a winner.
If they are, you update the status table with this new "Winners" draw hour, and that'll prevent any more draws being made for the rest of the hour. Though, what happens if, by chance, no one actually "wins" the draw in any particular hour? Do you guarantee a win to the last person who registered, or there just isn't a winner at all?