Skip to content

Instantly share code, notes, and snippets.

@manuman94
Created January 8, 2018 07:51
Show Gist options
  • Save manuman94/6da06a843f5aa5107f5470ba6da05578 to your computer and use it in GitHub Desktop.
Save manuman94/6da06a843f5aa5107f5470ba6da05578 to your computer and use it in GitHub Desktop.
Codeigniter, PHP: Generate date of birth drop downs with sessions in case of errors
// THIS FILE IS A CUSTOM CODEIGNITER CLASS FOR GENERATING DROPDOWNS INCLUDED FROM THE LIBRARIES FOLDER
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Dob_dropdown{
public function buildDayDropdown($name='',$id='',$value='')
{
$days='';
while ( $days <= '31')
{
if($days>0)
{
$day[$days]=$days;
}else{
$day['select a day'] = ''; // PUT ANY DEFAULT VALUE HERE
}
$days++;
}
$id = 'id='.$id;
return form_dropdown($name, $day, $value, $id);
}
function buildYearDropdown($name='',$id='',$value='')
{
$years = range(1900, date("Y"));
array_unshift($years, 'Select a year');
foreach($years as $year)
{
if($year == 'Select a year'){
$year_list['Select a year'] = ''; // PUT ANY DEFAULT VALUE HERE
}else{
$year_list[$year] = $year;
}
}
$id = 'id='.$id;
return form_dropdown($name, $year_list, $value, $id);
}
function buildMonthDropdown($name='',$id='',$value='')
{
$month=array(
'Select a month'=>'', // PUT ANY DEFAULT VALUE HERE
'01'=>'Jan',
'02'=>'Feb',
'03'=>'Mar',
'04'=>'Apr',
'05'=>'May',
'06'=>'Jun',
'07'=>'Jul',
'08'=>'Aug',
'09'=>'Sep',
'10'=>'Oct',
'11'=>'Nov',
'12'=>'Dec');
$id = 'id='.$id;
return form_dropdown($name, $month, $value, $id);
}
}
/* End of file dob_dropdown.php */
/* Location: ./application/libraries/dob_dropdown.php */
// ======================== CONTROLLER FILE NO ERRORS
$data['dropday'] = $this->dob_dropdown->buildDayDropdown('drop_day', 'drop_day');
$data['dropmonth'] = $this->dob_dropdown->buildMonthDropdown('drop_month', 'drop_month');
$data['dropyear'] = $this->dob_dropdown->buildYearDropdown('drop_year', 'drop_year');
$data['main_content'] = 'play_view';
$data['header'] = 'includes/page1_header_view';
$data['footer'] = 'includes/footer_view';
$this->load->view('includes/master_page_view', $data);
// ======================== CONTROLLER FILE IF ERRORS
$data['dropday'] = $this->dob_dropdown->buildDayDropdown('drop_day','drop_day', set_value('drop_day', $this->session->userdata('drop_day')));
$data['dropmonth'] = $this->dob_dropdown->buildMonthDropdown('drop_month','drop_month', set_value('drop_month', $this->session->userdata('drop_month')));
$data['dropyear'] = $this->dob_dropdown->buildYearDropdown('drop_year','drop_year', set_value('drop_year', $this->session->userdata('drop_year')));
$data['main_content'] = 'play_view';
$data['header'] = 'includes/page1_header_view';
$data['footer'] = 'includes/footer_view';
// ============================================= MERGE ARRAYS
$data = array_merge($data, $user_exists_result_array, $get_stats_result_array);
$this->load->view('includes/master_page_view', $data);
// ======================== VIEW FILE
if(form_error('drop_day') == TRUE || form_error('drop_month') == TRUE || form_error('drop_year') == TRUE)
{
echo '<p class="error">Please enter a valid date</p>';
}
echo '<label>Date of Birth* </label>';
echo $dropday;
echo $dropmonth;
echo $dropyear;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment