2013-10-07 62 views
0

我試圖在php中創建一個日曆,並希望所選的月份顯示一張圖片(例如一月份爲一月份,另一月份爲二月份等)。我想要從名爲'月'的文件夾中提取圖片。我怎麼做?在日曆中插入照片

這是我的代碼:

<?php 
class Calendar 
{ 
    var $events; 

    function Calendar($date) 
    { 
     if(empty($date)) $date = time(); 
     define('NUM_OF_DAYS', date('t',$date)); 
     define('CURRENT_DAY', date('j',$date)); 
     define('CURRENT_MONTH_A', date('F',$date)); 
     define('CURRENT_MONTH_N', date('n',$date)); 
     define('CURRENT_YEAR', date('Y',$date)); 
     define('START_DAY', (int) date('N', mktime(0,0,0,CURRENT_MONTH_N,1, CURRENT_YEAR)) - 1); 
     define('COLUMNS', 7); 
     define('PREV_MONTH', $this->prev_month()); 
     define('NEXT_MONTH', $this->next_month()); 
     $this->events = array(); 
    } 

    function prev_month() 
    { 
     return mktime(0,0,0, 
       (CURRENT_MONTH_N == 1 ? 12 : CURRENT_MONTH_N - 1), 
       (checkdate((CURRENT_MONTH_N == 1 ? 12 : CURRENT_MONTH_N - 1), CURRENT_DAY, (CURRENT_MONTH_N == 1 ? CURRENT_YEAR - 1 : CURRENT_YEAR)) ? CURRENT_DAY : 1), 
       (CURRENT_MONTH_N == 1 ? CURRENT_YEAR - 1 : CURRENT_YEAR)); 
    } 

    function next_month() 
    { 
     return mktime(0,0,0, 
       (CURRENT_MONTH_N == 12 ? 1 : CURRENT_MONTH_N + 1), 
       (checkdate((CURRENT_MONTH_N == 12 ? 1 : CURRENT_MONTH_N + 1) , CURRENT_DAY ,(CURRENT_MONTH_N == 12 ? CURRENT_YEAR + 1 : CURRENT_YEAR)) ? CURRENT_DAY : 1), 
       (CURRENT_MONTH_N == 12 ? CURRENT_YEAR + 1 : CURRENT_YEAR)); 
    } 

    function getEvent($timestamp) 
    { 
     $event = NULL; 
     if(array_key_exists($timestamp, $this->events)) 
      $event = $this->events[$timestamp]; 
     return $event; 
    } 

    function addEvent($event, $day = CURRENT_DAY, $month = CURRENT_MONTH_N, $year = CURRENT_YEAR) 
    { 
     $timestamp = mktime(0, 0, 0, $month, $day, $year); 
     if(array_key_exists($timestamp, $this->events)) 
      array_push($this->events[$timestamp], $event); 
     else 
      $this->events[$timestamp] = array($event); 
    } 

    function makeEvents() 
    { 
     if($events = $this->getEvent(mktime(0, 0, 0, CURRENT_MONTH_N, CURRENT_DAY, CURRENT_YEAR))) 
      foreach($events as $event) echo $event.'<br />'; 
    } 



    function makeCalendar() 
    { 
     echo '<table border="1" cellspacing="4"><tr>'; 
       echo '<td colspan="7" style="text-align:center"> <img src= \"img/months/$month.jpg\" > </td>'; 
       echo '</tr><tr>'; 
     echo '<td width="30"><a href="?date='.PREV_MONTH.'">&lt;&lt;</a></td>'; 
     echo '<td colspan="5" style="text-align:center">'.CURRENT_MONTH_A .' - '. CURRENT_YEAR.'</td>'; 
     echo '<td width="30"><a href="?date='.NEXT_MONTH.'">&gt;&gt;</a></td>'; 
     echo '</tr><tr>'; 
     echo '<td width="30">Mon</td>'; 
     echo '<td width="30">Tue</td>'; 
     echo '<td width="30">Wed</td>'; 
     echo '<td width="30">Thu</td>'; 
     echo '<td width="30">Fri</td>'; 
     echo '<td width="30">Sat</td>'; 
     echo '<td width="30">Sun</td>'; 
     echo '</tr><tr>'; 

     echo str_repeat('<td>&nbsp;</td>', START_DAY); 

     $rows = 1; 

     for($i = 1; $i <= NUM_OF_DAYS; $i++) 
     { 
      if($i == CURRENT_DAY) 
       echo '<td style="background-color: #C0C0C0"><strong>'.$i.'</strong></td>'; 
      else if($event = $this->getEvent(mktime(0, 0, 0, CURRENT_MONTH_N, $i, CURRENT_YEAR))) 
       echo '<td style="background-color: #99CCFF"><a href="?date='.mktime(0,0,0,CURRENT_MONTH_N,$i,CURRENT_YEAR).'">'.$i.'</a></td>'; 
      else 
       echo '<td>'.$i.'</td>'; 

      if((($i + START_DAY) % COLUMNS) == 0 && $i != NUM_OF_DAYS) 
      { 
       echo '</tr><tr>'; 
       $rows++; 
      } 
     } 
     echo str_repeat('<td>&nbsp;</td>', (COLUMNS * $rows) - (NUM_OF_DAYS + START_DAY)).'</tr></table>'; 
    } 
} 


$epcMonthPic = date("m", mktime(0,0,0,$mo)); //change the "F" for "m" if you want to use numbers 
$epcImagePath = "/img/"; // the path to your monthly images (keep the trailing slash) 
$epcImageExt = "jpg"; // the extension you'll be using for your images 
echo "<img src=\"$epcImagePath$epcMonthPic.$epcImageExt\">"; 

$month = $_REQUEST["month"]; 

$cal = new Calendar($_GET['date']); 

$cal->makeCalendar(); 



?> 

回答

0

在該方法中makeCalendar()修復行:

echo '<td colspan="7" style="text-align:center"> <img src= \"img/months/$month.jpg\" > </td>'; 

有:

echo '<td colspan="7" style="text-align:center"> <img src="img/months/' . CURRENT_MONTH_N . '.jpg" > </td>'; 

,並確保圖像存在:

img/months/1.jpg 
img/months/2.jpg 
... 
img/months/12.jpg