Do I use a helper, library or place my datepicker code in a model in 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 开发者_C百科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..
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.
A helper sounds like it would work best for this. To do that look at the documentation and this SO question
On a sidenote, what I do with such generic code that may be used in more than one framework, application, or location, is the following:
- have a completely separate php file, say My_Utils.php
- create a class in there full of static functions (or use Namespaces if >= PHP 5.3)
- you do this to make sure your functions aren't defined elsewhere in any framework/other code
- include that in your CodeIgniter's index.php before any CodeIgniter initialization/routing
- this way they're available all across its codebase (controllers, views)
- use them as required
- keep it in a central location in each web server for maintenability and easy inclusion
In this case your form_dropdown
would simply be a static function in said file:
class My_Utils {
public static function form_dropdown($param1, $param2) {
}
}
And you'd use it:
echo My_Utils::form_dropdown('days',$days)
精彩评论