duanpenpan5796 2010-09-18 16:05
浏览 28
已采纳

我是否使用帮助器,库或将我的datepicker代码放在codeigniter中的模型中?

Heres my code:

//Setup days
 $data['days']['FALSE'] = 'Day';

 //Setup months
 $data['months'] = array('FALSE' => 'Month',
                 '1'  => 'Jan',
                 '2'  => 'Feb',
                 '3'  => 'Mar',
                 '4'  => 'Apr',
                 '5'  => 'May',
                 '6'  => 'Jun',
                 '7'  => 'Jul',
                 '8'  => 'Aug',
                 '9'  => 'Sep',
                 '10' => 'Oct',
                 '11' => 'Nov',
                 '12' => 'Dec'
                );


 for($i=1;$i<=31;$i++){
    $data['days'][$i] = $i;
 }

 //Setup years
 $start_year = date("Y",mktime(0,0,0,date("m"),date("d"),date("Y")-100)); //Adjust 100 to however many year back you want
 $data['years']['FALSE'] = 'Year';

 for ($i=$start_year;$i<=date("Y");++$i) {
    $data['years'][$i] = $i;
}

and here's my if statement:

if($this->form_validation->run()){
    $month = $this->input->post('months');
    $day = $this->input->post('days');
    $year = $this->input->post('years');

    $birthday = date("m-d-Y H:i:s",mktime(0,0,0,$month,$day,$year));
 } 

here's my corresponding code in my form in the view:

   <p>
        <label for="birthday">Birthday: </label>
        <?php echo form_dropdown('days',$days). " " . form_dropdown('months',$months). " " . form_dropdown('years',$years); ?>
    </p>

When I put the datepicker code directly in my controller the drop down on my form shows correctly. I wanted to be neat with my coding so decided to create a model and attempted to make calls from my controller but I get the undefined variable error a few times when my view tries to display the form.

I've done some research and wondering whether it's better to make my datepicker a helper "datepicker_helper.php" put the code in there and load it in my controller.

Is this the best way to do this?

If so can somebody give me a example of how I can do this? If there is another way can somebody show me please?

Thanks in advance.. I'm here to learn..

  • 写回答

2条回答 默认 最新

  • duanlaiquan8174 2010-09-19 15:59
    关注

    A lot of developers - myself included - have developed their own version of this code. There are two issues I want to point out here.

    First of all, you've lulled yourself into a false sense of security here. Bounded select elements can not and will not guarantee that every request includes a valid date. Never trust input. Always validate. Extend the Form_validation class with your own date validation functions.

    Second, making a user pull three dropdowns (without the mitigating guarantee of a valid date in all requests) is pretty crappy UX, especially in cases where the user needs to populate multiple dates (start date/end date, milestone dates, etc).

    After I came to understand these two things, I refactored this out of my site. Now my date fields are input type text, with a label that specifies the expected format of the date. My Form_validation class is extended to validate date fields. And I've added a real datepicker to my forms.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值