Welcome Guest, Not a member yet? Register   Sign In
Build Date Dropdown helper
#1

[eluser]egoslip[/eluser]
Hey so I looked around for an easy way of having date dropdowns created and selected with a predefined value. so I made this simple helper to create the drop downs for day month and year

hope this helps some people I just couldnt stand all that html in my pretty view file Smile

Code:
<?

if (!defined('BASEPATH')) exit('No direct script access allowed');
    
    function buildDayDropdown($name='',$value='')
    {
        $days='';
        while ( $days <= '31'){
            $day[]=$days;$days++;
        }
        return form_dropdown($name, $day, $value);
    }
    
    function buildYearDropdown($name='',$value='')
    {
        $years=date('y');
        while ( $years <= '31'+date('y')){
            $year['20'.$years]='20'.$years;$years++;
        }
        return form_dropdown($name, $year, $value);
    }
    
    function buildMonthDropdown($name='',$value='')
    {
        $month=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');
        return form_dropdown($name, $month, $value);
    }
?&gt;
#2

[eluser]garymardell[/eluser]
No offence to you but i think this should be a helper not a library.
#3

[eluser]egoslip[/eluser]
ah yes thats the one .... sorry about that still getting used to the whole structure of CI and havent really messed with helpers just yet so I just called it what I already knew how to do.. I would rename it to helper now thanks so much for your insight
#4

[eluser]coolgeek[/eluser]
doh, I wrote one of them too. I guess most people did.

Extending it, however, has anybody written any functions that ensure a valid date? I was looking to do that via a form_validation callback

thanks in advance
#5

[eluser]coolgeek[/eluser]
ah, as noted in multiple other threads, the PHP checkdate function does the job
#6

[eluser]stef25[/eluser]
im using egoslip's function as a helper after modifying the buildYearDropdown function slightly:

Code:
function buildYearDropdown($name='',$value='')
    {        
        $years = range(1900, date("Y"));
        foreach($years as $year)
        {
            $year_list[$year] = $year;
        }    
        
        return form_dropdown($name, $year_list, $value);
    }

called from controller with the 2nd parameter being the value that should be preselected in the drop down:

Code:
$data['dob_dropdown_year'] = buildYearDropdown('year', $data['old_profile_data']['year']);

and then printed out in the view via

Code:
&lt;?= $dob_dropdown_year ?&gt;


this generates a select field with name='year' and listing all years starting from 1900 till the current year and the values of the options are correct.

hope this helps some one!




Theme © iAndrew 2016 - Forum software by © MyBB