重新开始生日下拉菜单

时间:2013-01-18 00:57:03

标签: php codeigniter

当验证失败并且页面重新加载时,我无法弄清楚如何在CodeIgniter中重新填充我的生日下拉帮助器。我试图使用set_value('field_name')它比我的其他下降更多一些因为涉及三个元素。

我的代码可用here

提前致谢

以下是验证规则

$this->form_validation->set_rules('relation', 'Relation', 'trim|required');
$this->form_validation->set_rules('location', 'Location', 'trim|required');
$this->form_validation->set_rules('city', 'City', 'trim|required');
$this->form_validation->set_rules($birthdate, 'Birthday', 'trim|required');
$this->form_validation->set_rules('gender', 'Gender', 'trim|required');

2 个答案:

答案 0 :(得分:2)

这是我用来验证生日(并重新填充下拉列表)的精简版本。我在这里有一些逻辑你可能想删除。

验证规则:

$this->form_validation->set_rule('birthdate-month','Birthdate Month','required|is_natural_no_zero|greater_than[0]|less_than[13]');
$this->form_validation->set_rule('birthdate-day','Birthdate Day','required|is_natural_no_zero|greater_than[0]|less_than[32]');
$this->form_validation->set_rule('birthdate-year','Birthdate Year','required|is_natural_no_zero|greater_than[1930]|less_than['.(date("Y") - 18).']');

控制器:

if ($this->form_validation->run()) {
    $birthdate = array(
        $this->form_validation->set_value('birthdate-year'),
        $this->form_validation->set_value('birthdate-month'),
        $this->form_validation->set_value('birthdate-day')
    );

    $data = array(
        'birthdate' => implode("-", $birthdate)
    );
}

// Then pass $data to your model and do whatever you want, i.e.:
$this->user_model->update_user($data);

然后是视图中的PHP / HTML:

<select name="birthdate-month" id="birthdate-month">
    <option value=""></option>
    <?php foreach($months as $month_number => $month_name) { ?>
    <option value="<?php echo $month_number; ?>" <?php echo set_select('birthdate-month', $month_number); ?>><?php echo $month_name; ?></option>
    <?php } ?>
</select>

<select name="birthdate-day" id="birthdate-day">
    <option value=""></option>
    <?php for($i=1; $i<=31; $i++) { ?>
    <option value="<?php echo $i; ?>" <?php echo set_select('birthdate-day', $i); ?>><?php echo $i; ?></option>
    <?php } ?>
</select>

<select name="birthdate-year" id="birthdate-year">
    <option value=""></option>
    <?php for($i=(date("Y") - 18); $i>=1930; $i--) { ?>
    <option value="<?php echo $i; ?>" <?php echo set_select('birthdate-year', $i); ?>><?php echo $i; ?></option>
    <?php } ?>
</select>

另外,仅供参考,$months是一个数组:

array(
    '01' => 'January',
    '02' => 'February',
    '03' => 'March',
    '04' => 'April',
    '05' => 'May',
    '06' => 'June',
    '07' => 'July',
    '08' => 'August',
    '09' => 'September',
    '10' => 'October',
    '11' => 'November',
    '12' => 'December'
);

答案 1 :(得分:1)

文件说明:

  

<强> set_select()

     

如果使用菜单,此功能允许您显示所选的菜单项。第一个参数必须包含选择菜单的名称,第二个参数必须包含每个项目的值,第三个(可选)参数允许您将项目设置为默认值(使用布尔值TRUE / FALSE)。

示例:

<select name="myselect">
<option value="one" <?php echo set_select('myselect', 'one', TRUE); ?> >One</option>
<option value="two" <?php echo set_select('myselect', 'two'); ?> >Two</option>
<option value="three" <?php echo set_select('myselect', 'three'); ?> >Three</option>
</select>