日历PHP脚本修改

时间:2012-10-29 14:07:28

标签: php mysql calendar pdo

我正在开发具有日历系统的项目。为了更有效地开发,我得到了日历代码from this site

原始代码很好:我做了一些修改,但是我已经陷入了我的项目所需的修改之一。由于日历使用数据库对话框,我希望它在特定日期内显示数据库表的一些列。例如,如果我在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方面受到限制。

0 个答案:

没有答案