0

So the goal is to be able to create a dynamic value for my progress bar based on dynamic variables from the database. Below is a pseudo code for what i am looking for if its possible:

//League rank names
['bronze_V','bronze_IV','bronze_III','bronze_II','bronze_I'];

Lets say i start at bronze_IV to bronze_II i need to calculate that to 100?
and then i need to store that value is that possible to be dynamic?
1
  • 1
    Yes this can be done, no not with the snippet you have provided. If you want help, you will need to provide more information. Where is the numeric data? Commented Oct 26, 2015 at 11:29

1 Answer 1

0

are you trying something like this,

$data = ['bronze_V','bronze_IV','bronze_III','bronze_II','bronze_I'];

$start = $end = 0;

while($element = current($data)) {
    if ($element == 'bronze_IV') $start = key($data);
    if ($element == 'bronze_I') $end = key($data);
    next($data);
}

$percentage = ( (($end-$start) + 1) / count($data) ) * 100;

var_dump($percentage); //float(80)

example with multiple tiers,

#note : badge count for each tier has to be 5 for this to work, 
#if you change the count change change the value inside the loop

$data = array(
        ['silver_V','silver_IV','silver_III','silver_II','silver_I'],
        ['bronze_V','bronze_IV','bronze_III','bronze_II','bronze_I']
    );

$start = $end = 0;

$start_badge = 'silver_V';
$end_badge = 'bronze_II';

//loop through tiers
foreach ($data as $key => $tier) {
    //loop through badges
    while($badge = current($tier)){
        //position of the start badge
        if ($badge == $start_badge) $start = ($key * 5) + key($tier)+1; //see note 
        //position of the end badge
        if ($badge == $end_badge) $end = ($key * 5) + key($tier)+1; //see note
        next($tier);
    }
}

//count badges
$badges_count = (count($data, COUNT_RECURSIVE) - count($data));

//calculate percentage
$percentage = ( (($end-$start) + 1) / $badges_count ) * 100;

var_dump($percentage); //float(90)
Sign up to request clarification or add additional context in comments.

5 Comments

this looks promising ill give it a try
Can you please iterate how i would be able to modify the above to contain the entire league tiers bronze,silver,gold,platinum and diamond. Please comment each part for me thanks :)
@Elevant are those different array, each with 5 levels?
yes each has 5 levels however they could all be on the same array if needed
So for example say i have the following values: start=bronze_I current=silver_II end=silver_I would the above still be used?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.