php-tool-box:selection-builder
PHPでの選択リスト作成ヘルパー - SelectionBuilder
更新履歴とコメントはゼロと無限の間のログ » SelectionBuilder.phpへどうぞ。
選択リスト、セレクトボックス、ドロップダウンリスト。呼び名は様々だが、要はselect要素とoption要素で作るWebフォームで選択肢を提供するアレだ。
何回も作っているとだんだん面倒になり、ついつい部品化してしまう。フレームワークでこれを作れる機能があれば良いが、フレームワークを使うほどでもないような場合にはやはり手作り。毎回部品化するのも効率が悪いので、MITライセンスにして再利用しよう。
今回やりたかったのは、
- 数字の連番の選択リストを簡単に作れる
- 年月日の選択リストを簡単に作れる
- 年月日の選択リストはYahho Calendarと連携させる
- 同じ名前でPOSTされた値を再表示する
といったところ。今後必要に応じて改良するかも。
ソースコード
<?php /** * SelectionBuilder * @version 0.1.2 * @see http://0-oo.net/sbox/php-tool-box/selection-builder * @copyright 2008 dgbadmin@gmail.com * @license http://0-oo.net/pryn/MIT_license.txt (The MIT license) */ class SelectionBuilder { /** 連番・日付の選択リストの先頭に空の選択肢を入れるか */ public static $blankFlg = false; /** 日付選択リストのカレンダーアイコンへのパス */ public static $pathToCalIcon = ''; /** * 選択リストを作る * @param string $name 項目名 * @param array $options 選択肢(値 => ラベル) * @param string $default (省略可)初期表示で選択状態にする値 * @param array $attr (省略可)その他の属性(属性名 => 属性値) * @return string 選択リストのhtml */ public static function text($name, array $options, $default = '', array $attr = array()) { $h = SelectionBuilder::_getStartTag($name, $attr); foreach($options as $value => $text) { $h .= '<option' . SelectionBuilder::_getOptionAttr($name, $default, $value); $h .= ' value="' . SelectionBuilder::_h($value) . '">'; $h .= SelectionBuilder::_h($text) . "</option>\n"; } return $h . '</select>'; } /** * 連番数字の選択リストを作る * @param string $name 項目名 * @param integer $start 最初の数字 * @param integer $end 最後の数字 * @param integer $default (省略可)初期表示で選択状態にする値 * @param array $attr (省略可)その他の属性(属性名 => 属性値) * @return string 選択リストのhtml */ public static function num($name, $start, $end, $default = '', array $attr = array()) { $h = SelectionBuilder::_getStartTag($name, $attr); if (SelectionBuilder::$blankFlg) { $h .= "<option><!-- blank --></option>\n"; } for ($i = $start; $i <= $end; $i++) { $h .= '<option' . SelectionBuilder::_getOptionAttr($name, $default, $i) . ">$i</option>\n"; } return $h . '</select>'; } /** * 年月日の選択リストを作る(Yahho Calendar付き) * @param string $prefix 項目名の先頭部分 * @param string $default (省略可)初期表示する日付(strToTime()できる形式で) * @param integer $yearBefore (省略可)今年より何年前まで選択可能にするか * @param integer $yearAfter (省略可)今年より何年後まで選択可能にするか * @return string 日付選択リストのhtml */ public static function date($prefix, $default = 'today', $yBefore = 0, $yAfter = 1) { $idY = $prefix . '_y'; $idM = $prefix . '_m'; $idD = $prefix . '_d'; $ymd = strToTime($default); $y = date('Y', $ymd); $m = date('m', $ymd); $d = date('d', $ymd); $thisYear = date('Y'); $yStart = $thisYear - $yBefore; $yEnd = $thisYear + $yAfter; $onclick = "YahhoCal.render('" . $idY . "', '" . $idM . "', '" . $idD . "');return false;"; $h = ''; $h .= SelectionBuilder::num($idY, $yStart, $yEnd, $y, array('id' => $idY)) . "年 \n"; $h .= SelectionBuilder::num($idM, 1, 12, $m, array('id' => $idM)) . "月 \n"; $h .= SelectionBuilder::num($idD, 1, 31, $d, array('id' => $idD)) . "日"; if (SelectionBuilder::$pathToCalIcon) { //画像へのパスが設定されてれば画像ボタンを表示 $h .= '<input type="image" class="calendar" src="' . SelectionBuilder::$pathToCalIcon . '"'; $h .= ' onclick="' . $onclick . '" alt="カレンダーから選択" title="カレンダーから選択" />'; } else { //画像へのパスが設定されていなければ普通のボタンを表示 $h .= '<input type="button" onclick="' . $onclick . '" value="カレンダーから選択" />'; } return $h; } /** * select要素の開始タグを取得する * @param string $name 項目名 * @param array $attr 属性(属性名 => 属性値) * @return string 属性のhtml */ private static function _getStartTag($name, array $attr) { $attr['name'] = $name; $h = '<select'; foreach ($attr as $key => $value) { $h .= ' ' . $key . '="' . $value . '"'; } return $h . ">\n"; } /** * オプション要素のselected属性を取得する * @param string $name 項目名 * @param string $default 初期表示で選択状態にする値 * @param string $value オプション要素の値 * @return string 属性のhtml */ private static function _getOptionAttr($name, $default, $value) { $selected = $_REQUEST[$name]; if ($selected == '') { $selected = $default; } return ($value == $selected) ? ' selected="selected"' : ''; } /** * htmlエスケープ * @param string $value * @return string エスケープした文字列 */ private static function _h($value) { return htmlSpecialChars($value, ENT_QUOTES); } }
php-tool-box/selection-builder.txt · 最終更新: 2008/11/02 21:58 by dgbadmin