Reputation: 1175
I have a projectstatus in my mysql database. Depending on the status, it's 1, 2 or 3. Also I have a css status bar of which I can set the width of, to see the progress.
Here's the example code:
<td>" . $projstatid ."
<div class='meter-wrap'>
<div class='meter-value' style='background-color: #0a0; width: 33,3%;'>
<div class='meter-text'></div>
</div>
</div>
</td>
Now I can do 3 if statements at the width's size like:
if ($projstatid) = 1 { echo "33.3%"; } elseif {..} else {...}
But how do I write a simple loop which loops through the numbers untill it reaches the $projstatid
's value, but every time it re-loops adds 33.3%?
Even better would be if the loop devided the amount time of loopings by the max amount of projstatids. But that I'll figure out, once I have this basic loop working.
EDIT
What I'm trying to do is to create a loop which does this:
x=1
y=0
$projectstatid = $record['projectstatid'] (in this example lets say it's 5)
loop: x=1, is it 5? Nope. return: y= +20%
loop: x=2, is it 5? Nope. return: y= +20%
etc.
untill it reaches 5 (and it will return 100%)
Upvotes: 0
Views: 552
Reputation: 76395
Instead of incrementing $x
until $x == $projectstatid
, you could use a switch
(which is sort of a loop):
switch ($projectstatid)
{
case 1:
$y = '20%';
break;
case 2:
$y = '40%';
break;
//etc, until the max is reached, in which case:
default:
$y = '100%';
}
See the docs for all info concerning the switch
statement.
Also if this: if ($projstatid) = 1 { echo "33.3%"; } elseif {..} else {...}
is what your code looks like, it's no surprise that it keeps echo-ing 33%
, because =
is the assignment operator, and assigning to a variable will almost always return true. Also, your parentheses are not where they should be, what it should look like is this:
if ($projstatid == 1)
{
echo "33.3%";
}
else if ($projstatid == 2)
{..}
else {...}
Upvotes: 1
Reputation: 4517
<?php $meter_val = 0; ?>
<?php if ($projstatid <= 5) $meter_val = ($projstatid * 20);
<td>" . $projstatid ."
<div class='meter-wrap'>
<div class='meter-value' style='background-color: #0a0; width: <?php echo $meter_val; ?>%;'>
<div class='meter-text'></div>
</div>
</div>
</td>
Upvotes: 0
Reputation: 25552
You can do something like this to compute the percent corresponding to your current value :
$percent = floor($number * 100 / $nbmax);
Here $percent
is the value to put in your css and $number
is your current status and $nbmax
is the maximum value that it could have.
In your case your would have :
$number = $record['projstatid'];
$nbmax = 3;
$percent = floor($number * 100 / $nbmax);
EDIT: Based on your edit I guess you want something like this:
$number = 5;
for($i = 1 ; $i < $number; $i++) {
$percent = floor($i * 100 / $number);
echo $percent;
}
Upvotes: 2