カレンダー制作 vol.3
- 【授業内容】
- カレンダー制作
※前回掲示板でやったものを参考にMVCに分けてみた。
○index
<?php define('WEB_ROOT_DIR' , realpath(dirname('__FILE__').'/..')); //controllerを呼び出す。 require_once(WEB_ROOT_DIR.'/calendar/lib/controller.php'); $controller = new Controller(); $controller->dispatch(); ?>
○Controller
<?php class Controller { public $module; public $action; public function __construct() { $this->module = $_GET["module"]; $this->action = $_GET["action"]; } public function dispatch() { $act_file_name = sprintf(WEB_ROOT_DIR.'/calendar/%s/actions/%s.php' ,$this->module, $this->action); $view_file_name = sprintf(WEB_ROOT_DIR.'/calendar/%s/view/%s.php' ,$this->module, $this->action); if(file_exists($act_file_name) !== true) { } else { require_once($act_file_name); $action = new $this->action(); $action->execute(); $action->render($view_file_name); } } } ?>
○Model
<?php class calendar { public $calendar_dates = array( 'blanktop' => array(), 'dates' => array(), 'blankbottom' => 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'][] = "★"; } //var_dump($calendar_dates); //月ごとの日にちを取ってくる($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ファイルの呼び出し 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 $year = 1970; while ($year <= 2030) { if ($year == 2009) { echo "<option value='$year' selected>$year</option>"; } else { echo "<option value='$year'>$year</option>"; } $year++; } ?> </select> 年 <select name="month"> <?php $month = 1; while ($month <= 12) { echo "<option value='$month'>$month</option>"; $month++; } ?> </select> 月 <input type="submit" name="submit" value="表示する!"> </form> <div class="calendar"> <table class="calendar" border='5' bordercolor='green'> <caption><?php echo isset($_POST["month"]) ? $_POST["month"] : " ";?>月</caption> <tr> <td class="calendar-sunday">日</td> <td class="calendar-weekday">月</td> <td class="calendar-weekday">火</td> <td class="calendar-weekday">水</td> <td class="calendar-weekday">木</td> <td class="calendar-weekday">金</td> <td class="calendar-saturday">土</td> </tr> <tr> <?php $blank_key = 0; foreach ($this->calendar_dates['blanktop'] as $key => $star) { echo "<td class='calendar-blank' align='center'>$star</td>"; $blank_key = $key + 1; } foreach ($this->calendar_dates["dates"] as $key => $date) { if (($blank_key + $key) % 7 == 0) { echo "</tr><tr>"; echo "<td class='calendar-sunday' align='center'>$date</td>"; } elseif (($blank_key + $key) % 7 == 6) { echo "<td class='calendar-saturday' align='center'>$date</td>"; } else { echo "<td class='calendar-weekday' align='center'>$date</td>"; } } foreach ($this->calendar_dates["blankbottom"] as $star) { echo "<td class='calendar-blank' align='center'>$star</td>"; } ?> </tr> </table> </body> </html>
【CSSで土曜と日曜に色を付けてみる】
CSSを当てようとしたら、tdにクラスを設定してなかったことが発覚。
全てのファイルを直すことに…上記のコードが直したあとのもの。
以上、今日の授業でした!