Dynamic Select Plugin for ExpressionEngine

March 19th, 2009 in ExpressionEngine | add comments

About the Plug-in

The other day I needed to construct select fields for a form I was building on Self Storage website. It was pretty strait forward but time consuming so I thought if I could make a plug-in to do all that, it would save me the hassle of redoing this ever again.

I should also mention that there are two plug-ins I've found on at ExpressionEngine were similar but not quite suitable for what I needed. First one is BU Countries Select Countries Select Plugin and the other one is UK Counties Select Plugin. Unfortunately none of them was suitable for what I needed, so there you go, enjoy the time saver.

Installation

Download Dynamic Select Plugin and unzip the package into your ExpressionEngine plug-ins folder.

Below is the actual code if you want to have a quick look.

<?php
/*
=======================================================
= Author: Cem Meric =
= http://webunder.com.au =
=======================================================
= File: pi.dynamic_select.php =
=-----------------------------------------------------=
= Compatibility: ExpressionEngine 1.6.7 =
=-----------------------------------------------------=
= Purpose: Dynamically creates date and =
= time drop down list for forms =
=-----------------------------------------------------=
= Version History: =
= 1.0.0 - 26 February 2009 =
= plugin first built =
=======================================================
*/


$plugin_info = array(
'pi_name' => 'Dynamic_select',
'pi_version' => '1.0',
'pi_author' => 'Cem Meric',
'pi_author_url' => 'http://webunder.com.au',
'pi_description' => 'Dynamically creates date and time drop down list for forms',
'pi_usage' => Dynamic_select::usage()
);


class Dynamic_select {

var $return_data = "";



/** ----------------------------------------
/** Constructor
/** ----------------------------------------*/

function Dynamic_select()
{
global $TMPL;
$parameter = $TMPL->fetch_param('type');
$start_range = $TMPL->fetch_param('start_range');
$end_range = $TMPL->fetch_param('end_range');
$id = $TMPL->fetch_param('name');
$selected = $TMPL->fetch_param('selected');

switch ($parameter)
{
case "years":
/*** the current year ***/
$selected = is_null($selected) ? date('Y') : $selected;

/*** range of years ***/
$r = range($start_range, $end_range);

/*** create the select ***/
$select = '<select name="'.$id.'" id="'.$id.'">'."\n";
foreach( $r as $year )
{
$select .= "\t<option value=\"$year\"";
$select .= ($year==$selected) ? ' selected="selected"' : '';
$select .= ">$year</option>\n";
}
$select .= '</select>';

$this->return_data = $select;
break;

case "months":
/*** array of months ***/
$months = array(
1=>'January',
2=>'February',
3=>'March',
4=>'April',
5=>'May',
6=>'June',
7=>'July',
8=>'August',
9=>'September',
10=>'October',
11=>'November',
12=>'December');

/*** current month ***/
$selected = is_null($selected) ? date('m') : $selected;

$select = '<select name="'.$id.'" id="'.$id.'">'."\n";
foreach($months as $key=>$mon)
{
$select .= "\t<option value=\"$key\"";
$select .= ($key==$selected) ? ' selected="selected"' : '';
$select .= ">$mon</option>\n";
}
$select .= '</select>';

$this->return_data = $select;
break;

case "days":
/*** range of days ***/
$r = range(1, 31);

/*** current day ***/
$selected = is_null($selected) ? date('d') : $selected;

$select = '<select name="'.$id.'" id="'.$id.'">'."\n";
foreach ($r as $day)
{
$select .= "\t<option value=\"$day\"";
$select .= ($day==$selected) ? ' selected="selected"' : '';
$select .= ">$day</option>\n";
}
$select .= '</select>';

$this->return_data = $select;
break;

case "hours":
/*** range of hours ***/
$r = range(1, 12);

/*** current hour ***/
$selected = is_null($selected) ? date('h') : $selected;

$select = '<select name="'.$id.'" id="'.$id.'">'."\n";
foreach ($r as $hour)
{
$select .= "\t<option value=\"$hour\"";
$select .= ($hour==$selected) ? ' selected="selected"' : '';
$select .= ">$hour</option>\n";
}
$select .= '</select>';

$this->return_data = $select;
break;

case "minutes":
/*** array of mins ***/
$minutes = array(0, 15, 30, 45);

$selected = in_array($selected, $minutes) ? $selected : 0;

$select = '<select name="'.$id.'" id="'.$id.'">'."\n";
foreach($minutes as $min)
{
$select .= "\t<option value=\"$min\"";
$select .= ($min==$selected) ? ' selected="selected"' : '';
$select .= ">".str_pad($min, 2, '0')."</option>\n";
}
$select .= '</select>';

$this->return_data = $select;
break;

case "clock":
$r = array('AM', 'PM');

/*** set the select minute ***/
$selected = is_null($selected) ? date('A') : strtoupper($selected);

$select = '<select name="'.$id.'" id="'.$id.'">'."\n";
foreach($r as $ampm)
{
$select .= "\t<option value=\"$t\"";
$select .= ($ampm==$selected) ? ' selected="selected"' : '';
$select .= ">$ampm</option>\n";
}
$select .= '</select>';

$this->return_data = $select;
break;

}
}

/* FUNCTION END */

// ----------------------------------------
// Plugin Usage
// ----------------------------------------

// This function describes how the plugin is used.
// Make sure and use output buffering

function usage()
{
ob_start();
?>

Examples:

{exp:dynamic_select type="days" name="days" selected="18"}

{exp:dynamic_select type="months" name="months" selected="6"}

{exp:dynamic_select type="years" start_range="2009" end_range="2015" name="years" selected="2012"}

{exp:dynamic_select type="hours" name="hours" selected="9"}

{exp:dynamic_select type="minutes" name="minutes" selected="30"}

{exp:dynamic_select type="clock" name="clock" selected="am"}


<?php
$buffer = ob_get_contents();

ob_end_clean();

return $buffer;
}
/* END */


}
// END CLASS
?>

Examples

ExpressionEngine tag

{exp:dynamic_select type="months" name="sel2" selected="1"}

Output

<select name="sel2" class="sel2">
<option value="1" selected="selected">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>

License

Dynamic Select Plugin is licensed under a Creative Commons Attribution 3.0 License.

Picture of Cem Meric

email | follow | peak

Cem Meric

Cem runs a creative web design and development studio called Webunder, specialising in ExpressionEngine CMS and he is an active member of the ExpressionEngine forums.

When he's not working, he enjoys being a hero with friends.

 

Commenting is not available in this entry.