marunomaruno-memo

marunomaruno-memo

Smartyを使って、年月日の入力フォームを作る

2011年01月11日 | Weblog
2010-01-11
Smartyを使って、年月日の入力フォームを作る
======================================================================

■select_date_sample.php
---
<?php
require_once("/usr/share/Smarty/libs/Smarty.class.php");

//Smartyのインスタンス$tplの生成
$tpl = new Smarty();

$year = array();
$localtime_assoc = localtime(time(), true);
$this_year = $localtime_assoc["tm_year"] + 1900;
for ($i = $this_year - 50; $i <= $this_year; $i++) {
    $year[] = $i;    
}

$month = array();
for ($i = 1; $i <= 12; $i++) {
    $month[] = $i;    
}

$day = array();
for ($i = 1; $i <= 31; $i++) {
    $day[] = $i;    
}

//$tplにアサイン(登録)
$tpl->assign("year", $year);
$tpl->assign("month", $month);
$tpl->assign("day", $day);

//テンプレートに基づいて表示
$tpl->display("select_date_sample.html");

?>
---



■テンプレート select_date_sample.html
---
<!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>Insert title here</title>
</head>
<body>

<select name="year">
{foreach from=$year item=i}
<option value="{$i}">{$i}</option>
{/foreach}
</select>


<select name="month">
{foreach from=$month item=i}
<option value="{$i}">{$i}</option>
{/foreach}
</select>


<select name="day">
{foreach from=$day item=i}
<option value="{$i}">{$i}</option>
{/foreach}
</select>

</body>
</html>
---