dpzo13732 2013-06-17 09:37
浏览 56
已采纳

PHP日历功能仅显示第10,11和12个月的事件

I have a Calendar function and the problem is that it only displays events for months 10, 11 and 12 none before that are shown.

So if on the URL I add:

calender.php?month=01&year=2013 TO calender.php?month=09&year=2013, No events will be displayed.

BUT if I do:

calender.php?month=10&year=2013, calender.php?month=11&year=2013 or calender.php?month=12&year=2013, these will display fine.

I cannot find the part of the code responsible for this bug, any ideas anyone? :o/

Here is the Full function code:

<?php

        /* draws a calendar */
        function draw_calendar($month,$year,$events = array()){

          /* draw table */
          $calendar = '<table cellpadding="0" cellspacing="0" class="calendar">';

          /* table headings */
          $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>';

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

          /* row for week one */
          $calendar.= '<tr class="calendar-row">';

          /* print "blank" days until the first of the current week */
          for($x = 0; $x < $running_day; $x++):
            $calendar.= '<td class="calendar-day-np">&nbsp;</td>';
            $days_in_this_week++;
          endfor;

          /* keep going with days.... */
          for($list_day = 1; $list_day <= $days_in_month; $list_day++):


            ////$calendar.= '<td class="calendar-day"><div style="position:relative;height:100px;">';
            $calendar.= '<td class="calendar-day"><div style="position:relative;height:100px;">';

            /* add in the day number */
            if($list_day < 10) {
                $list_day = str_pad($list_day, 2, '0', STR_PAD_LEFT);
            }


            $calendar.= ".$list_day.";

            $event_day = $year.'-'.$month.'-'.$list_day;



            //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////  

              /* add in the day number */
              $calendar.= '<div class="day-number">'.$list_day.'</div>';

              $event_day = $year.'-'.$month.'-'.$list_day;



              if(isset($events[$event_day])) {

                  //var_dump($events[$event_day]);

                foreach($events[$event_day] as $event) {
               ////$calendar.= '<div class="event"><a href="cal_popup.php?title='.$event['title'].'" target="blank" title="'.$event['title'].'">'.$event['title'].'</a></div>';
                   $calendar.= '<div class="event" id="selector"><a href="#" target="blank" title="'.$event['title'].'">'.$event['title'].'</a><div class="overlayOuter"><div class="overlayInner"></div></div></div>';

                    //var_dump($events[$event_day]);


                }
              }
              else {
                $calendar.= str_repeat('<p>&nbsp;</p>',2);
              }

            ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

            $calendar.= '</div></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;

          /* 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">&nbsp;</td>';
            endfor;
          endif;

          /* final row */
          $calendar.= '</tr>';


          /* end the table */
          $calendar.= '</table>';

          /** DEBUG **/
          $calendar = str_replace('</td>','</td>'."
",$calendar);
          $calendar = str_replace('</tr>','</tr>'."
",$calendar);

          /* all done, return result */
          return $calendar;
        }

        function random_number() {
          srand(time());
          return (rand() % 7);
        }

        /* date settings */
        $month = (int) ($_GET['month'] ? $_GET['month'] : date('m'));
        $year = (int)  ($_GET['year'] ? $_GET['year'] : date('Y'));

        /* select month control */
        /*$select_month_control = '<select name="month" id="month">';
        for($x = 1; $x <= 12; $x++) {
          $select_month_control.= '<option value="'.$x.'"'.($x != $month ? '' : ' selected="selected"').'>'.date('F',mktime(0,0,0,$x,1,$year)).'</option>';
        }
        $select_month_control.= '</select>';*/

        $select_month_control = '<select name="month" id="month">';
            for($x = 1; $x <= 12; $x++) {
        $select_month_control.= '<option value="'.str_pad($x, 2, "0", STR_PAD_LEFT).'"'.($x != $month ? '' : ' selected="selected"').'>'.date('F',mktime(0,0,0,$x,1,$year)).'</option>';
}
$select_month_control.= '</select>';

        /* select year control */
        $year_range = 7;
        $select_year_control = '<select name="year" id="year">';
        for($x = ($year-floor($year_range/2)); $x <= ($year+floor($year_range/2)); $x++) {
          $select_year_control.= '<option value="'.$x.'"'.($x != $year ? '' : ' selected="selected"').'>'.$x.'</option>';
        }
        $select_year_control.= '</select>';

        /* "next month" control */
        $next_month_link = '<a href="?month='.($month != 12 ? $month + 1 : 1).'&year='.($month != 12 ? $year : $year + 1).'" class="control">Next Month &gt;&gt;</a>';

        /* "previous month" control */
        $previous_month_link = '<a href="?month='.($month != 1 ? $month - 1 : 12).'&year='.($month != 1 ? $year : $year - 1).'" class="control">&lt;&lt;   Previous Month</a>';


        /* bringing the controls together */
        $controls = '<form method="get">'.$select_month_control.$select_year_control.'&nbsp;<input id="cal_submit" type="submit" name="submit" value="Go" style="padding:1px 4px;margin:2px 0;" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.$previous_month_link.'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.$next_month_link.' </form>';

        /* get all events for the given month */
        $events = array();
        //$query = "SELECT title, DATE_FORMAT(date,'%Y-%m-%d') AS date FROM table1 WHERE user_id = '$session_user_id' AND date LIKE '$year-$month%' AND active = 1";

        $query = "SELECT title, DATE_FORMAT(date,'%Y-%m-%d') AS date FROM table1 WHERE user_id = '$session_user_id' AND date BETWEEN '$year-$month-1' AND '" . date("Y-m-t", strtotime("$year-$month-1")) . "' AND active = 1";



        $result = mysql_query($query,$db_link) or die('cannot get results!');
        while($row = mysql_fetch_assoc($result)) {

          $events[$row['date']][] = $row;

         //var_dump($events);
        //echo ($query);


        }

        echo '<div style="clear:left;float:left;width:100%;margin-top:30px;">';
        echo '<h2 style="float:left; padding-right:30px;">'.date('F',mktime(0,0,0,$month,1,$year)).' '.$year.'</h2>';
        echo '<div style="float:left;">'.$controls.'</div>';
        echo '<div style="clear:both;"></div>';
        echo '</div>';
        echo draw_calendar($month,$year,$events);
        echo '<br /><br />';


    ?>
  • 写回答

1条回答 默认 最新

  • douxihui8270 2013-06-17 11:57
    关注

    To problem is you make a integer of the month that means if you have 09 it will be 9 because 09 is not a integer.

    To fix this, change this line:

    $month = (int) ($_GET['month'] ? $_GET['month'] : date('m'));
    

    to

    $month = isset($_GET['month']) ? $_GET['month'] : date('m');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题