Welcome Guest, Not a member yet? Register   Sign In
Date form?
#1

[eluser]Wievior[/eluser]
Hello,

Has anyone developed something like form_date($date) that would generate a three inputs like [DD]-[MM]-[YYYY] or something similar?

Seems like making three different fields out of a date field (or six in datetime) isn't the right way.

It would definitely be nice to have this date fields generator.
#2

[eluser]Dam1an[/eluser]
I've nevr seen it, although now that you mention it, it seems like such an obvious thing to want to have
Can easily extend the form helper to add it yourself
#3

[eluser]Evil Wizard[/eluser]
I created a static php class to generate html drop downs for date and to pre select the date from a mysql formatted date, I think it also did time as well, it was a few years ago and I don't know how easy it would be to implement it in CI. I'll have a look for it and if I find it, I'll attach it to the thread.
#4

[eluser]Wievior[/eluser]
Dam1an - it sure does seem like it would be useful.

I just wanted to find out whether there was something like it already done before writing it myself.

Evil Wizard - thanks in advance for any code ;]
#5

[eluser]davidbehler[/eluser]
I just read this thread and wanted to see if I can come up with a quick solution and it was not that hard actually:

Code:
if ( ! function_exists('form_date'))
{
    function form_date($name = '', $selected = array(), $extra = '')
    {
        if ( ! is_array($selected))
        {
            $selected = array($selected);
        }

        // If no selected state was submitted we will attempt to set it automatically
        if (count($selected) === 0)
        {
            // If the form name appears in the $_POST array we have a winner!
            if (isset($_POST[$name]))
            {
                $selected = $_POST[$name];
            }
        }

        if ($extra != '') $extra = ' '.$extra;

        $form = '<select name="'.$name.'[day]"'.$extra.">\n";

        for($i = 1; $i <= 31; $i++)
        {
            if(isset($selected['day']))
            {
                $value = $selected['day'];
            }
            else
            {
                $value = '';
            }
            $sel = $value == $i ? ' selected="selected"' : '';
            $form .= '<option value="'.$i.'"'.$sel.'>'.$i."</option>\n";
        }

        $form .= "</select>\n";

        $form .= '<select name="'.$name.'[month]"'.$extra.">\n";

        for($i = 1; $i <= 12; $i++)
        {
            if(isset($selected['month']))
            {
                $value = $selected['month'];
            }
            else
            {
                $value = '';
            }
            $sel = $value == $i ? ' selected="selected"' : '';
            $form .= '<option value="'.$i.'"'.$sel.'>'.$i."</option>\n";
        }

        $form .= "</select>\n";

        $form .= '<select name="'.$name.'[year]"'.$extra.">\n";

        $max_year = date('Y');
        for($i = $max_year; $i >= 1900; $i--)
        {
            if(isset($selected['year']))
            {
                $value = $selected['year'];
            }
            else
            {
                $value = '';
            }
            $sel = $value == $i ? ' selected="selected"' : '';
            $form .= '<option value="'.$i.'"'.$sel.'>'.$i."</option>\n";
        }

        $form .= "</select>\n";

        return $form;
    }
}

