カレンダー制作 vol.4

【授業内容】
カレンダー制作
【HTMLにまだPHPコードが残ってたからもうちょっと整理してみた】

○model

<?php
 	class calendar
	{
 	    public  $calendar_dates = array(
 	                                    'blanktop' => array(),
 	                                    'dates' => array(),
 	                                    'blankbottom' => array()
	                                    );
 	   
 	    public $calendar_contents = array();
	   
 	    public $select_years = array();
 	   
 	    public $select_months = array();
 	   
	   
 	    public function execute() {
 	       
 	        //「表示する」ボタンが押されたら、
 	        if (isset($_POST["submit"])) {
 	
 	            //月の始めまで、余白を★にする($calendar_datesに格納1)
            $w_top = date('w',mktime(0, 0, 0, $_POST["month"], 1, $_POST["year"]));
 	            for($i = 0 ; $i < $w_top; $i++){
 	                $this->calendar_dates['blanktop'][] = "";
 	            }
 	
 	            //月ごとの日にちを取ってくる($calendar_datesに格納2)
 	            for($day = 1; checkdate($_POST["month"], $day, $_POST["year"]); $day++) {
 	                $this->calendar_dates['dates'] []= $day;
	                $lastday = $day;
 	            }
	           
 	            //月の終わりまで、余白を★にする。($calendar_datesに格納3)
 	            $w_end = date('w', mktime(0, 0, 0, $_POST["month"], $lastday, $_POST["year"]));
 	            for($i = 0; $i < 6-$w_end; $i++) {
 	                $this->calendar_dates['blankbottom'][] = "";
 	            }
 	
 	            //月初めの★をHTMLタグに入れる($calendar_contentsに格納1)
 	            $blank_key = 0;
 	
 	            foreach  ($this->calendar_dates['blanktop'] as $key => $star) {
 	                $this->calendar_contents[] =  "<td class='calendar-blank' align='center'>
 	                                               $star</td>";
 	                $blank_key = $key + 1;
 	            }
 	
 	            //日付をHTMLタグに入れる($calendar_contentsに格納2)
 	            foreach ($this->calendar_dates["dates"] as $key => $date) {
 	                if (($blank_key + $key) % 7 == 0) {
	                    $this->calendar_contents[] = "</tr><tr>";
 	                    $this->calendar_contents[] = "<td class='calendar-sunday' align='center'>
                                                          $date</td>";
 	                } elseif (($blank_key + $key) % 7 == 6) {
 	                    $this->calendar_contents[] = "<td class='calendar-saturday' align='center'>
 	                                                  $date</td>";
 	                } else {
 	                    $this->calendar_contents[] = "<td class='calendar-weekday' align='center'>
 	                                                  $date</td>";
 	                }
 	            }
 	            //月終わりの★をHTMLタグに格納($calendar_contentsに格納3)
 	            foreach  ($this->calendar_dates["blankbottom"] as $star) {
 	                $this->calendar_contents[] = "<td class='calendar-blank' align='center'>
 	                                              $star</td>";
 	            }
	        }
 	
 	        //select(年)の値を作る
 	        $year = 1970;
 	        while ($year <= 2030) {
 	            if ($year == 2009) {
 	                $this->select_years[] = "<option value='$year' selected>$year</option>";
 	            } else {
	                $this->select_years[] = "<option value='$year'>$year</option>";
 	            }
 	            $year++;
 	        }
 	
 	        //select(月)の値を作る
 	        $month = 1;
 	        while ($month <= 12) {
 	            $this->select_months[] = "<option value='$month'>$month</option>";
 	            $month++;
	        }
 	    }
 	
 	
	    //HTMLファイルの呼び出し
 	    public function render($path) {
 	        require_once($path);
 	    }
 	}
 	
 	?>

○view

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 	"http://www.w3.org/TR/html4/loose.dtd">
 	<html>
 	<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 	     <title>かれんだー</title>
 	<link href="./calendar.css" rel="stylesheet" type="text/css" />
	</head>
 	<body>
 	 <h1>CALENDAR</h1>
 	<form aciton="calendar_index.php?module=main&action=calendar" method="POST">
 	
 	<select name="year">
 	<?php

 	      foreach ($this->select_years as $select_year) {
 	          echo $select_year;
      }
 	
 	?>
 	</select><select name="month">
 	<?php
 	
 	      foreach ($this->select_months as $select_month) {
 	          echo $select_month;
 	      }
 	
 	?>
 	</select><input type="submit" name="submit" value="表示する!">
 	</form>
 	
 	<div class="calendar">
 	  <table class="calendar" border="4" cellspacing="1" cellpadding="10">
 	    <caption>
 	             <?php echo isset($_POST["year"]) ? $_POST["year"] : " ";?><?php echo isset($_POST["month"]) ? $_POST["month"] : " ";?></caption>
	          <tr>
	            <th class="calendar-sunday" align='center'></td>
	            <th class="calendar-weekday" align='center'></td>
 	            <th class="calendar-weekday" align='center'></td>
 	            <th class="calendar-weekday" align='center'></td>
 	            <th class="calendar-weekday" align='center'></td>
 	            <th class="calendar-weekday" align='center'></td>
 	            <th class="calendar-saturday" align='center'></td>
 	         </tr>
 	         <tr>
 	<?php
 	foreach ($this->calendar_contents as $calendar_content) {
	    echo $calendar_content;
 	}
 	?>
 	            </tr>
 	     </table>
	
 	</body>
 	</html>



以上、今日の授業でした!