开发者

update multiple keys in array representing progressbars in different speeds

i have 9 progress bars. generated from an array like this:

$progress = "100-30-0-0-0-0-0-0-0";
$array= explode("-", $progress ); 

I need to update the progress bar in different speeds. This is how I do it now:

if($array[0] >= 100){  $array[0]  = 100; }else{ $array[0] = $array[0] + rand(3,8); }    
if($array[0] >= 75){if($array[1] >= 100 ){ $array[1] = 100; }else{ $array[1] = $array[1] + rand(2,7); }} 
if($array[1] >= 75){if($array[2] >= 100 ){ $array[2] = 100; }else{ $array[2] = $array[2] + rand(2,5); }} 
if($array[2] >= 75){if($array[3] >= 100 ){ $array[3] = 100; }else{ $array[3] = $array[3] + rand(1,4); }} 
if($array[3] >= 75){if($array[4] >= 100 ){ $ar开发者_如何学Pythonray[4] = 100; }else{ $array[4] = $array[4] + rand(1,3); }} 
if($array[4] >= 75){if($array[5] >= 100 ){ $array[5] = 100; }else{ $array[5] = $array[5] + rand(1,3); }} 
if($array[5] >= 75){if($array[6] >= 100 ){ $array[6] = 100; }else{ $array[6] = $array[6] + rand(1,2); }} 
if($array[6] >= 75){if($array[7] >= 100 ){ $array[7] = 100; }else{ $array[7] = $array[7] + rand(0,2); }} 
if($array[7] >= 75){if($array[8] >= 100 ){ $array[8] = 100; }else{ $array[8] = $array[8] + rand(0,1); }} 

if($array[0] > 100 ){ $array[0] = 100;}
if($array[1] > 100 ){ $array[1] = 100;}
if($array[2] > 100 ){ $array[2] = 100;}
if($array[3] > 100 ){ $array[3] = 100;}
if($array[4] > 100 ){ $array[4] = 100;}
if($array[5] > 100 ){ $array[5] = 100;}
if($array[6] > 100 ){ $array[6] = 100;}
if($array[7] > 100 ){ $array[7] = 100;}
if($array[8] > 100 ){ $array[8] = 100;}

$progress  = implode("-", $array); 

This updates the first progress bar up to 75 %, 3 to 8% a the time before the next progress bar start increasing, in slower and slower rate.

Now I need a loop to update an endless amount of progress bars in this way.


You're using way too many if statement, making the whole thing manual. You should be doing it in a loop.

Here's an example, that uses your arguments to rand (how are they generated? I've just hardcoded your ones, and default to rand(0,1) if its beyond 9.

This loops through the progress values and if one is less than 100 applies the random addition, never more than 100, and then breaks.

There's a next link I used for testing.

<?php
$progress = isset($_GET['progress'])?$_GET['progress']:"0-0-0-0-0-0-0-0-0";
$array= explode("-", $progress ); 

$randArgs = array(
    array(3,8),
    array(2,7),
    array(2,5),
    array(1,4),
    array(1,3),
    array(1,3),
    array(1,2),
    array(0,2),
    array(0,1),
);

foreach($array as $key => $progressbar) {
    if ($progressbar < 100) {
        $limits = isset($randArgs[$key]) ? $randArgs[$key] : array(0,1);
        $nextValue = $progressbar + rand($limits[0], $limits[1]);
        $array[$key] = $nextValue > 100 ? 100 : $nextValue;
        break;
    }
}

$progress  = implode("-", $array);

var_dump($progress);

?>
<a href="?progress=<?php echo $progress?>">Next</a>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