Jithin Varghese
Jithin Varghese

Reputation: 2228

change color according to max and min numbers

I want to change the calendar background color for day according to max and min values.

/* 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;

In the above code I have tried to display the 3 colors (red, yellow and green).

I will explain my concept in detail.

//Example 1

If $per_day_chu = 10 and $check_day = 0 to 6 then the background color for the day should be green and if $check_day = 7 or 8 then the color should be yellow and if $check_day = 9 or 10 the the color should be red.

//Example 2

If $per_day_chu = 8 and $check_day = 0 to 4 then the background color for the day should be green and if $check_day = 5 or 6 then the color should be yellow and if $check_day = 7 or 8 the the color should be red.

#per_day_chu and $check_day values will change dynamically. not a constant value.

I have tried using the above code. But the color not displaying correctly. Anyone please help me. Thankyou.

Upvotes: 0

Views: 102

Answers (1)

Happy Coding
Happy Coding

Reputation: 2525

Code below did the trick :

<?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>';

}



?>

Updated Code :

Instead of $i, you can use $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';

Upvotes: 1

Related Questions