登録フォームなどがある画面で、入力値をチェックし問題がある場合は再度入力画面を表示する処理があると思います。
そのときに画面にドロップダウンがあると選択値を再設定する必要がありますが、CodeIgniter標準のform_dropdownでは値の再設定が不可能です。
対応方法としては既存機能を上書きで修正します。
環境:CodeIgniter v2.0.0
1.次の場所に「MY_form_helper.php」を作成する
/application/helpers/MY_form_helper.php
2.次のコードをMY_form_helper.phpに貼り付ける
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); /** * CodeIgniter * * An open source application development framework for PHP 5.1.6 or newer * * @package CodeIgniter * @author ExpressionEngine Dev Team * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. * @license http://codeigniter.com/user_guide/license.html * @link http://codeigniter.com * @since Version 1.0 * @filesource */ // ------------------------------------------------------------------------ /** * CodeIgniter MY Form Helpers * * @package CodeIgniter * @subpackage Helpers * @category Helpers * @author ExpressionEngine Dev Team * @link http://codeigniter.com/user_guide/helpers/form_helper.html */ // ------------------------------------------------------------------------ /** * Custom Drop-down Menu * * @access public * @param string * @param array * @param string * @param string * @return string */ if ( ! function_exists('form_dropdown')) { function form_dropdown($name = '', $options = array(), $selected = array(), $extra = '') { if (! is_array($selected)) { $selected = array($selected); } # Insert the following code //------------------------- // Check post if post is not empty if (! empty($_POST)) { // Post data exists. Default is select has no options selected. $selected = array(); // Strip the [] from the name if multiple $post_name = preg_replace('/\[\]/','',$name); // Check for value(s) for this select item by name in $_POST array if (isset($_POST[$post_name])) { // Check if multiple values if (is_array($_POST[$post_name])) { // Extract each posted item foreach ($_POST[$post_name] as $posted) { // Override default values with posted values $selected[] = $posted; } } else { // Single select value $selected = array($_POST[$post_name]); } } } // If no selected state was submitted we will attempt to set it automatically if (count($selected) === 0) { // If the form name appears in the $_POST array we have a winner! if (isset($_POST[$name])) { $selected = array($_POST[$name]); } } if ($extra != '') $extra = ' '.$extra; $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : ''; $form = '<select name="'.$name.'"'.$extra.$multiple.">\n"; foreach ($options as $key => $val) { $key = (string) $key; if (is_array($val) && ! empty($val)) { $form .= '<optgroup label="'.$key.'">'."\n"; foreach ($val as $optgroup_key => $optgroup_val) { $sel = (in_array($optgroup_key, $selected)) ? ' selected="selected"' : ''; $form .= '<option value="'.$optgroup_key.'"'.$sel.'>'.(string) $optgroup_val."</option>\n"; } $form .= '</optgroup>'."\n"; } else { $sel = (in_array($key, $selected)) ? ' selected="selected"' : ''; $form .= '<option value="'.$key.'"'.$sel.'>'.(string) $val."</option>\n"; } } $form .= '</select>'; return $form; } } /* End of file MY_form_helper.php */ /* Location: ./application/helpers/MY_form_helper.php */
3.通常通りform_dropdownを使います。