***selectタグ [#eeea4884]
プラグインを書いていて、気づくと3行ほどのselectタグに十数行のコードを書いていました。なんだか虚しいですね。(一応他のプラグインにも使うけどね (^^; )
//cut
#region(こんな感じ)
#code(c){{{{
$form = new PluginXxxxxxForm(array('#dde','#ddeae5','#dde685'));
$select_data = array(
'win' => 'Windows(CRLF)',
'unx' => 'UNIX(LF)',
'mac' => 'Macintosh(CR)',
);
$select = $form->select($select_data, 'sequence', 'win', 0);
//////// 略 ///////
class PluginXxxxxxForm
{
var $c_count = 0;
var $s_count = 0;
var $bgcolor = array();
function PluginXxxxxxForm($bg_color)
{
$this->c_count = count($bg_color);
if ($this->c_count){
if ($this->c_count > 1) {
foreach($bg_color as $color){
$this->bgcolor[] = 'style="background-color:' . $color . ';"';
}
} else {
$this->bgcolor = array_fill(0, 30, 'style="background-color:' . $bg_color[0] . ';"');
}
} else {
$this->bgcolor = array_fill(0, 30, '');
}
}
function select($select_data, $s_name, $present, $equal = 1)
{
$select_list = '';
$count = 0;
foreach($select_data as $key => $s_data) {
$key_data = $equal ? $s_data : $key;
if ($present == $s_data){
$select_list .= " <option value=\"$key_data\" {$this->bgcolor[$count]} selected=\"selected\">$s_data</option>\n";
} else {
$select_list .= " <option value=\"$key_data\" {$this->bgcolor[$count]}>$s_data</option>\n";
}
$count++;
}
$select = " <select name=\"$s_name{$this->s_count}\">$select_list</select>\n";
$this->s_count++;
return $select;
}
}
}}}}
#endregion
#region(ちょっと修正)
#code(c){{{
function select($select_data, $s_name, $present, $equal = 1)
{
$select_list = '';
$count = 0;
foreach($select_data as $key => $s_data) {
$key_data = $equal ? $s_data : $key;
$selected = ($present == $key_data) ? ' selected="selected"' : '';
$select_list .= " <option value=\"$key_data\"{$this->bgcolor[$count]}$selected>$s_data</option>\n";
$count++;
}
$select = " <select name=\"$s_name{$this->s_count}\">$select_list</select>\n";
$this->s_count++;
return $select;
}
}}}
#endregion
-実際にはカラー部分も普通の関数名にして簡単に変更できるようにして使っています。'07/02/06