[eluser]Nexus Rex[/eluser]
[quote author="JulianM" date="1262763235"]Thanks, I think this is a good approach, but still thinking that this code could be placed in a helper to avoid duplicating the logic each time.
Code:
(1 == $member->active) ? set_radio('active', $member->active, TRUE) : set_radio('active', '1'))
What do you think?
Julian
[/quote]
Great idea — and done!
Instructions for benefit of any who would like to use this helper to easily preset select boxes, checkbox fields, and radio buttons with values from database (or elsewhere) with one simple call while retaining the ability of the form to remember submitted values from the POST.
1. Create a new file called "my_form_helper.php" in your /system/application/helpers/ directory.
2. Copy and paste this code into your new helper file:
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* Code Igniter
*
* An open source application development framework for PHP 4.3.2 or newer
*
* @package CodeIgniter
* @author Rick Ellis
* @copyright Copyright (c) 2006, pMachine, Inc.
* @license http://www.codeignitor.com/user_guide/license.html
* @link http://www.codeigniter.com
* @since Version 1.0
* @filesource
*/
// ------------------------------------------------------------------------
/**
* Code Igniter My Form Helpers
*
* @package CodeIgniter
* @subpackage Helpers
* @category Helpers
* @author Travis Cable aka Nexus Rex
* @link
*/
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
/**
* Preset Select
*
* Let's you preset the selected value of a checkbox field via info from a database
* and allows info the in the POST array to override.
*
* @access public
* @param string
* @param string
* @param string
* @return string
*/
if ( ! function_exists('preset_select'))
{
function preset_select($field = '', $value = '', $preset_value = '')
{
if ($value == $preset_value)
{
return set_select($field, $preset_value, TRUE);
}
else
{
return set_select($field, $value);
}
}
}
// ------------------------------------------------------------------------
/**
* Preset Checkbox
*
* Let's you preset the selected value of a checkbox field via info from a database
* and allows info the in the POST array to override.
*
* @access public
* @param string
* @param string
* @param string
* @return string
*/
if ( ! function_exists('preset_checkbox'))
{
function preset_checkbox($field = '', $value = '', $preset_value = '')
{
if ($value == $preset_value)
{
return set_checkbox($field, $preset_value, TRUE);
}
else
{
return set_checkbox($field, $value);
}
}
}
// ------------------------------------------------------------------------
/**
* Preset Radio
*
* Let's you preset the selected value of a radio field via info from a database
* and allows info the in the POST array to override.
* If Form Validation is active it retrieves the info from the validation class
*
* @access public
* @param string
* @param string
* @param string
* @return string
*/
if ( ! function_exists('preset_radio'))
{
function preset_radio($field = '', $value = '', $preset_value = '')
{
if ($value == $preset_value)
{
return set_radio($field, $preset_value, TRUE);
}
else
{
return set_radio($field, $value);
}
}
}
?>
3. Load the new helper in your Controller:
Code:
$this->load->helper('my_form');
4. Call the new preset_radio($field_name, $field_value, $preset_value) function in your radio buttons instead of set_radio():
Code:
<?php echo form_radio('active', '1', FALSE, preset_radio('active', '1', $member->active)); ?> <label for="active">Active</label>
<?php echo form_radio('active', '0', FALSE, preset_radio('active', '0', $member->active)); ?> <label for="active">Inactive</label>
The helper includes functions for select boxes and checkboxes as well: preset_select() and preset_checkbox()