2

我正在开发这个有日历系统的项目。为了更高效地开发,我从这个站点获得了日历代码。

原始代码很好:我做了一些修改,但我陷入了我的项目所需的修改之一。由于日历与数据库对话框一起使用,我希望它在其特定日期内显示数据库表的某些列。例如,如果我在 2012/10/28 记录了一个足球赛事(类似于我提到的网站的原始数据库代码),我希望它的标题显示在它的相对日期中。我真的很努力,但我没有找到解决方案。请帮忙!

我用来显示日历的代码是这样的:

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

$stmt = $db->prepare('SELECT time 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;
}
    /* 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"> </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">';

        /*
         * Assign a unique id to the day number. This unique id will be
         * used by jQuery to locate events from the database that are
         * on this day. The unique id is the timestamp for the first minute
         * of $list_day of $month of $year.
         *
         * @author Josh Lockhart, http://www.newmediacampaigns.com, lines 40-41
         */

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

        if (in_array($timestamp, $cleanDataArray)) {
        $calendar.= '<div class="day-number day-number-event"><a id="'.$timestamp.'" 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;

/* 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;
    }

我一直在尝试我的知识允许的每一种方式,但是我在 PHP 和 MySQL 方面有点受限。

4

0 回答 0