I am creating a Level up System for a wordpress blog. I believe that this issue is more towards Php and maths rather than wordpress which is why im asking the question here.
I want users to gain 'levels' when the ratings (or you can call it xp) custom field gets to a multiple of 25 ( Although I would prefer to have the xp requirement increase exponentially, please look at request #2 below).
I have this in my functions.php file.
function movie_level() {
if ( is_single() ) {
global $post;
$movie_level = get_post_meta( $post->ID, "movie_level", true);
if ( !isset( $movie_level ) OR empty( $movie_level ) OR !is_numeric( $movie_level ) ) {
$movie_level = 1;
add_post_meta( $post->ID, "movie_level", $movie_level );
}
$vmeta = get_post_meta( get_the_ID(), 'ratings_score', true );
$movie_percentage_check = $vmeta / ( 25*$movie_level ) * 100;
if ( $movie_percentage_check == 100 ) {
$new_level = $movie_level + 1;
update_post_meta( $post->ID, "movie_level", $new_level );
$update_rating = $vmeta + 1;
update_post_meta( $post->ID, "ratings_score", $update_rating );
} return $movie_level;
}
}
What it does is add +1 to movie_movie level when ratings reach 25.
In my single.php file i have the following:
$meta = get_post_meta( get_the_ID(), 'ratings_score', true );
$level= movie_level();
$perc = $meta / ( 25 * $level ) * 100;
echo '<div class="progress large-6 small-12 columns "><span class="meter" style="width:'.$perc.'%"><span></span></span></div>';
echo movie_level();
This displays the progress to the next level using a progress bar css component.
Requests
How can I make $perc start at 0% again when an new level is reached? With my current equation the progress bar will always show progress starting from level 1 - level 3(for example), rather than level 2 - level 3.
How can I update my code so that the ratings requirement increases exponentially for each level and still achieve what I wanted in request #1