P
P
Preci2016-03-04 19:24:43
PHP
Preci, 2016-03-04 19:24:43

System of "levels (exping)" for wordpress. What is the best way to organize data storage in mysql?

Hello!
I don’t know how to formulate the essence of the question more correctly, I’ll try to explain what I want to do:
I have 2 functions for converting exp to level and level to exp:

//Кол-во опыта на уровень
function getExpLevel($level) {
return pow($level, 10/7) * 100;
}
//Уровень по опыту
function getLevelExp($exp) {
return pow($exp / 100, 7/10);
}

With their help, passing experience / level into them, I found out what level the user has and how much experience is needed for a particular level. That's not the point.
I want to display on the page the TOP users by experience gained for the week and I have a problem with this.
How can I organize data storage in the database?
For example, make 2 tables:
1. Table with user_id | total_exp (total experience of each user)
2. User_id table | date | exp (how much experience each user gained per day)
It turns out that from the 1st table I will take data to display in the user's profile, etc., where his level will be shown (calculated by experience)
From the 2nd table, I will summarize the amount of experience of each user for the last week, group by user_id and display sorted by experience gained. But, I will need to display data not for the last 7 days, but starting from the previous Monday, i.e.:
* If today is Monday, then I show the TOP by experience for the previous Mon - Sun.
* If today is Tuesday, Wednesday...Sunday - I show the TOP for the period from the current Monday to the current day.
* In the user's profile, I want to show his current place in the TOP
Question:
Everything marked with * can I calculate for the table structure I proposed? Can you suggest me some more rational solution?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor Vorotnev, 2016-03-04
@HeadOnFire

Everything can be (and reasonably) implemented with built-in tools.
1. Store data in wp_usermeta table
2. Use WP_User_Query , namely meta_query to get data by levels/experience + date_query to group and sort results by period.
PS: This is the case if your user-players are also accounts (users) of WordPress. If not, for example the player is a custom post type - then use WP_Query.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question