What does the portion of the code mean?

Discussion in 'PHP' started by saadi123, Feb 17, 2014.

  1. #1
    I was reading a code about PHP calendar and couldn't understand a small portion of it. Blow is the code.

    
    <?php
    /* Draws a calendar*/
    function draw_calendar($month, $year) {
        /* Draw table*/
        $calendar = '<table cellpadding="0" cellspacing="0" class="calendar">';
       
        /*Table heading*/
        $headings = array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
        $calendar .= '<tr class="calendar-row"><td class="calendar-day-head">'.implode('</td><td class="calendar-day-head">',$headings).'</td></tr>';
        /*row for week one*/
        $calendar .= '<tr class="calendar-row">';
        /*Days and weeks vars now*/
        $running_day = date('w',mktime(0,0,0,$month,1,$year));
        $days_in_month = date('t',mktime(0,0,0,$month,1,$year));
        $days_in_this_week = 1;
        $day_counter = 0;
        $dates_array = array();
       
        /*Print blank days until first of the current week*/
        for ($x=0;$x<$running_day;$x++) {
            $calendar .= '<td class = "calendar-day-np"> </td>';
            $days_in_this_week++;
        }
       
        /*Keep going with days...*/
        for($list_day=1;$list_day<=$days_in_month;$list_day++) {
            $calendar .= '<td class = "calendar-day">';
            /*Add in the day number*/
            $calendar .= '<div class = "day-number">' . $list_day. '</div>';
           
            /*Query database for an entry on this day. If found, print them on them*/
            $calendar .= str_repeat('<p> </p>', 2);
           
            $calendar .= '</td>';
            if ($running_day == 6) {
                $calendar .= '</tr>';
                if (($day_counter+1) != $days_in_month) {
                  $calendar .= '<tr class =" calendar-row">';
                }
                $running_day = -1;
                $days_in_this_week = 0;
            }
            $days_in_this_week++; $running_day++; $day_counter++;
        }
       
        /*Finish the rest of the days in the week*/
       
        if($days_in_this_week < 8) {
            for ($x=1;$x<=(8-$days_in_this_week); $x++){
                $calendar .='<td class ="calendar-day-np"> </td>';
            }
        }
       
        /*Final row*/
        $calendar .= '</tr>';
       
        /*End of table*/
        $calendar .= '</table>';
       
        /*all done, return result*/
        return $calendar;
    }
    
    Code (markup):
    The first thing is that why the $dates_array variable has been created?
    What is this portion of the code is doing?

    
    if ($running_day == 6) {
                $calendar .= '</tr>';
                if (($day_counter+1) != $days_in_month) {
                  $calendar .= '<tr class =" calendar-row">';
                }
                $running_day = -1;
                $days_in_this_week = 0;
            }
            $days_in_this_week++; $running_day++; $day_counter++;
        }
    
    Code (markup):
    The most important thing I want to understand is that at the end of the if statement, why the variables are being incremented to one.

    Thank you.
     
    Solved! View solution.
    saadi123, Feb 17, 2014 IP
  2. #2
    This is the codebit that makes the table-rows stop and start over when it reaches the end of a week. The counters are a bit... weird, and such, but it's pretty straight forward. A count for running-day from -1 to 5 gives 7 (-1,0,1,2,3,4,5) and the day-counter counts the total of days passed in the current month, and checks if there are more days in the current month. The days_in_this_week-variable is being used to fill out the last row with empty <td>s after the last date is displayed.

    Pretty straight forward calendar-stuff, but the numbers chosen are a bit weird, and could have been done easier.

    EDIT: oh, yeah, and the $dates_array is declared, but not used - I dunno why, probably something that's put there for a "might be useful" or used by some other function not present in the current code
     
    PoPSiCLe, Feb 17, 2014 IP