smartyãã©ã°ã¤ã³
smartyã使ã£ã¦ãphperã¯å¤ãã¨æãã¾ããããã©ã°ã¤ã³ãèªä½ãã¦ã人ã¯ããã¾ãããªãããã«æãã¾ããå®ã¯ã¯ã½ç°¡åã§è¶
便å©ãªã®ã§è¶
ããããã§ããã¦ããããsmartyã®æ¨æºãã©ã°ã¤ã³ã¯è²ã
å¾®å¦ãããã®ã§ã¡ããã£ã¨ã«ã¹ã¿ãã¤ãºããã ãã§smartyã®å©ä¾¿æ§ãæ ¼æ®µã«ãããã¾ãã
html_optionsãªããã¯ã¾ãã«ããã§ãé常ã«ä½¿ãåæãæªãã§ããé£æ³é
åã渡ãã¨selectã丸ãã¨çæãã¦ãããæ©è½ãããã®ã§ãããå®éãã¹ã¿ããã¾ããã¨å¼ã£å¼µã£ã¦ãããã¼ã¿ã¨ããã®ã¯é£æ³é
åã§ã¯ãªãã®ã§ãçµå±DBããåã£ã¦ããå¾ã«å å·¥ããå¿
è¦ãããã¾ããå
¨ç¶çååãã¦ããï¼ããããªãrowsãã¾ããã¨æãã¦foreachã§ãã£ãæ¹ãæ©ããã»ã»ã»ãã¨ãªã£ã¦ãã¾ãããã§ããããã§ãrowsã丸ãã¨æããã¨selectã«ãã¦ããããã©ã°ã¤ã³ãã¡ãã¡ãã£ã¨ä½ãã¾ããã
function smarty_function_select($params, &$smarty) { $column_id = isset($params['column_id']) ? $params['column_id'] : 'id'; $column_name = isset($params['column_name']) ? $params['column_name'] : 'name'; $name = isset($params['name']) ? $params['name'] : ''; $selected_value = isset($params['selected']) ? $params['selected'] : ''; $notext = isset($params['no_text']) ? $params['no_text'] : ''; $novalue = isset($params['no_value']) ? $params['no_value'] : ''; print "<select "; if(isset($params['id'])){ print " id=\"{$params['id']}\">"; } print 'name="'.htmlspecialchars($name).'">'; print '<option value="'.htmlspecialchars($novalue).'">'.htmlspecialchars($notext).'</option>'; if (isset($params['from'])) { foreach ($params['from'] as $item) { $selected = ($item[$column_id] == $selected_value) ? 'selected="selected"' : ''; print '<option value="'.htmlspecialchars($item[$column_id]).'" '.$selected.'>'.htmlspecialchars($item[$column_name]).'</option>'; } } print "</select>"; }
- before
<select name="hoge"> <option value="0">é¸æãªã</option> <option value="21"{ if $hoge == 21 } selected{ /if }>éé»é¢ä¿</option> <option value="22"{ if $hoge == 22 } selected{ /if }>æ²»å·¥å ·</option> <option value="23"{ if $hoge == 23 } selected{ /if }>å·¥ä½æ©æ¢°</option> <option value="24"{ if $hoge == 24 } selected{ /if }>測å®æ©æ¢°</option> <option value="25"{ if $hoge == 25 } selected{ /if }>ç£æ¥ç¨æ©æ¢°</option> <option value="26"{ if $hoge == 26 } selected{ /if }>AVæ©å¨</option> <option value="27"{ if $hoge == 27 } selected{ /if }>家é»</option> <option value="28"{ if $hoge == 28 } selected{ /if }>OAæ©å¨</option> <option value="29"{ if $hoge == 29 } selected{ /if }>éä¿¡æ©å¨</option> <option value="30"{ if $hoge == 30 } selected{ /if }>é»åé¨å</option> <option value="31"{ if $hoge == 31 } selected{ /if }>å®¶å ·</option> <option value="32"{ if $hoge == 32 } selected{ /if }>å¨æ¿</option> <option value="33"{ if $hoge == 33 } selected{ /if }>建ç¯åæ¨è³æ</option> <option value="34"{ if $hoge == 34 } selected{ /if }>輸éæ©å¨</option> <option value="35"{ if $hoge == 35 } selected{ /if }>輸éæ©å¨é»è£ å</option> <option value="36"{ if $hoge == 36 } selected{ /if }>å»çæ©å¨</option> <option value="37"{ if $hoge == 37 } selected{ /if }>ææ¿å ·é貨</option> <option value="38"{ if $hoge == 38 } selected{ /if }>ã¬ã¸ã£ã¼ç¨å</option> <option value="39"{ if $hoge == 39 } selected{ /if }>ç §æå¨å ·</option> <option value="40"{ if $hoge == 40 } selected{ /if }>äºåç¨æ©å¨</option> </select>
- after
$sql = 'select id, name from hoge'; $rs = $db->query($sql); $hoges = pg_fetch_all($rs); $smarty->assign('hoges ', $hoges); $smarty->assign('hoge', $hoge);
{select name="hoge" from=$hoges selected=$hoge}