根据最大和最小数字

时间:2017-03-09 07:30:09

标签: php html css

我想根据最大值和最小值更改日历的日历背景颜色。

/* keep going with days.... */
for($list_day = 1; $list_day <= $days_in_month; $list_day++):
    $check_day = checkDay($list_day, date("Y"), date("m"));
    $red_start = $per_day_chu - 1;
    $red_finish = $per_day_chu;
    $yellow_start = $per_day_chu - 2;
    $yellow_finish = $red_start;
    $green_start = 0;
    $green_finish = $yellow_start;

    if($check_day > $red_start or $check_day == $red_start or $check_day >= $red_finish)
        $calendar.= '<td class="calendar-day" style="background:#F00">';
    else if($check_day > $yellow_start or $check_day == $yellow_start or $check_day >= $yellow_finish)
        $calendar.= '<td class="calendar-day" style="background:#FFED00">';
    else if($check_day > $green_start or $check_day == $green_start or $check_day <= $green_finish)
        $calendar.= '<td class="calendar-day" style="background:#518F00">';
    else
        $calendar.= '<td class="calendar-day">';

    /* add in the day number */
    if($currentDayOfMonth == $list_day) {
        $calendar.= '<div class="day-number"><font color="#F00">'.$list_day.'</font></div>';
    }
    else {
        $calendar.= '<div class="day-number">'.$list_day.'</div>';
    }

    /** QUERY THE DATABASE FOR AN ENTRY FOR THIS DAY !!  IF MATCHES FOUND, PRINT THEM !! **/
    $calendar.= str_repeat('<p>'.$check_day.'</p>',1);

    $calendar.= '</td>';
    if($running_day == 6):
        $calendar.= '</tr>';
    if(($day_counter+1) != $days_in_month):
        $calendar.= '<tr class="calendar-row">';
    endif;
    $running_day = -1;
    $days_in_this_week = 0;
    endif;
    $days_in_this_week++; $running_day++; $day_counter++;
endfor;

在上面的代码中,我试图显示3种颜色(红色,黄色和绿色)。

我将详细解释我的概念。

//示例1

如果$per_day_chu = 10$check_day = 0 to 6那么当天的背景颜色应为green,如果是$check_day = 7 or 8,则颜色应为yellow,如果$check_day = 9 or 10 1}}颜色应为red

//示例2

如果$per_day_chu = 8$check_day = 0 to 4那么当天的背景颜色应为green,如果是$check_day = 5 or 6,则颜色应为yellow,如果$check_day = 7 or 8 1}}颜色应为red

#per_day_chu$check_day值会动态更改。不是一个恒定的价值。

我尝试过使用上面的代码。但颜色显示不正确。有人请帮助我。三江源。

1 个答案:

答案 0 :(得分:1)

以下代码可以解决问题:

<?php

$per_day_chu = 8;

for($i=0; $i <= $per_day_chu; $i++){

    $first_color_count = $per_day_chu - 4; // set first color
    $second_color_count = $per_day_chu - 2;

    if($i <= $first_color_count)
        $color = 'green';
    else if($i <= $second_color_count)
        $color = 'yellow';
    else
        $color = 'red';

    echo 'Count: '.$i.' Color: '.$color.'<br>';

}



?>

更新代码:

而不是$ i,您可以使用$ check_day

 $first_color_count = $per_day_chu - 4; // set first color
    $second_color_count = $per_day_chu - 2;

    if($check_day <= $first_color_count)
        $color = 'green';
    else if($check_day <= $second_color_count)
        $color = 'yellow';
    else
        $color = 'red';