Welcome Guest, Not a member yet? Register   Sign In
Can't load variables from helper in controller, help is appreciated.
#1

[eluser]psychonetics[/eluser]
What I'm trying to do


I have a date dropdown on my signup form for my website. PHP is used to generate the dropdown data. Rather than have the data in my controller I put it in a helper because I will want to use it again through out my website.

Below is a copy of the code on my controller, helper and view.
I have also copied and pasted the text from my browser when I load my signup form page.



Controller

Code:
<?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:

Code:
&lt;?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);

    }
}
here's my corresponding code in my form in the view file:
Code:
<!DOCTYPE html>
&lt;html lang="en"&gt;
&lt;head&gt;
    &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
    &lt;title&gt;Signup Form&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;


        &lt;?php echo form_open('join/submit'); ?&gt;

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

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

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

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

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

                 &lt;!-- gender drop down to go here --&gt;

            &lt;?php echo form_submit('submit','Sign Up'); ?&gt;



        &lt;?php echo form_close(); ?&gt;


&lt;/body&gt;
&lt;/html&gt;

Here's the message i get when I load my page with the form on it..
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


So I see some variable(s) is/are not defined. I've been googling all day and finally decided to post and get some help.

Thanks in advance
#2

[eluser]bobbob[/eluser]
Have you thought of using jquery ui's datepicker? It is so easy to implement.

Then very easy to handle in your controller.

http://jqueryui.com/demos/datepicker/
#3

[eluser]psychonetics[/eluser]
Yep

but I would have to do some customization to make just the drop down show.. for now I would prefer to learn what I'm doing wrong so I can use it to help me in the future with other tasks.




Theme © iAndrew 2016 - Forum software by © MyBB