L
L
Lyuba2018-04-02 12:33:04
PHP
Lyuba, 2018-04-02 12:33:04

How to fix a bug in the code?

Can you please tell me how to fix the code? My $ost variable displays how many days are left until the announcement expires, but it does not work correctly.
The idea is 30 days green, 20 days orange, 10 red.
The code itself is:

?>
  <div class='progressBlock'>
    <div class='percent'></div>
</div>


<style>
.progressBlock {
    width: 100px;
    height: 10px;
    border-radius: 3px;
    border: 1px solid black;
    position: relative;
}
 .percent{
    position: absolute;
    <?
     
        //$ost = 30; //Осталось 10 дней это для примера так работает
    $total = 30; //Всего 30 дней
    $p = $ost * 100 / $total; // Получим 33%
    echo "width: $p%;\n";
    $color = "lime";
        if ($ost >= 10 and $ost < 20) $color = "yellow";
        if ($ost < 10) $color = "red";
        echo "background-color: $color;";
      
    ?>
    height: 10px;
}
</style>
 <?

5ac1f89de8cc3569781895.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sanovskiy, 2018-04-02
@Sanovskiy

switch (true){
    case $ost>=10 && $ost <20:
        $color = "yellow";
        break;
    case $ost<10:
        $color = "red";
        break;
    default:
        $color = 'green';
        break;
}

As an option like this

D
Dmitry, 2018-04-02
@slo_nik

Good afternoon.
I'll put in my five cents :)

$ost = 9;
switch (true){
    case $ost >= 10 && $ost < 20:
        $color = "yellow";
        break;
    case $ost < 10:
        $color = "red";
        break;
    case $ost > 20:
        $color = 'green';
        break;
}
?>
<div style="color:<?= $color ?>">test</div>

Default в данном случае, я думаю, лишний. Хотя должно работать и с default, так что проверяйте содержимое переменной $ost. И вообще, существует ли переменная $ost? Где Вы её инициализируете?
$ost = 45;
$p = $ost * 100 / $total;
// далее остальной код

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question