You can manually set the values for each dropdown using the selected param when calling the function:
Code:
e.g.
echo form_date('date', array('day' => 1, 'month' => 2, 'year' => 2009);
if no default is given the functions tries to determine the selected values using the $_POST variable.

I know this function needs some tweaking, e.g. the format of the date as in europe it's different to usa (day.month.year in europe/germany and month/day/year in usa) or the years that are shown in the dropdown that are now limited to 1900 till now, but someone might need a year in the future or further back.

Hope it's still useful for you.
#6

[eluser]jedd[/eluser]
[quote author="waldmeister" date="1243522159"]I just read this thread and wanted to see if I can come up with a quick solution and it was not that hard actually:[/quote]

I was thinking that it probably wouldn't be all that hard, just vaguely repetitive .. but nonetheless, impressive!

Quote:I know this function needs some tweaking, e.g. the format of the date as in europe it's different to usa (day.month.year in europe/germany and month/day/year in usa) ...

... and I was going to say earlier, that if anyone does write one - be sure to use [url="http://en.wikipedia.org/wiki/ISO_8601"]ISO 8601[/url] to get around the cross-pond confusion of date order. I'm a big advocate of this standard (or rather, a big hater of the totally illogical US method).
#7

[eluser]davidbehler[/eluser]
The function can be easily altered to use ISO 8601 instead of the current format:

Code:
if ( ! function_exists('form_date'))
{
    function form_date($name = '', $selected = array(), $extra = '')
    {
        if ( ! is_array($selected))
        {
            $selected = array($selected);
        }

        // If no selected state was submitted we will attempt to set it automatically
        if (count($selected) === 0)
        {
            // If the form name appears in the $_POST array we have a winner!
            if (isset($_POST[$name]))
            {
                $selected = $_POST[$name];
            }
        }

        if ($extra != '') $extra = ' '.$extra;

        $form = '<select name="'.$name.'[year]"'.$extra.">\n";

        $max_year = date('Y');
        for($i = $max_year; $i >= 1900; $i--)
        {
            if(isset($selected['year']))
            {
                $value = $selected['year'];
            }
            else
            {
                $value = '';
            }
            $sel = $value == $i ? ' selected="selected"' : '';
            $form .= '<option value="'.$i.'"'.$sel.'>'.$i."</option>\n";
        }

        $form .= "</select>\n&nbsp;-&nbsp;";

        $form .= '<select name="'.$name.'[month]"'.$extra.">\n";

        for($i = 1; $i <= 12; $i++)
        {
            if(isset($selected['month']))
            {
                $value = $selected['month'];
            }
            else
            {
                $value = '';
            }
            $sel = $value == $i ? ' selected="selected"' : '';
            $form .= '<option value="'.$i.'"'.$sel.'>'.$i."</option>\n";
        }

        $form .= "</select>\n&nbsp;-&nbsp;";

        $form .= '<select name="'.$name.'[day]"'.$extra.">\n";

        for($i = 1; $i <= 31; $i++)
        {
            if(isset($selected['day']))
            {
                $value = $selected['day'];
            }
            else
            {
                $value = '';
            }
            $sel = $value == $i ? ' selected="selected"' : '';
            $form .= '<option value="'.$i.'"'.$sel.'>'.$i."</option>\n";
        }

        $form .= "</select>\n";

        return $form;
    }
}
#8

[eluser]Wievior[/eluser]
Wow, that's extremely impressive!

What I came up with was using jquery and four inputs (year, month, day, hidden for the whole date) - not neat.

However, I can't quite make the one that you provided work with my code.

Controller:

Code:
...
var $fields = array(
    'data_ur' => array('name' => 'data_ur'),
);

function add()
    {
    $data = $this->fields;
    
    $this->validation->set_rules($this->rules);
    
    if ($this->validation->run() == FALSE)
        {
        foreach($this->fields AS $key => $value)
            {
            $data[$key]['value'] = $this->input->post($key);
            }
        $this->response['content'] = $this->load->view('admin/'.$this->dzial_views.'/add', $data, True);
        }
    else
        {
        $this->load->model('Zawodnik');
        $this->Zawodnik->add(array('data_ur' => $this->input->post('data_ur')));
        $this->response['content'] = '<div class="ok">Dodano '.$this->dzial_pojedyncza.'!</div>';
        $this->Logs->add($this->dzial, 'dodano (id:'.$this->db->insert_id().')');
        }
        $this->load->view('admin/index', $this->response);
    }
...

View:
Code:
&lt;?php echo form_open(uri_string()); ?&gt;
<table width="90%" border="0" cellspacing="3" cellpadding="3">
<tr><td width="180"><b>Data ur</b></td><td>&lt;?php echo form_date($data_ur); ?&gt;</td></tr>
<tr><td> </td><td>&lt;?php echo form_submit('submit', 'Dodaj'); ?&gt;</td></tr>
</table>
&lt;?php echo form_close(); ?&gt;
<div class="not_ok">&lt;?=$this->validation->error_string; ?&gt;</div>

Changes to your function:
Code:
if (isset($name))
            {
                $selected = $name;
            }

INSTEAD OF:

            if (isset($_POST[$name]))
            {
                $selected = $_POST[$name];
            }

I think that it has to be something with the arrays provided to the function, but I can't quite figure it out :/
#9

[eluser]davidbehler[/eluser]
So data_ur is your date you want to mark as selected when using the form_date function?

When calling the form_date function the first parameter is the name of the select input that will be created and the second is the array of values that should be selected. So instead of
Code:
&lt;?php echo form_date($data_ur); ?&gt;
try
Code:
&lt;?php echo form_date('data_ur'); ?&gt;

The selected variable that you can provide as second parameter to the form_date function must be in this format:
Code:
array('day' => number_of_the_day, 'month' => number_of_the_month, 'year' => the_year);

If you don't want to set a default, then you don't have to provide the second parameter. If the form was submitted and a validation error occured, then the form_date function will automatically try to determine the selected values using the $_POST variable.




Theme © iAndrew 2016 - Forum software by © MyBB