Welcome Guest, Not a member yet? Register   Sign In
Html table plugin
#11

[eluser]xwero[/eluser]
Calendar generation isn't a situation i thought of but in a month view empty cells need to be added in the beginning when the first day of the month isn't the first day of the week.
So you need to manipulate the array before it gets 'columnized', i think it's not a big problem to pad the array at the same time to have the last row filled with the right amount of cells.
#12

[eluser]sophistry[/eluser]
hi xwero,

EDIT (5 mins after posting): added check to the code to deal with empty array.

here's what i use to generate "nested weeks" calendar array output. drop these two functions into a controller to test. so, now we've replaced the core of the table class and the core of the calendar class with functions that push the layout definitions into the view where they belong. ;-)

Code:
function calendar($m='',$y='')
    {
        echo '<pre>';
        print_r($this->_nested_weeks($m,$y,'[EMPTY]'));
        echo '</pre>';
    }
    
    function _nested_weeks($m,$y,$empty_value='&nbsp;')
    {
        // calendar columns
        $col = 7;
        // get timestamp of the first
        // day of this month and year
        $ts = mktime(0, 0, 0, $m, 1, $y);
        // generate array from the
        // number of days in month
        $arr = range(1,date('t',$ts));
        // get the day num of the
        // first day of the month
        $day_of_week = date('w', $ts);
        // pad the beginning of the array
        // based on day_of_week, negative keys
        $front_pad = array();
        $r = ($day_of_week)?range(-$day_of_week,-1):$front_pad;
        // flip keys, value to empty_value
        foreach($r as $k => $v)
        {
            $front_pad[$v] = $empty_value;
        }
        // put the lead-in days on
        $arr = $front_pad+$arr;
        // chunk out weekly arrays
        $ca = array_chunk($arr,$col);
        // pad final array element
        $ca[] = array_pad(array_pop($ca),$col,$empty_value);
        
        return $ca;
    }




Theme © iAndrew 2016 - Forum software by © MyBB