0
此代碼創建了一個不錯的日曆(original code),但我試圖對其進行一些修改。 第一行是行,沒必要注意這一點,但在這裏它是:Php和MySQL日曆修改
$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;
這裏就是我stucked。從現在起,日曆訪問數據庫並基於我記錄的事件打印我的日期。對於查詢發現任何事件的日期,代碼將在其中打印鏈接。看代碼:
$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>';}