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 に答える