Results 1 to 14 of 14

Thread: Simplify code to display time table

  1. #1

    Simplify code to display time table

    Hey guys, still working on the event calendar class. There is this function that prints a week and time for each day from 6am-10pm. I just finished writing that part, but I'm going to keep on staring at the code to find ways to simplify the code even more.

    If you have some suggestions on how to simplify it please let me know.

    Here is the code that prints the cells for the time:
    PHP Code:
            // >>>>>>>>>>>>>>>>>>    TIME    <<<<<<<<<<<<<<
            // this time array is used for printing rows, one row for 00 and 15 minutes, and another row for 30 and 45 minutes
            
    $time=array();
            for(
    $i=6$i <= 22$i++){
                
    $time[] = strtotime('+'.$i.' hour 12AM');
                
    $time[] = strtotime('+'.$i.' hour 30 minutes 12AM');
            }
            
            
    // loop hours
    //        for($i=6; $i <= 22; $i++){
            
    foreach($time as $key => $fifteen_increments){
                
    $hour date('H'$fifteen_increments);
                
    $hour_minutes date('H:i'$fifteen_increments);
                
    $fifteen_increments = ($hour_minutes != '12:00') ? date('g:i A'$fifteen_increments) : 'Noon';

                
    // a row, every half-hour
                
    if(date('i'strtotime($fifteen_increments))=='00'){
                    
    $complete .= '<tr><th scope="row">'.$fifteen_increments.'</th>';
                }
                
    // a row, every half-hour
                
    if(date('i'strtotime($fifteen_increments))=='30'){
                    
    $complete .= '<tr><th scope="row"></th>';
                }

                
    $running_day='';
                
    $running_day_15='';
                
    $running_day_30='';
                
    $running_day_45='';

                
    // loop days
                
    for($j=0$j<7;$j++){
                    
    $insert_content '';
                    if(
    substr($hour_minutes, -2)==00)
                    {
                        
    $running_day date('Y-m-d'strtotime('+'.$j.' day '.$sunday_legible)).' '.$hour_minutes.':00';
                        
    $running_day_15 date('Y-m-d'strtotime('+'.$j.' day '.$sunday_legible)).' '.$hour.':15:00';
                    }
                    

                    if(
    substr($hour_minutes, -2)==30)
                    {
                        
    $running_day_30 date('Y-m-d'strtotime('+'.$j.' day '.$sunday_legible)).' '.$hour.':30:00';
                        
    $running_day_45 date('Y-m-d'strtotime('+'.$j.' day '.$sunday_legible)).' '.$hour.':45:00';
                    }

                    foreach(
    $events as $key => $value){
                        if(
    substr($hour_minutes, -2)==00){
                            if(
    $running_day==date('Y-m-d H:i:s'strtotime($value[0])) or $running_day_15==date('Y-m-d H:i:s'strtotime($value[0])) ){
                            
    $insert_content .= '<span class="event">'.date('g:i A'strtotime($value[0])).'<br />'.$value[1].'</span>';
                            }
                        }
                        if(
    substr($hour_minutes, -2)==30){
                            if(
    $running_day_30==date('Y-m-d H:i:s'strtotime($value[0])) or $running_day_45==date('Y-m-d H:i:s'strtotime($value[0])) ){
                            
    $insert_content .= '<span class="event">'.date('g:i A'strtotime($value[0])).'<br />'.$value[1].'</span>';
                            }
                    }
                }

                
    $running_day='';
                
    $running_day_15='';
                
    $running_day_30='';
                
    $running_day_45='';

                
    $complete .= '<td>'.$insert_content.'</td>';
                }
                
    $complete .= '</tr>';
            }
            
    $label .= '</tr>';
            
    $content .= '</tr>'
    Last edited by imagined; November 10th, 2009 at 04:05 PM.
    "You are playing a very dangerous game"
    "Change always is"

    Creativo Development

  2. #2
    actionAction's Avatar
    1,142
    posts
    humanBeing._beard=true;
    This is not a big deal at all, but since you are asking:
    I'm not convinced that you need both the $key and $value parameters in your foreach as the arrays you are using don't appear to be associative and I don't see any usage of $key in your code. Have you tried using foreach($time as $fifteen_increments)?

    Also, there are some repetitive statements that should be encapsulated into a single variable. Namely this statement which occurs in multiple conditionals:
    PHP Code:
    date('Y-m-d H:i:s'strtotime($value[0])) 
    To gain efficiency, put the value of this function call into a variable before the conditions and check against the variable. Same with:
    PHP Code:
    substr($hour_minutes, -2
    One last thing, someone at some point mentioned your outputting html content within the functions. You could clean this up with a function that generates the html (it will make your code much more readable):
    PHP Code:
    private function _html($tag$content$attribs=array()){
        
    $attribute_string = array();
        foreach(
    $attribs as $attribute => $value){
            
    $attribute_string[] = "$attribute=\"$value\"";
        }
        
    $attribute_string implode(" "$attribute_string);
        
    $html "<$tag $attribute_string>$content</$tag>";
        return 
    $html;
    }    

    echo 
    _html('p''Hello there', array('id' => 'unique')); 

  3. #3
    actionAction's Avatar
    1,142
    posts
    humanBeing._beard=true;
    This should be much more efficient, I removed all redundant function calls (I use the HTML function mentioned above).
    PHP Code:
    // >>>>>>>>>>>>>>>>>>    TIME    <<<<<<<<<<<<<<
            // this time array is used for printing rows, one row for 00 and 15 minutes, and another row for 30 and 45 minutes
            
    $time=array();
            for(
    $i=6$i <= 22$i++){
                
    $time[] = strtotime('+'.$i.' hour 12AM');
                
    $time[] = strtotime('+'.$i.' hour 30 minutes 12AM');
            }
            
            
    // loop hours
            
    foreach($time as $fifteen_increments){
                
    $hour date('H'$fifteen_increments);
                
    $hour_minutes date('H:i'$fifteen_increments);
                
    $fifteen_increments = ($hour_minutes != '12:00') ? date('g:i A'$fifteen_increments) : 'Noon';
                
    $fifteen_time date('i'strtotime($fifteen_increments));
                
                
    // a row, every half-hour
                
    if($fifteen_time == '00'){
                    
    $complete .= '<tr><th scope="row">'.$fifteen_increments.'</th>';
                }
                
    // a row, every half-hour
                
    if($fifteen_time == '30'){
                    
    $complete .= '<tr><th scope="row"></th>';
                }

                
    $running_day='';
                
    $running_day_15='';
                
    $running_day_30='';
                
    $running_day_45='';

                
    // loop days
                
    for($j=0$j<7;$j++){
                    
    $insert_content '';
                    
    $minutes_place substr($hour_minutes, -2));
                    
    $running_day_holder date('Y-m-d'strtotime('+'.$j.' day '.$sunday_legible));
                    
                    if(
    $minutes_place == 00)
                    {
                        
    $running_day     $running_day_holder.' '.$hour_minutes.':00';
                        
    $running_day_15 $running_day_holder.' '.$hour.':15:00';
                    }
                    

                    if(
    $minutes_place == 30)
                    {
                        
    $running_day_30 $running_day_holder.' '.$hour.':30:00';
                        
    $running_day_45 $running_day_holder.' '.$hour.':45:00';
                    }

                    foreach(
    $events as $value){
                        
    $running_day_time date('Y-m-d H:i:s'strtotime($value[0]));
                        
    $event_date date('g:i A'strtotime($value[0]));
                        
    $content_to_insert $this->_html('span'$event_date.'<br />'.$value[1], array('class'=>'event'));
                        
                        if(
    substr($minutes_place == 00){
                            if(
    $running_day == $running_day_time or $running_day_15 == $running_day_time){
                            
    $insert_content .= $content_to_insert
                            
    }
                        }
                        if(
    $minutes_place == 30){
                            if(
    $running_day_30 == $running_day_time or $running_day_45 == $running_day_time){
                            
    $insert_content .= $content_to_insert
                            
    }
                    }
                }

                
    $running_day='';
                
    $running_day_15='';
                
    $running_day_30='';
                
    $running_day_45='';

                
    $complete .= '<td>'.$insert_content.'</td>';
                }
                
    $complete .= '</tr>';
            }
            
    $label .= '</tr>';
            
    $content .= '</tr>'
    You could get this all working with the HTML function by nesting successive calls to the $content parameter like this:
    PHP Code:
    $this->_html('tr'$this->_html('td'$content), array('scope'=>'row')); 
    Let me know if you have any questions.
    Last edited by actionAction; November 10th, 2009 at 01:21 PM.

  4. #4
    Hey thanks a lot! I think it does make a big difference:
    • If I try to change the function weeks or months later it'll be way easier to understand
    • Shaving function calls, if a few hundreds or thousands(hopefully) use the app, it may make an impact on performance.


    You guys are right about outputting HTML from the function. I will look more closely into it once the basic functions of the class work. That's why the calendar looks so bad right now, I'm leaving the actual look of it to the end.
    "You are playing a very dangerous game"
    "Change always is"

    Creativo Development

  5. #5
    actionAction's Avatar
    1,142
    posts
    humanBeing._beard=true;
    No problem imagined, it should make a pretty big difference in performance. I agree that you should leave the look to the end, but that should be the css. You should have a plan about output as you build it up to keep it flexible. I rewrote the foreach (which is the main part of the posted code) to use the html function, though I can't test it.
    PHP Code:
    // loop hours
            
    foreach($time as $fifteen_increments){
                
    $hour date('H'$fifteen_increments);
                
    $hour_minutes date('H:i'$fifteen_increments);
                
    $fifteen_increments = ($hour_minutes != '12:00') ? date('g:i A'$fifteen_increments) : 'Noon';
                
    $fifteen_time date('i'strtotime($fifteen_increments));
                
                
    // a row, every half-hour
                
    if($fifteen_time == '00'){
                    
    $complete .= $this->_html('th'$fifteen_increments, array('scope'=>'row'));
                }
                
    // a row, every half-hour
                
    if($fifteen_time == '30'){
                    
    $complete .= $this->_html('th'"", array('scope'=>'row'));
                }

                
    $running_day='';
                
    $running_day_15='';
                
    $running_day_30='';
                
    $running_day_45='';

                
    // loop days
                
    for($j=0$j<7;$j++){
                    
    $insert_content '';
                    
    $minutes_place substr($hour_minutes, -2));
                    
    $running_day_holder date('Y-m-d'strtotime('+'.$j.' day '.$sunday_legible));
                    
                    if(
    $minutes_place == 00)
                    {
                        
    $running_day     $running_day_holder.' '.$hour_minutes.':00';
                        
    $running_day_15 $running_day_holder.' '.$hour.':15:00';
                    }
                    

                    if(
    $minutes_place == 30)
                    {
                        
    $running_day_30 $running_day_holder.' '.$hour.':30:00';
                        
    $running_day_45 $running_day_holder.' '.$hour.':45:00';
                    }

                    foreach(
    $events as $value){
                        
    $running_day_time date('Y-m-d H:i:s'strtotime($value[0]));
                        
    $event_date date('g:i A'strtotime($value[0]));
                        
    $content_to_insert $this->_html('span'$event_date.'<br />'.$value[1], array('class'=>'event'));
                        
                        if(
    substr($minutes_place == 00){
                            if(
    $running_day == $running_day_time or $running_day_15 == $running_day_time){
                                
    $insert_content .= $content_to_insert;
                            }
                        }
                        if(
    $minutes_place == 30){
                            if(
    $running_day_30 == $running_day_time or $running_day_45 == $running_day_time){
                            
    $insert_content .= $content_to_insert;
                        }
                    }
                }

                
    $running_day    '';
                
    $running_day_15    '';
                
    $running_day_30    '';
                
    $running_day_45    '';

                
    $complete .= $this->_html('td'$insert_content);
                }
                
    $complete $this->_html('tr'$complete);
            } 

  6. #6
    Thanks so much. I'll check the code from the latest post right now.

    In case you want to play with it, here is the class. I'm still working on the DAY view calendar, so that part won't work properly.
    PHP Code:
    class LeoCalendar
    {
        protected 
    $_workDate;
        protected 
    $_calendarType;
        protected 
    $_events;
        protected 
    $_year;
        protected 
    $_month;
        protected 
    $_day;
        protected 
    $_week;
        protected 
    $_calendar;

        
    // boolean
        // 0 = is not in this month
        // 1 = it is in this month
        
    protected $_inThisMonth;

        public function 
    __construct($date=''$calendarType='month'$events=array())
        {
            if(!isset(
    $date))
            {
                
    $date date('Y-m-d');
                
    $this->_workDate $date;
            }
            else
            {
                
    $date date('Y-m-d'strtotime($date));
                
    $this->_workDate $date;
            }
            
    // assign the values to the class properties
            
    $this->_year = ( int ) date'Y'strtotime($this->_workDate));
            
    $this->_month = ( int ) date'n'strtotime($this->_workDate));
            
    $this->_day = ( int ) date'j'strtotime($this->_workDate));

            
    $this->_calendarType $calendarType;
            
    $allowedCalendarTypes = array('month''week''day');
            if(!
    in_array($this->_calendarType$allowedCalendarTypes)){
                
    $this->_calendarType='month';
                
    // throw exception
            
    }

            if(!
    is_null($events) && !is_array($events))
            {
                throw new 
    Exception('Must be array');
                exit(
    'Error!');
            }
            
    $this->_events $events;
        }


        
    ################################################
        # PUBLIC METHODS FOR blah blah blah            #
        ################################################
        
    public function createWeekRow($enterDate$presentMonth$format='complete'$events=array()){
            if(!isset(
    $presentMonth) or $presentMonth==NULL){$presentMonth=date('m'strtotime($enterDate));}
            
    $today date('Y-m-d');
            
    $today_timestamp strtotime($today);
            
    $timestamp strtotime($enterDate);
            
    $sunday_timestamp $this->getSunday($timestamp'timestamp');
            
    $sunday_legible $this->getSunday($timestamp'legible');
            
    $year date('Y'$sunday_timestamp);
            
    $label '<tr class="dayLabel">';
            
    $content '<tr class="dayContent">';
            
    $year date('Y'$sunday_timestamp);
            
    $complete '<p>
                            <a href="'
    .$_SERVER['PHP_SELF'].'?calendarType=week&workDate='.date('Y-m-d'strtotime('-1 day '.$sunday_legible)).'">&#x2190;</a>
                             | 
                            <a href="'
    .$_SERVER['PHP_SELF'].'?calendarType=week&workDate='.date('Y-m-d'strtotime('+7 days '.$sunday_legible)).'">&#x2192;</a>
                        </p>
                        <table>
                            <caption>'
    .date('F j'$sunday_timestamp).' - '.date('F j, Y'strtotime('+6 days '.$sunday_legible)).'</caption>
                            <colgroup>
                                    <col id="time" />
                                    <col id="Sunday" />
                                    <col id="Monday" />
                                    <col id="Tuesday" />
                                    <col id="Wednesday" />
                                    <col id="Thursday" />
                                    <col id="Friday" />
                                    <col id="Saturday" />
                                </colgroup>
                                <thead><tr><th scope="col">'
    .$year.'</th>
                        '
    ;

            
    // days
            
    for($i=0;$i<7;$i++){
                
    $date date('l, M j'strtotime('+'.$i.' day '.$sunday_legible));
                
    $day date('j'strtotime('+'.$i.' day '.$sunday_legible));
                
    $today '';
                
    $running_day date('Y-m-d'strtotime('+'.$i.' day '.$sunday_legible));
                
    $running_month date('m'strtotime('+'.$i.' day '.$sunday_legible));
                
    $insert_content '';
                
    // find out a way to highlight today's date
                
    if(date('Y-m-d') == $running_day){
                    
    $today ' id="today"';
                }
                else{
                    
    $today '';
                }

                foreach(
    $events as $key => $value){
                    if(
    $running_day==date('Y-m-d'strtotime($value[0]))){
                        
    $insert_content .= '<span>'.date('g:i A'$value[0]).'<br />'.$value[1].'</span>';
                    }
                }

                
    $complete .= '<th scope="col"'.$today.'>'.$date.'</th>';
                if(
    $running_month $presentMonth){
                    
    $label .= '<td class="previous"'.$today.'>'.$day.'</td>';
                    
    $content .= '<td class="previous"'.$today.'>'.$insert_content.'</td>';
                }
                elseif(
    $running_month $presentMonth){
                    
    $label .= '<td class="next"'.$today.'>'.$day.'</td>';
                    
    $content .= '<td class="next"'.$today.'>'.$insert_content.'</td>';
                }
                else{
                    
    $label .= '<td'.$today.'>'.$day.'</td>';
                    
    $content .= '<td'.$today.'>'.$insert_content.'</td>';
                }
            }
            
    $complete .='</tr>';

            
    // >>>>>>>>>>>>>>>>>>    TIME    <<<<<<<<<<<<<<
            // this time array is used for printing rows, one row for 00 and 15 minutes, and another row for 30 and 45 minutes
            
    $time=array();
            for(
    $i=6$i <= 22$i++){
                
    $time[] = strtotime('+'.$i.' hour 12AM');
                
    $time[] = strtotime('+'.$i.' hour 30 minutes 12AM');
            }
            
            
    // loop hours
            
    foreach($time as $fifteen_increments){
                
    $hour date('H'$fifteen_increments);
                
    $hour_minutes date('H:i'$fifteen_increments);
                
    $fifteen_increments = ($hour_minutes != '12:00') ? date('g:i A'$fifteen_increments) : 'Noon';

                
    // a row, every half-hour
                
    if(date('i'strtotime($fifteen_increments))=='00'){
                    
    $complete .= '<tr><th scope="row">'.$fifteen_increments.'</th>';
                }
                
    // a row, every half-hour
                
    if(date('i'strtotime($fifteen_increments))=='30'){
                    
    $complete .= '<tr><th scope="row"></th>';
                }

                
    $running_day='';
                
    $running_day_15='';
                
    $running_day_30='';
                
    $running_day_45='';

                
    // loop days
                
    for($j=0$j<7;$j++){
                    
    $insert_content '';
                    
    $minutes substr($hour_minutes, -2);
                    if(
    $minutes==00)
                    {
                        
    $running_day date('Y-m-d'strtotime('+'.$j.' day '.$sunday_legible)).' '.$hour_minutes.':00';
                        
    $running_day_15 date('Y-m-d'strtotime('+'.$j.' day '.$sunday_legible)).' '.$hour.':15:00';
                    }
                    

                    if(
    $minutes==30)
                    {
                        
    $running_day_30 date('Y-m-d'strtotime('+'.$j.' day '.$sunday_legible)).' '.$hour.':30:00';
                        
    $running_day_45 date('Y-m-d'strtotime('+'.$j.' day '.$sunday_legible)).' '.$hour.':45:00';
                    }

                    foreach(
    $events as $value){
                        
    $event_day_time date('Y-m-d H:i:s'strtotime($value[0]));
                        
    $event_time date('g:i A'strtotime($value[0]));
                        
    $event_name $value[1];
                        if(
    $minutes==00){
                            if(
    $running_day==$event_day_time or $running_day_15==$event_day_time ){
                            
    $insert_content .= '<span class="event">'.$event_time.'<br />'.$event_name.'</span>';
                            }
                        }
                        if(
    $minutes==30){
                            if(
    $running_day_30==$event_day_time or $running_day_45==$event_day_time ){
                            
    $insert_content .= '<span class="event">'.$event_time.'<br />'.$event_name.'</span>';
                            }
                    }
                }

                
    $running_day='';
                
    $running_day_15='';
                
    $running_day_30='';
                
    $running_day_45='';

                
    $complete .= '<td>'.$insert_content.'</td>';
                }
                
    $complete .= '</tr>';
            }
            
    $label .= '</tr>';
            
    $content .= '</tr>';
            
            if(
    $format=='complete'){
                return 
    $complete.'</table>';
            }
            else{
                return 
    $label.$content;
            }
        }

        public function 
    getSunday($timestamp$format='timestamp')
        {
            
    $timestamp_legible date('Y-m-d'$timestamp);
            if(
    date('l'$timestamp)=='Sunday')
            {
                
    $sunday_timestamp $timestamp;
                
    $sunday_legible date('F j, Y'$sunday_timestamp);
            }
            else
            {
                
    $sunday_timestamp strtotime(date('r',strtotime('last Sunday '.$timestamp_legible)));
                
    $sunday_legible date('F j, Y'$sunday_timestamp);
            }
            
    $year date('Y'$sunday_timestamp);
            if(
    $format=='legible')
            {
                return 
    $sunday_legible;
            }
            else
            {
                return 
    $sunday_timestamp;
            }
        }

        public function 
    getNextSunday($timestamp$format='legible')
        {
            
    $timestamp_legible date('Y-m-d'$timestamp);
            if(
    date('l'$timestamp)=='Saturday')
            {
                
    $next_week_timestamp strtotime(date('r',strtotime($timestamp_legible.' + 7 days')));
                
    $next_week_legible date('F j, Y'$next_week_timestamp);
                
    $sunday_next_week_timestamp strtotime(date('r'strtotime('last Sunday '.$next_week_legible)));
                
    $sunday_next_week_legible date('F j, Y'$sunday_next_week_timestamp);
            }
            else
            {
                
    $next_week_timestamp strtotime(date('r',strtotime($timestamp_legible.' + 8 days')));
                
    $next_week_legible date('F j, Y'$next_week_timestamp);
                
    $sunday_next_week_timestamp strtotime(date('r'strtotime('last Sunday '.$next_week_legible)));
                
    $sunday_next_week_legible date('F j, Y'$sunday_next_week_timestamp);
            }

            if(
    $format=='legible')
            {
                return 
    $sunday_next_week_legible;
            }
            else
            {
                return 
    $sunday_next_week_timestamp;
            }
        }

        public function 
    createMonthSkeleton($date$events)
        {
        
            
    $legible_date date('l, F j,  Y'strtotime($date));
            
    $year date('Y'strtotime($date));
            
    $month date('m'strtotime($date));
            
    $day date('d'strtotime($date));
        
            
    // get first day of previous month
            // using the first day of the previous
            // month, the link to the previous month
            // is created
            
    if($month==1)
            {
                
    $previousYear $year-1;
                
    $previousMonth=12;
            }
            else
            {
                
    $previousYear $year;
                
    $previousMonth=$month-1;
            }
            
    // get first day of next month
            // using the first day of the next
            // month, the link to the next month
            // is created
            
    if($month==12)
            {
                
    $nextYear $year+1;
                
    $nextMonth=1;
            }
            else
            {
                
    $nextYear $year;
                
    $nextMonth=$month+1;
            }
        
            
    $firstDayOfMonth $year.'-'.$month.'-01';
            
    $firstDayOfPreviousMonth date('Y-m-d'strtotime($previousYear.'-'.$previousMonth.'-01'));
            
    $firstDayOfNextMonth date('Y-m-d'strtotime($nextYear.'-'.$nextMonth.'-01'));
            
    $numberOfDaysOfPreviousMonth date('t',strtotime(date('Y-m-d'strtotime($firstDayOfPreviousMonth))));
            
    $numberOfDaysOfCurrentMonth date('t',strtotime(date('Y-m-d'strtotime($year.'-'.$month.'-01'))));
        
            
    // what day of the week is the first day of the month?
            
    switch(date('l',strtotime($firstDayOfMonth)))
            {
                case 
    'Sunday':
                    
    $startTheCalendar 1;
                    break;
        
                case 
    'Monday':
                    
    $startTheCalendar $numberOfDaysOfPreviousMonth;
                    break;
        
                case 
    'Tuesday':
                    
    $startTheCalendar $numberOfDaysOfPreviousMonth-1;
                    break;
        
                case 
    'Wednesday':
                    
    $startTheCalendar $numberOfDaysOfPreviousMonth-2;
                    break;
        
                case 
    'Thursday':
                    
    $startTheCalendar $numberOfDaysOfPreviousMonth-3;
                    break;
        
                case 
    'Friday':
                    
    $startTheCalendar $numberOfDaysOfPreviousMonth-4;
                    break;
        
                case 
    'Saturday':
                    
    $startTheCalendar $numberOfDaysOfPreviousMonth-5;
            }
        
            
    $calendar '<p><a href="'.$_SERVER['PHP_SELF'].'?workDate='.$firstDayOfPreviousMonth.'">&#x2190;</a> <a href="'.$_SERVER['PHP_SELF'].'?workDate='.$firstDayOfNextMonth.'">&#x2192;</a></p>';
            
    $calendar .= '<table><caption>'.date('F Y',strtotime($date)).'</caption>';
            
    $calendar .= '<tr>';
            
    $calendar .= '<th>Sunday</th><th>Monday</th><th>Tuesday</th><th>Wednesday</th><th>Thursday</th><th>Friday</th><th>Saturday</th>';
            
    $calendar .= '</tr>';
            
            
    // does the calendar start with the previous month?
            
    if($startTheCalendar 20)
            {
                
    $startedPreviousMonth=1;
                
    $goCurrentMonth=0;
                
    $startedCurrentMonth=0;
            }
            else
            {
                
    $startedPreviousMonth=0;
                
    $goCurrentMonth=1;
                
    $startedCurrentMonth=1;
            }
            
            
    $days $startTheCalendar;
            
    $startedNextMonth=0;
            
    $goNextMonth=0;

            for(
    $w=0;$w<6;$w++)
            {
                if(
    $w==0)
                {
                    
    $week_day $firstDayOfMonth;
                }
                else
                {
                    
    $next_sunday $this->getNextSunday(strtotime($week_day));
                    
    $week_day $next_sunday;
                }
                
    $calendar .= $this->createWeekRow($week_day$month'row'$events);
            }
            
    $calendar $calendar.'</table>';
            return 
    $calendar;
        }

        public function 
    createDay($enterDate$events){
            
    $timestamp strtotime($enterDate);
            
    $timestamp_legible date('Y-m-d'$timestamp);

            
    $output '<p>
                            <a href="'
    .$_SERVER['PHP_SELF'].'?calendarType=day&workDate='.date('Y-m-d'strtotime('-1 day '.$timestamp_legible)).'">&#x2190;</a>
                             | 
                            <a href="'
    .$_SERVER['PHP_SELF'].'?calendarType=day&workDate='.date('Y-m-d'strtotime('+1 days '.$timestamp_legible)).'">&#x2192;</a>
                        </p>'
    ;

            
    $output .=    '<table>
                            <caption>'
    .date('F j'$timestamp).'</caption>
                            <thead><tr><th>'
    .date('Y'$timestamp).'</th><th>'.date('F j'$timestamp).'</th></tr></thead>
                            </tbody>
                        '
    ;




            
    // >>>>>>>>>>>>>>>>>>    TIME    <<<<<<<<<<<<<<
            // this time array is used for printing rows, one row for 00 and 15 minutes, and another row for 30 and 45 minutes
            
    $time=array();
            for(
    $i=6$i <= 22$i++){
                
    $time[] = strtotime('+'.$i.' hour 12AM');
                
    $time[] = strtotime('+'.$i.' hour 30 minutes 12AM');
            }
            
            
    // loop hours
            
    foreach($time as $fifteen_increments){
                
    $hour date('H'$fifteen_increments);
                
    $hour_minutes date('H:i'$fifteen_increments);
                
    $fifteen_increments = ($hour_minutes != '12:00') ? date('g:i A'$fifteen_increments) : 'Noon';

    //            echo date('i', strtotime($fifteen_increments)).'<br />';

                // a row, every half-hour
                
    if(date('i'strtotime($fifteen_increments))=='00'){
                    
    $output .= '<tr><th scope="row">'.$fifteen_increments.'</th>';
                }
                
    // a row, every half-hour
                
    if(date('i'strtotime($fifteen_increments))=='30'){
                    
    $output .= '<tr><th scope="row"></th>';
                }

                
    $running_day='';
                
    $running_day_15='';
                
    $running_day_30='';
                
    $running_day_45='';

                
    // loop days
    //            for($j=0; $j<7;$j++){
                    
    $insert_content '';
                    
    $minutes substr($hour_minutes, -2);
                    if(
    $minutes==00)
                    {
                        
    $running_day date('Y-m-d'strtotime('+'.$j.' day '.$sunday_legible)).' '.$hour_minutes.':00';
                        
    $running_day_15 date('Y-m-d'strtotime('+'.$j.' day '.$sunday_legible)).' '.$hour.':15:00';
                    }
                    

                    if(
    $minutes==30)
                    {
                        
    $running_day_30 date('Y-m-d'strtotime('+'.$j.' day '.$sunday_legible)).' '.$hour.':30:00';
                        
    $running_day_45 date('Y-m-d'strtotime('+'.$j.' day '.$sunday_legible)).' '.$hour.':45:00';
                    }

    //                foreach($events as $value){
                        
    $event_day_time date('Y-m-d H:i:s'strtotime($value[0]));
                        
    $event_time date('g:i A'strtotime($value[0]));
                        
    $event_name $value[1];
                        if(
    $minutes==00){
                            if(
    $running_day==$event_day_time or $running_day_15==$event_day_time ){
                            
    $insert_content .= '<span class="event">'.$event_time.'<br />'.$event_name.'</span>';
                            }
                        }
                        if(
    $minutes==30){
                            if(
    $running_day_30==$event_day_time or $running_day_45==$event_day_time ){
                            
    $insert_content .= '<span class="event">'.$event_time.'<br />'.$event_name.'</span>';
                            }
                        }
    //                }
    //            }

                
    $running_day='';
                
    $running_day_15='';
                
    $running_day_30='';
                
    $running_day_45='';

                
    $output .= '<td>'.$insert_content.'</td></tr>';
    //            $output .= '</tr>';
            
    }
    //        $label .= '</tr>';
    //        $content .= '</tr>';


            
    $output .= '</tbody></table>';
            return 
    $output;
        }


        public function 
    displayCalendar()
        {
            
    $calendarHeader =    '<h1>$_SERVER["REQUEST_TIME"]: '.$_SERVER['REQUEST_TIME'].'</h1>
                                <h2>'
    .strtotime('2009-11-25 11:00:00').'</h2>
                                <p>
                                    <a href="'
    .$_SERVER['PHP_SELF'].'?calendarType=day&workDate='.$this->_workDate.'">day</a> | 
                                    <a href="'
    .$_SERVER['PHP_SELF'].'?calendarType=week&workDate='.$this->_workDate.'">week</a> | 
                                    <a href="'
    .$_SERVER['PHP_SELF'].'?calendarType=month&workDate='.$this->_workDate.'">month</a>
                                </p>
                                <p>
                                    <a href="'
    .$_SERVER['PHP_SELF'].'?calendarType='.$this->_calendarType.'&workDate='.date("Y-m-d").'">Today</a>
                                </p>'
    ;
            
    // change to a swich/case instead of ifelse
            
    if($this->_calendarType=='month')
            {
    //            return $calendarHeader.$this->createMonth($this->_workDate, $this->_events);
                
    return $calendarHeader.$this->createMonthSkeleton($this->_workDate$this->_events);
            }
            elseif(
    $this->_calendarType=='week')
            {
                return 
    $calendarHeader.$this->createWeekRow($this->_workDate$presentMonth'complete'$this->_events);
            }
            elseif(
    $this->_calendarType=='day')
            {
                return 
    $calendarHeader.$this->createDay($this->_workDate$this->_events);
            }
        }

    Testing the class
    PHP Code:
    <style>
    body{ font-family:Arial, Helvetica, sans-serif; background-color:#CCCCCC; }
    div { background-color:#FFFFFF; padding:100px; margin-left:10%; margin-right:10%; }
    table { border:1px solid #999; border-collapse:collapse; }
    caption { font-size:2em; font-weight:bold; }
    th { background-color:#CCCCCC; font-size:8pt; }
    th#today { background-color:#FF9966; }
    td{ border:1px solid #666; font-size:8pt; }
    td.previous, td.next { background-color:#CCCCCC; }
    td#today{ background-color:#FFFFCC; }
    tr.dayLabel { font-size:.75em; text-align:right; }
    tr.dayLabel td { border-bottom:0; }
    tr.dayContent td, tr.dayContent td.previous { border-top:0; }
    tr.dayContent td span { background-color:#CCCCFF; border:1px solid #9999FF; clear:both; display:block; }
    span.event { background-color:#CCCCFF; display:block; }
    </style>

    <div>
    <?php
    include_once 'pathToClasses/LeoCalendar.class.php';

    $events =    array
                (
                    array(
    '2009-11-09 15:45:00''tech tranining'),
                    array(
    '2009-11-25 11:00:00''website launch'),
                    array(
    '2009-11-11 08:30:00''work on Calendar class'),
                    array(
    '2009-11-11 13:00:00''CSS tranining session'),
                    array(
    '2009-11-13 17:15:00''PHP tranining at KH'),
                    array(
    '2009-11-10 11:00:00''kronos tranining'),
                    array(
    '2009-12-20 09:30:00''vacations!')
                );

    $calendar = new LeoCalendar($_GET['workDate'], $_GET['calendarType'], $events);
    echo 
    $calendar->displayCalendar();

    ?>
    </div>
    "You are playing a very dangerous game"
    "Change always is"

    Creativo Development

  7. #7
    Day View now works.

    Now working on event duration.

    PHP Code:
    class LeoCalendar
    {
        protected 
    $_workDate;
        protected 
    $_calendarType;
        protected 
    $_events;
        protected 
    $_year;
        protected 
    $_month;
        protected 
    $_day;
        protected 
    $_week;
        protected 
    $_calendar;

        
    // boolean
        // 0 = is not in this month
        // 1 = it is in this month
        
    protected $_inThisMonth;

        public function 
    __construct($date=''$calendarType='month'$events=array())
        {
            if(!isset(
    $date))
            {
                
    $date date('Y-m-d');
                
    $this->_workDate $date;
            }
            else
            {
                
    $date date('Y-m-d'strtotime($date));
                
    $this->_workDate $date;
            }
            
    // assign the values to the class properties
            
    $this->_year = ( int ) date'Y'strtotime($this->_workDate));
            
    $this->_month = ( int ) date'n'strtotime($this->_workDate));
            
    $this->_day = ( int ) date'j'strtotime($this->_workDate));

            
    $this->_calendarType $calendarType;
            
    $allowedCalendarTypes = array('month''week''day');
            if(!
    in_array($this->_calendarType$allowedCalendarTypes)){
                
    $this->_calendarType='month';
                
    // throw exception
            
    }

            if(!
    is_null($events) && !is_array($events))
            {
                throw new 
    Exception('Must be array');
                exit(
    'Error!');
            }
            
    $this->_events $events;
        }


        
    ################################################
        # PUBLIC METHODS FOR blah blah blah            #
        ################################################
        
    public function createWeekRow($enterDate$presentMonth$format='complete'$events=array()){
            if(!isset(
    $presentMonth) or $presentMonth==NULL){$presentMonth=date('m'strtotime($enterDate));}
            
    $today date('Y-m-d');
            
    $today_timestamp strtotime($today);
            
    $timestamp strtotime($enterDate);
            
    $sunday_timestamp $this->getSunday($timestamp'timestamp');
            
    $sunday_legible $this->getSunday($timestamp'legible');
            
    $year date('Y'$sunday_timestamp);
            
    $label '<tr class="dayLabel">';
            
    $content '<tr class="dayContent">';
            
    $year date('Y'$sunday_timestamp);
            
    $complete '<p>
                            <a href="'
    .$_SERVER['PHP_SELF'].'?calendarType=week&workDate='.date('Y-m-d'strtotime('-1 day '.$sunday_legible)).'">&#x2190;</a>
                             | 
                            <a href="'
    .$_SERVER['PHP_SELF'].'?calendarType=week&workDate='.date('Y-m-d'strtotime('+7 days '.$sunday_legible)).'">&#x2192;</a>
                        </p>
                        <table>
                            <caption>'
    .date('F j'$sunday_timestamp).' - '.date('F j, Y'strtotime('+6 days '.$sunday_legible)).'</caption>
                            <colgroup>
                                    <col id="time" />
                                    <col id="Sunday" />
                                    <col id="Monday" />
                                    <col id="Tuesday" />
                                    <col id="Wednesday" />
                                    <col id="Thursday" />
                                    <col id="Friday" />
                                    <col id="Saturday" />
                                </colgroup>
                                <thead><tr><th scope="col">'
    .$year.'</th>
                        '
    ;

            
    // days
            
    for($i=0;$i<7;$i++){
                
    $date date('l, M j'strtotime('+'.$i.' day '.$sunday_legible));
                
    $day date('j'strtotime('+'.$i.' day '.$sunday_legible));
                
    $today '';
                
    $running_day date('Y-m-d'strtotime('+'.$i.' day '.$sunday_legible));
                
    $running_month date('m'strtotime('+'.$i.' day '.$sunday_legible));
                
    $insert_content '';
                
    // find out a way to highlight today's date
                
    if(date('Y-m-d') == $running_day){
                    
    $today ' id="today"';
                }
                else{
                    
    $today '';
                }

                foreach(
    $events as $key => $value){
                    if(
    $running_day==date('Y-m-d'strtotime($value[0]))){
                        
    $insert_content .= '<span>'.date('g:i A'$value[0]).'<br />'.$value[1].'</span>';
                    }
                }

                
    $complete .= '<th scope="col"'.$today.'>'.$date.'</th>';
                if(
    $running_month $presentMonth){
                    
    $label .= '<td class="previous"'.$today.'>'.$day.'</td>';
                    
    $content .= '<td class="previous"'.$today.'>'.$insert_content.'</td>';
                }
                elseif(
    $running_month $presentMonth){
                    
    $label .= '<td class="next"'.$today.'>'.$day.'</td>';
                    
    $content .= '<td class="next"'.$today.'>'.$insert_content.'</td>';
                }
                else{
                    
    $label .= '<td'.$today.'>'.$day.'</td>';
                    
    $content .= '<td'.$today.'>'.$insert_content.'</td>';
                }
            }
            
    $complete .='</tr>';

            
    // >>>>>>>>>>>>>>>>>>    TIME    <<<<<<<<<<<<<<
            // this time array is used for printing rows, one row for 00 and 15 minutes, and another row for 30 and 45 minutes
            
    $time=array();
            for(
    $i=6$i <= 22$i++){
                
    $time[] = strtotime('+'.$i.' hour 12AM');
                
    $time[] = strtotime('+'.$i.' hour 30 minutes 12AM');
            }
            
            
    // loop hours
            
    foreach($time as $fifteen_increments){
                
    $hour date('H'$fifteen_increments);
                
    $hour_minutes date('H:i'$fifteen_increments);
                
    $fifteen_increments = ($hour_minutes != '12:00') ? date('g:i A'$fifteen_increments) : 'Noon';

                
    // a row, every half-hour
                
    if(date('i'strtotime($fifteen_increments))=='00'){
                    
    $complete .= '<tr><th scope="row">'.$fifteen_increments.'</th>';
                }
                
    // a row, every half-hour
                
    if(date('i'strtotime($fifteen_increments))=='30'){
                    
    $complete .= '<tr><th scope="row"></th>';
                }

                
    $running_day='';
                
    $running_day_15='';
                
    $running_day_30='';
                
    $running_day_45='';

                
    // loop days
                
    for($j=0$j<7;$j++){
                    
    $insert_content '';
                    
    $minutes substr($hour_minutes, -2);
                    if(
    $minutes==00)
                    {
                        
    $running_day date('Y-m-d'strtotime('+'.$j.' day '.$sunday_legible)).' '.$hour_minutes.':00';
                        
    $running_day_15 date('Y-m-d'strtotime('+'.$j.' day '.$sunday_legible)).' '.$hour.':15:00';
                    }
                    

                    if(
    $minutes==30)
                    {
                        
    $running_day_30 date('Y-m-d'strtotime('+'.$j.' day '.$sunday_legible)).' '.$hour.':30:00';
                        
    $running_day_45 date('Y-m-d'strtotime('+'.$j.' day '.$sunday_legible)).' '.$hour.':45:00';
                    }

                    foreach(
    $events as $value){
                        
    $event_day_time date('Y-m-d H:i:s'strtotime($value[0]));
                        
    $event_time date('g:i A'strtotime($value[0]));
                        
    $event_name $value[1];
                        if(
    $minutes==00){
                            if(
    $running_day==$event_day_time or $running_day_15==$event_day_time ){
                                
    $insert_content .= '<span class="event">'.$event_time.'<br />'.$event_name.'</span>';
                            }
                        }
                        if(
    $minutes==30){
                            if(
    $running_day_30==$event_day_time or $running_day_45==$event_day_time ){
                                
    $insert_content .= '<span class="event">'.$event_time.'<br />'.$event_name.'</span>';
                            }
                        }
                    }
                    
    $complete .= '<td>'.$insert_content.'</td>';
                }
                
    $complete .= '</tr>';
            }
            
    $label .= '</tr>';
            
    $content .= '</tr>';

            if(
    $format=='complete'){
                return 
    $complete.'</table>';
            }
            else{
                return 
    $label.$content;
            }
        }

        public function 
    getSunday($timestamp$format='timestamp')
        {
            
    $timestamp_legible date('Y-m-d'$timestamp);
            if(
    date('l'$timestamp)=='Sunday')
            {
                
    $sunday_timestamp $timestamp;
                
    $sunday_legible date('F j, Y'$sunday_timestamp);
            }
            else
            {
                
    $sunday_timestamp strtotime(date('r',strtotime('last Sunday '.$timestamp_legible)));
                
    $sunday_legible date('F j, Y'$sunday_timestamp);
            }
            
    $year date('Y'$sunday_timestamp);
            if(
    $format=='legible')
            {
                return 
    $sunday_legible;
            }
            else
            {
                return 
    $sunday_timestamp;
            }
        }

        public function 
    getNextSunday($timestamp$format='legible')
        {
            
    $timestamp_legible date('Y-m-d'$timestamp);
            if(
    date('l'$timestamp)=='Saturday')
            {
                
    $next_week_timestamp strtotime(date('r',strtotime($timestamp_legible.' + 7 days')));
                
    $next_week_legible date('F j, Y'$next_week_timestamp);
                
    $sunday_next_week_timestamp strtotime(date('r'strtotime('last Sunday '.$next_week_legible)));
                
    $sunday_next_week_legible date('F j, Y'$sunday_next_week_timestamp);
            }
            else
            {
                
    $next_week_timestamp strtotime(date('r',strtotime($timestamp_legible.' + 8 days')));
                
    $next_week_legible date('F j, Y'$next_week_timestamp);
                
    $sunday_next_week_timestamp strtotime(date('r'strtotime('last Sunday '.$next_week_legible)));
                
    $sunday_next_week_legible date('F j, Y'$sunday_next_week_timestamp);
            }

            if(
    $format=='legible')
            {
                return 
    $sunday_next_week_legible;
            }
            else
            {
                return 
    $sunday_next_week_timestamp;
            }
        }

        public function 
    createMonthSkeleton($date$events)
        {
        
            
    $legible_date date('l, F j,  Y'strtotime($date));
            
    $year date('Y'strtotime($date));
            
    $month date('m'strtotime($date));
            
    $day date('d'strtotime($date));
        
            
    // get first day of previous month
            // using the first day of the previous
            // month, the link to the previous month
            // is created
            
    if($month==1)
            {
                
    $previousYear $year-1;
                
    $previousMonth=12;
            }
            else
            {
                
    $previousYear $year;
                
    $previousMonth=$month-1;
            }
            
    // get first day of next month
            // using the first day of the next
            // month, the link to the next month
            // is created
            
    if($month==12)
            {
                
    $nextYear $year+1;
                
    $nextMonth=1;
            }
            else
            {
                
    $nextYear $year;
                
    $nextMonth=$month+1;
            }
        
            
    $firstDayOfMonth $year.'-'.$month.'-01';
            
    $firstDayOfPreviousMonth date('Y-m-d'strtotime($previousYear.'-'.$previousMonth.'-01'));
            
    $firstDayOfNextMonth date('Y-m-d'strtotime($nextYear.'-'.$nextMonth.'-01'));
            
    $numberOfDaysOfPreviousMonth date('t',strtotime(date('Y-m-d'strtotime($firstDayOfPreviousMonth))));
            
    $numberOfDaysOfCurrentMonth date('t',strtotime(date('Y-m-d'strtotime($year.'-'.$month.'-01'))));
        
            
    // what day of the week is the first day of the month?
            
    switch(date('l',strtotime($firstDayOfMonth)))
            {
                case 
    'Sunday':
                    
    $startTheCalendar 1;
                    break;
        
                case 
    'Monday':
                    
    $startTheCalendar $numberOfDaysOfPreviousMonth;
                    break;
        
                case 
    'Tuesday':
                    
    $startTheCalendar $numberOfDaysOfPreviousMonth-1;
                    break;
        
                case 
    'Wednesday':
                    
    $startTheCalendar $numberOfDaysOfPreviousMonth-2;
                    break;
        
                case 
    'Thursday':
                    
    $startTheCalendar $numberOfDaysOfPreviousMonth-3;
                    break;
        
                case 
    'Friday':
                    
    $startTheCalendar $numberOfDaysOfPreviousMonth-4;
                    break;
        
                case 
    'Saturday':
                    
    $startTheCalendar $numberOfDaysOfPreviousMonth-5;
            }
        
            
    $calendar '<p><a href="'.$_SERVER['PHP_SELF'].'?workDate='.$firstDayOfPreviousMonth.'">&#x2190;</a> <a href="'.$_SERVER['PHP_SELF'].'?workDate='.$firstDayOfNextMonth.'">&#x2192;</a></p>';
            
    $calendar .= '<table><caption>'.date('F Y',strtotime($date)).'</caption>';
            
    $calendar .= '<tr>';
            
    $calendar .= '<th>Sunday</th><th>Monday</th><th>Tuesday</th><th>Wednesday</th><th>Thursday</th><th>Friday</th><th>Saturday</th>';
            
    $calendar .= '</tr>';
            
            
    // does the calendar start with the previous month?
            
    if($startTheCalendar 20)
            {
                
    $startedPreviousMonth=1;
                
    $goCurrentMonth=0;
                
    $startedCurrentMonth=0;
            }
            else
            {
                
    $startedPreviousMonth=0;
                
    $goCurrentMonth=1;
                
    $startedCurrentMonth=1;
            }
            
            
    $days $startTheCalendar;
            
    $startedNextMonth=0;
            
    $goNextMonth=0;

            for(
    $w=0;$w<6;$w++)
            {
                if(
    $w==0)
                {
                    
    $week_day $firstDayOfMonth;
                }
                else
                {
                    
    $next_sunday $this->getNextSunday(strtotime($week_day));
                    
    $week_day $next_sunday;
                }
                
    $calendar .= $this->createWeekRow($week_day$month'row'$events);
            }
            
    $calendar $calendar.'</table>';
            return 
    $calendar;
        }

        public function 
    createDay($enterDate$events){
            
    $timestamp strtotime($enterDate);
            
    $timestamp_legible date('Y-m-d'$timestamp);

            
    $output '<p>
                            <a href="'
    .$_SERVER['PHP_SELF'].'?calendarType=day&workDate='.date('Y-m-d'strtotime('-1 day '.$timestamp_legible)).'">&#x2190;</a>
                             | 
                            <a href="'
    .$_SERVER['PHP_SELF'].'?calendarType=day&workDate='.date('Y-m-d'strtotime('+1 days '.$timestamp_legible)).'">&#x2192;</a>
                        </p>'
    ;

            
    $output .=    '<table>
                            <caption>'
    .date('F j'$timestamp).'</caption>
                            <thead><tr><th>'
    .date('Y'$timestamp).'</th><th>'.date('F j'$timestamp).'</th></tr></thead>
                            </tbody>
                        '
    ;

            
    // this time array is used for printing rows, one row for 00 and 15 minutes, and another row for 30 and 45 minutes
            
    $time=array();
            for(
    $i=6$i <= 22$i++){
                
    $time[] = strtotime('+'.$i.' hour 12AM');
                
    $time[] = strtotime('+'.$i.' hour 30 minutes 12AM');
            }
            
            
    // loop hours
            
    foreach($time as $fifteen_increments){
                
    $hour date('H'$fifteen_increments);
                
    $hour_minutes date('H:i'$fifteen_increments);
                
    $fifteen_increments = ($hour_minutes != '12:00') ? date('g:i A'$fifteen_increments) : 'Noon';

                
    // a row, every half-hour
                
    if(date('i'strtotime($fifteen_increments))=='00'){
                    
    $output .= '<tr><th scope="row">'.$fifteen_increments.'</th>';
                }
                
    // a row, every half-hour
                
    if(date('i'strtotime($fifteen_increments))=='30'){
                    
    $output .= '<tr><th scope="row"></th>';
                }

                
    $running_day='';
                
    $running_day_15='';
                
    $running_day_30='';
                
    $running_day_45='';

                
    $insert_content '';
                
    $minutes substr($hour_minutes, -2);
                if(
    $minutes==00)
                {
                    
    $running_day $timestamp_legible.' '.$hour_minutes.':00';
                    
    $running_day_15 $timestamp_legible.' '.$hour.':15:00';
                }
                if(
    $minutes==30)
                {
                    
    $running_day_30 $timestamp_legible.' '.$hour.':30:00';
                    
    $running_day_45 $timestamp_legible.' '.$hour.':45:00';
                }
                foreach(
    $events as $value){
                    
    $event_day_time date('Y-m-d H:i:s'strtotime($value[0]));
                    
    $event_time date('g:i A'strtotime($value[0]));
                    
    $event_name $value[1];
                    if(
    $minutes==00){
                        if(
    $running_day==$event_day_time or $running_day_15==$event_day_time ){
                        
    $insert_content .= '<span class="event">'.$event_time.'<br />'.$event_name.'</span>';
                        }
                    }
                    if(
    $minutes==30){
                        if(
    $running_day_30==$event_day_time or $running_day_45==$event_day_time ){
                        
    $insert_content .= '<span class="event">'.$event_time.'<br />'.$event_name.'</span>';
                        }
                    }
                }
                
    $output .= '<td>'.$insert_content.'</td></tr>';
            }
            
    $output .= '</tbody></table>';
            return 
    $output;
        }


        public function 
    displayCalendar()
        {
            
    $calendarHeader =    '<h1>$_SERVER["REQUEST_TIME"]: '.$_SERVER['REQUEST_TIME'].'</h1>
                                <h2>'
    .strtotime('2009-11-25 11:00:00').'</h2>
                                <p>
                                    <a href="'
    .$_SERVER['PHP_SELF'].'?calendarType=day&workDate='.$this->_workDate.'">day</a> | 
                                    <a href="'
    .$_SERVER['PHP_SELF'].'?calendarType=week&workDate='.$this->_workDate.'">week</a> | 
                                    <a href="'
    .$_SERVER['PHP_SELF'].'?calendarType=month&workDate='.$this->_workDate.'">month</a>
                                </p>
                                <p>
                                    <a href="'
    .$_SERVER['PHP_SELF'].'?calendarType='.$this->_calendarType.'&workDate='.date("Y-m-d").'">Today</a>
                                </p>'
    ;
            
    // change to a swich/case instead of ifelse
            
    if($this->_calendarType=='month')
            {
                return 
    $calendarHeader.$this->createMonthSkeleton($this->_workDate$this->_events);
            }
            elseif(
    $this->_calendarType=='week')
            {
                return 
    $calendarHeader.$this->createWeekRow($this->_workDate$presentMonth'complete'$this->_events);
            }
            elseif(
    $this->_calendarType=='day')
            {
                return 
    $calendarHeader.$this->createDay($this->_workDate$this->_events);
            }
        }

    Last edited by imagined; November 10th, 2009 at 04:04 PM.
    "You are playing a very dangerous game"
    "Change always is"

    Creativo Development

  8. #8
    actionAction's Avatar
    1,142
    posts
    humanBeing._beard=true;
    Here is what I got up to the foreach I posted above:
    PHP Code:
    class LeoCalendar
    {
        protected 
    $_workDate;
        protected 
    $_calendarType;
        protected 
    $_events;
        protected 
    $_year;
        protected 
    $_month;
        protected 
    $_day;
        protected 
    $_week;
        protected 
    $_calendar;
        protected 
    $_inThisMonth;

        public function 
    __construct($date=''$calendarType='month'$events=array())
        {
            
            
    $date             = (!isset($date)) ? 
                                
    date('Y-m-d') : 
                                
    date('Y-m-d'strtotime($date));
            
    $this->_workDate $date;
            
            
    $worktime         strtotime($this->_workDate);
            
    // assign the values to the class properties
            
    $this->_year     = ( int ) date'Y'$worktime);
            
    $this->_month     = ( int ) date'n'$worktime);
            
    $this->_day     = ( int ) date'j'$worktime);

            
    $this->_calendarType  $calendarType;
            
    $allowedCalendarTypes = array('month''week''day');
            if(!
    in_array($this->_calendarType$allowedCalendarTypes)){
                
    $this->_calendarType 'month';
                
    // throw exception
            
    }

            if(!
    is_null($events) && !is_array($events))
            {
                throw new 
    Exception('Must be array');
                exit(
    'Error!');
            }
            
    $this->_events $events;
        }


        private function 
    _html($tag$content$attribs=array()){
            
    $attribute_string = array();
            foreach(
    $attribs as $attribute => $value){
                
    $attribute_string[] = "$attribute=\"$value\"";
            }
            
    $attribute_string implode(" "$attribute_string);
            
    $html "<$tag $attribute_string>$content</$tag>";
            return 
    $html;
        }    

        
    ################################################
        # PUBLIC METHODS FOR blah blah blah            #
        ################################################
        
    public function createWeekRow($enterDate$presentMonth$format='complete'$events=array()){
            
    $enterDate_string strtotime($enterDate);
            
            if(!isset(
    $presentMonth) or $presentMonth==NULL){
                
    $presentMonth=date('m'$enterDate_string);
            }
            
    $today                  date('Y-m-d');
            
    $today_timestamp     strtotime($today);
            
    $timestamp              $enterDate_string;
            
    $sunday_timestamp     $this->getSunday($timestamp'timestamp');
            
    $sunday_legible     $this->getSunday($timestamp'legible');
            
    $year                 date('Y'$sunday_timestamp);
            
    $label                 '<tr class="dayLabel">';
            
    $content             '<tr class="dayContent">';

            
    $complete '<p>
                            <a href="'
    .$_SERVER['PHP_SELF'].'?calendarType=week&workDate='.date('Y-m-d'strtotime('-1 day '.$sunday_legible)).'">&#x2190;</a>
                             | 
                            <a href="'
    .$_SERVER['PHP_SELF'].'?calendarType=week&workDate='.date('Y-m-d'strtotime('+7 days '.$sunday_legible)).'">&#x2192;</a>
                        </p>
                        <table>
                            <caption>'
    .date('F j'$sunday_timestamp).' - '.date('F j, Y'strtotime('+6 days '.$sunday_legible)).'</caption>
                            <colgroup>
                                    <col id="time" />
                                    <col id="Sunday" />
                                    <col id="Monday" />
                                    <col id="Tuesday" />
                                    <col id="Wednesday" />
                                    <col id="Thursday" />
                                    <col id="Friday" />
                                    <col id="Saturday" />
                                </colgroup>
                                <thead><tr><th scope="col">'
    .$year.'</th>
                        '
    ;

            
    // days
            
    for($i=0;$i<7;$i++){
                
    $week_format_time strtotime('+'.$i.' day '.$sunday_legible);
                
    $date     date('l, M j'$week_format_time);
                
    $day     date('j'$week_format_time);
                
    $running_day date('Y-m-d'$week_format_time);
                
    $running_month date('m'$week_format_time);
                
    $insert_content '';
                
    // find out a way to highlight today's date
                
    if(date('Y-m-d') == $running_day){
                    
    $attribs['id'] = 'today';
                }

                foreach(
    $events as $value){            
                    if(
    $running_day == date('Y-m-d'strtotime($value[0]))){
                        
    $ins_cont_date date('g:i A'strtotime($value[0]));
                        
    $insert_content .= $this->_html('span'$ins_cont_date.'<br />'.$value[1]);
                    }
                }

                
    $complete .= $this->_html('th'$today.'>'.$date, array('scope' => 'col'));
                if(
    $running_month $presentMonth){
                    
    $attribs['class'] = 'previous';
                    
    $label         .= $this->_html('td'$day$attribs);
                    
    $content     .= $this->_html('td'$insert_content$attribs);
                }
                elseif(
    $running_month $presentMonth){
                    
    $attribs['class'] = 'next';
                    
    $label         .= $this->_html('td'$day$attribs);
                    
    $content     .= $this->_html('td'$insert_content$attribs);
                }
                else{
                    
    $attribs['class'] = null;
                    
    $label         .= $this->_html('td'$day$attribs);
                    
    $content     .= $this->_html('td'$insert_content$attribs);
                }
            }
            
    $complete .='</tr>';

            
    // >>>>>>>>>>>>>>>>>>    TIME    <<<<<<<<<<<<<<
            // this time array is used for printing rows, one row for 00 and 15 minutes, and another row for 30 and 45 minutes
            
    $time=array();
            for(
    $i=6$i <= 22$i++){
                
    $time[] = strtotime('+'.$i.' hour 12AM');
                
    $time[] = strtotime('+'.$i.' hour 30 minutes 12AM');
            } 

  9. #9
    Thanks a lot!

    I had little idea of how to go about creating functions without outputting HTML. Now that I see your code, it looks pretty straigh-forward. I'm going to implement it on the whole class.

    I'm going to ask you a question I asked other Web developers before. Hopefully it won't get deleted because might not be on the right forum. I'll ask this question at the Random forum.

    Ever thought about creating a Web application where users have to subscribe for a monthly fee like the ones from 37signals, box.net, dropsend or craiglist?
    "You are playing a very dangerous game"
    "Change always is"

    Creativo Development

  10. #10
    actionAction's Avatar
    1,142
    posts
    humanBeing._beard=true;
    No problem, I like programming + helping (win,win)!

    If I am understanding your question correctly: yes, I have thought about and have created web applications that users have to subscribe to (and pay for). Are you thinking about charging for your calendar app?

  11. #11
    Quote Originally Posted by actionAction View Post
    If I am understanding your question correctly: yes, I have thought about and have created web applications that users have to subscribe to (and pay for). Are you thinking about charging for your calendar app?
    It's not just a calendar app. It's a web app for a very particular group of users. The calendar is just part of the web app. It's the first subscription web app I'm working on.

    I'm not talking something like an inventory system for ONE client. I'm talking about a Web app a lot of people can subscribe to and pay monthly.

    I have a full-time job as a Webmaster in a school district. I just finished the new Website. It will launch in a few weeks. I used WordPress and created a custom theme for it. It has a lot of content. I also design websites for businesses on the side. But I would really like to quit my current job and just work on my own. I think a subscription web app it's a way of having a secure constant source of income as opposed to freelancing as a website designer.

    What are the Websites for your apps? I would like to take a look at your work.

    What should I expect for my web app? What was your experience with your apps?
    Last edited by imagined; November 10th, 2009 at 05:03 PM.
    "You are playing a very dangerous game"
    "Change always is"

    Creativo Development

  12. #12
    actionAction's Avatar
    1,142
    posts
    humanBeing._beard=true;
    I PMd you about your first question. Regarding the second: expect to have to work hard to get interest, even if you are the only fish in the sea. Do a lot of searching for existing apps that accomplish or are trying to accomplish the same set. Figure out what they are doing right/wrong, try to enhance what they are getting right and correct where they are going wrong. Determine your market viability by gathering as much information possible about your target demographic so you can answer:

    -are they willing to spend money on web apps?
    -what are the benefits of your app and how much is that worth to your users? (could include test markets)
    -how much disposable income do they have?
    -why do they want your software?
    -are other apps good enough or better?
    -is your software visible in the right places?

    Also answer:
    -where will it be hosted?
    -will it be distrubted to individuals for their own servers? -need a compiler, if so
    -what security concerns do I have? sensitive data?
    -what are my costs in running this software?
    -how/how much/when will I get paid?
    -will I use ads?
    -am I okay with not being paid for awhile?
    -am I ready to support software that I have written? what are my support hours like? do I outsource? is it just me?
    -how can I thoroughly test the software?
    -Last but not least, do I need to incorporate to protect myself and my assets from lawsuits should something bad and unforseen happen?

    Also, be ready for competition to sprout up and for them to be attacking your software (verbally); they will probably sign up and pay so they can reverse engineer it.

  13. #13
    Quote Originally Posted by actionAction View Post
    PHP Code:
        private function _html($tag$content$attribs=array()){
            
    $attribute_string = array();
            foreach(
    $attribs as $attribute => $value){
                
    $attribute_string[] = "$attribute=\"$value\"";
            }
            
    $attribute_string implode(" "$attribute_string);
            
    $html "<$tag $attribute_string>$content</$tag>";
            return 
    $html;
        }    
    [...]
                foreach(
    $events as $value){            
                    if(
    $running_day == date('Y-m-d'strtotime($value[0]))){
                        
    $ins_cont_date date('g:i A'strtotime($value[0]));
                        
    $insert_content .= $this->_html('span'$ins_cont_date.'<br />'.$value[1]);
                    }
                }

                
    $complete .= $this->_html('th'$today.'>'.$date, array('scope' => 'col'));
                if(
    $running_month $presentMonth){
                    
    $attribs['class'] = 'previous';
                    
    $label         .= $this->_html('td'$day$attribs);
                    
    $content     .= $this->_html('td'$insert_content$attribs);
                }
                elseif(
    $running_month $presentMonth){
                    
    $attribs['class'] = 'next';
                    
    $label         .= $this->_html('td'$day$attribs);
                    
    $content     .= $this->_html('td'$insert_content$attribs);
                }
                else{
                    
    $attribs['class'] = null;
                    
    $label         .= $this->_html('td'$day$attribs);
                    
    $content     .= $this->_html('td'$insert_content$attribs);
                }
            }
            
    $complete .='</tr>'
    I'm examining the code (_html function) and trying to see how to implement it on my classes to separate logic from design.

    Isn't this also design inside logic? It's still adding the tags inside the function. What would be the benefits of adding the _html function? I'm sorry, I'm not being rude, I'm just really trying to understand because I really want to implement the idea of separating logic from design as much as I can.
    "You are playing a very dangerous game"
    "Change always is"

    Creativo Development

  14. #14
    actionAction's Avatar
    1,142
    posts
    humanBeing._beard=true;
    You don't seem rude, I know you are trying to develop best practices for php. To answer your question: yes and no.

    Yes, I used the function extensively in your class with hard-coded tags so the design/logic are jumbled. However, my _html method actually contains no design, it doesn't care what html you feed to it or what attributes it has, it returns html to display. The html method could just as easily be using parameters to the parent methods it is used within to dictate the tag type and attributes
    PHP Code:
    $obj->_html($parent_function_param1$internal_logic_results$parent_function_param2
    rather than being hard coded as
    PHP Code:
    $obj->_html('td''whatever', array('foo'=>'bar')) 
    . PHP only exists because it generates html, so don't think of separating design from logic as separating PHP and HTML. As I said when I posted the _html method, in this specific implementation it only serves to clean things up and make your code more readable. Overall, the concept of separating logic from design is implying that you are not hard coding the display of dynamically generated content in a class; this should be left to the person implementing your class. If you hard code the html that is produced by your class, you will have to alter your class every time you use it, which defies the OOP tenant of reusability. In an ideal class, you allow the user to dictate the html structure of returned content through method/constructor parameters.

    The best way to separate logic from design in your class is to let the user input the tag structure of the returned content. Whether they want divs and spans or tables, let them do that by accepting an associative array of attributes and traversing it.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012