views:

73

answers:

3

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..

A: 

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)
Fanis
How would I do this the helper way round.. I've created my helper and loaded it in my controller but keep getting the undefined variable message. my view makes calls to the variables in the helper
Psychonetics
I'm afraid I don't have experience with CodeIgniter, only Kohana2, so I can't make an educated guess at what the issue could be. If you post your helper file as well as the exact PHP error about an undefined variable I might be able to help further.
Fanis
I posted it above thanks
Psychonetics
A: 

Helper file:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if ( ! function_exists('getdate'))
{
function getdate() {

//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;
}
}
}

Controller file:

<?php


class Join extends Controller {

    public function  __construct() {
        parent::controller();

        $this->load->helper(array('form','url'));
        $this->load->library('form_validation');


    }

    public function index() {

        $this->load->helper('join/datepicker');
        $this->datepicker->getdate();
        $this->load->view('join',$data);

    }
}

View file

<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Signup Form</title>
</head>
<body>


        <?php echo form_open('join/submit'); ?>

        <p>
            <label for="first_name">First Name: </label>
            <?php echo form_input('first_name'); ?>
        </p> 

        <p>
            <label for="last_name">Last Name: </label>
            <?php echo form_input('last_name'); ?>
        </p>             

            <p>
            <label for="email">E-mail: </label>
            <?php echo form_input('email'); ?>
        </p>

        <p>
            <label for="password">Password: </label>
            <?php echo form_password('password'); ?>
        </p>

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

                 <!-- gender drop down to go here -->

            <?php echo form_submit('submit','Sign Up'); ?>



        <?php echo form_close(); ?>


</body>
</html>

view:

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Join::$datepicker

Filename: controllers/join.php

Line Number: 18

Fatal error: Call to a member function getdate() on a non-object in /**my file path**/ on line 18

Psychonetics
A: 

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.

coolgeek
Thanks I didn't realise this answer was posted. I started fresh and got rid of that code.
Psychonetics