如何在下拉代码中检查条件?

时间:2015-02-26 10:48:13

标签: php codeigniter

我的codeigniter

中有以下视图
<li><label>Select Year: <em>*</em></label>
            <span>
                 <?php
                     $y= date('Y'); 
                     $y1=2009; 
                     $years['']='Select';
                     while($y1<=$y) {                       
                        $years[$y1]=$y1;
                        $y1=$y1+1;
                     }
                     $config='id="year"';
                     echo form_dropdown('year',$years,set_value('year'),$config);
                ?>
            </span>         
        </li>

我有一个像这样的控制器

    $this->form_validation->set_rules('year', 'Year', 'required');
    $this->form_validation->set_rules('fees_month', 'Month', 'required');
    $this->form_validation->run();

我已经给出了

<li><label>Select Month<em>*</em></label>
                <?php
                 $select="fees_month";
                 $data=array(
                    ""=>'Select',
                    "1"=>"January",
                    "2"=>"February",
                    "3"=>"March",
                    "4"=>"April",
                    "5"=>"May",
                    "6"=>"June",
                    "7"=>"July",
                    "8"=>"August",
                    "9"=>"September",
                    "10"=>"October",
                    "11"=>"November",
                    "12"=>"December",
                );
                echo form_dropdown($select,$data,@$fees_month!=''? @$fees_month: set_value('fees_month'),'onchange="this.form.submit()"');
                ?>
            </span>

如果选择了一个月,我已提交表单。我需要的是如果没有选择年份它没有显示错误。 如果它处于默认选择模式,我想显示年份字段

1 个答案:

答案 0 :(得分:1)

您的代码中应该是if子句。

$this->form_validation->set_rules('year', 'Year', 'required');
$this->form_validation->set_rules('fees_month', 'Month', 'required');
if($this->form_validation->run()) {
    //Success
} else { //Return to your form view }