0

这段代码创建了一个不错的日历原始代码),但我正在尝试对其进行一些修改。第一行是好的,不需要注意,但这里是:

$calendar = '<table cellpadding="0" cellspacing="0" class="calendar">';
$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>';

$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();

$calendar.= '<tr class="calendar-row">';

for($x = 0; $x < $running_day; $x++):
    $calendar.= '<td class="calendar-day-np"> </td>';
    $days_in_this_week++;
endfor;

这是我卡住的地方。从现在开始,日历访问数据库并根据我记录的事件打印我的日期。对于查询发现任何事件的日期,代码会在其中打印一个链接。见代码:

$db = new PDO('mysql:host=localhost;dbname=calendar','root','');
$stmt = $db->prepare('SELECT time, title FROM events');
$stmt->execute();

$rawTimeStamps = $stmt->fetchAll(PDO::FETCH_ASSOC);
$cleanDateArray = array();
foreach ($rawTimeStamps as $t) {
$rawDate = $t['time'];
$rawDate = getdate($rawDate);
$cleanDate = mktime(0,0,0,$rawDate['mon'],$rawDate['mday'],$rawDate['year']);
$cleanDataArray[] = $cleanDate;
}
for($list_day = 1; $list_day <= $days_in_month; $list_day++):

    $calendar.= '<td class="calendar-day">';
        $timestamp = mktime(0,0,0,$month,$list_day,$year);
        if (in_array($timestamp, $cleanDataArray)) {
        $calendar.= '<div class="day-number day-number-event"><a href="#">'.$list_day.'</a></div>';
        } else {
        $calendar.= '<div class="day-number day-number-noevent">'.$list_day.'</div></div><div id="calendar-events"></div>';
        }
    $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;

然后,代码完成了日历。

if($days_in_this_week < 8):
    for($x = 1; $x <= (8 - $days_in_this_week); $x++):
        $calendar.= '<td class="calendar-day-np"> </td>';
    endfor;
endif;

$calendar.= '</tr>';

$calendar.= '</table>';

return $calendar;

我需要的是从数据库中打印与发生事件的日期相关的其他信息。换句话说,我想要的只是在事件数据的正下方打印事件标题(也记录在事件表的列中)。就像是:

$calendar.= '<div class="day-number day-number-event"><a id="'.$timestamp.'" href="#">'.$list_day.'</a></div><p>'.$title.'</p>';}
4

1 回答 1

0

经过一些测试和研究,这是解决方案:

function draw_calendar($month,$year){

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


   $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"> </td>';
     $days_in_this_week++;
   endfor;

   $db = new PDO('mysql:host=localhost;dbname=calendar','root','');

   $stmt = $db->prepare('SELECT time, title FROM events');
   $stmt->execute();

   $rawTimeStamps = $stmt->fetchAll(PDO::FETCH_ASSOC);
   $cleanDateArray = array();

   foreach ($rawTimeStamps as $t) {
      $rawDate = $t['time'];
      $rawDate = getdate($rawDate);
      $cleanDate = mktime(0,0,0,$rawDate['mon'],$rawDate['mday'],$rawDate['year']);
      $cleanDataArray[] = $cleanDate;
   }

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

    $calendar.= '<td class="calendar-day">';

        $timestamp = mktime(0,0,0,$month,$list_day,$year);

        if (in_array($timestamp, $cleanDataArray)) {

        /* embromation */
        $date = getdate($timestamp);
        $time_start = mktime(0,0,0,$date['mon'],$date['mday'],$date['year']);
        $time_end = mktime(23,59,59,$date['mon'],$date['mday'],$date['year']);
        $stmt = $db->prepare('SELECT title FROM events WHERE time BETWEEN ? AND ?');
        $stmt->bindParam(1,$time_start,PDO::PARAM_INT);
        $stmt->bindParam(2,$time_end,PDO::PARAM_INT);
        $stmt->execute();
        $events = $stmt->fetch(PDO::FETCH_ASSOC);

        $calendar.= '<div class="day-number day-number-event"><a href="#">'.$list_day.'</a></div><p>'.$events["title"].'</p>';
        } else {
        $calendar.= '<div class="day-number day-number-noevent">'.$list_day.'</div></div><div id="calendar-events"></div>';
        }

    $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;

/* 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>';
    endfor;
endif;

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

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

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

}

于 2012-11-01T12:38:24.413 回答