Domanda

how could I go about detecting changes in a Mysql leaderboard? For example if a user moves ahead another the file could echo out a message such as 'x moved one place and is now ahead of y'

È stato utile?

Soluzione

In absence of any existing code, the only recommendation I can make is to create a column in the leaderboard table which holds the user's previous position. Whenever the user's position is updated to a new value, simultaneously store the previous position in that column.

UPDATE leaderboard SET oldposition = position, position = $new_position WHERE userid = $userid;

You can also create a column which holds a boolean indicating whether or not the user has been informed of this change. The first time the message has been displayed, the boolean is set to 1. After that you will not need to display it for the user anymore until it changes.

UPDATE leaderboard
  SET 
   oldposition = position, 
   position = $new_position, 
   notified = 0
WHERE userid = $userid;

After displaying the new change, flag it as notified so subsequent pageviews don't show the same message.

UPDATE leaderboard SET notified = 1 WHERE userid = $userid;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top