Welcome Guest, Not a member yet? Register   Sign In
Best way to populate an edit form from a database
#10

[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:
&lt;?php echo form_radio('active', '1', FALSE, preset_radio('active', '1', $member->active)); ?&gt; <label for="active">Active</label>
&lt;?php echo form_radio('active', '0', FALSE, preset_radio('active', '0', $member->active)); ?&gt; <label for="active">Inactive</label>


The helper includes functions for select boxes and checkboxes as well: preset_select() and preset_checkbox()


Messages In This Thread
Best way to populate an edit form from a database - by El Forum - 04-30-2009, 09:19 AM
Best way to populate an edit form from a database - by El Forum - 04-30-2009, 10:14 AM
Best way to populate an edit form from a database - by El Forum - 04-30-2009, 10:25 AM
Best way to populate an edit form from a database - by El Forum - 05-01-2009, 10:01 AM
Best way to populate an edit form from a database - by El Forum - 05-22-2009, 10:15 AM
Best way to populate an edit form from a database - by El Forum - 05-24-2009, 02:02 AM
Best way to populate an edit form from a database - by El Forum - 05-24-2009, 07:19 PM
Best way to populate an edit form from a database - by El Forum - 01-05-2010, 07:10 PM
Best way to populate an edit form from a database - by El Forum - 01-05-2010, 07:33 PM
Best way to populate an edit form from a database - by El Forum - 01-06-2010, 07:02 AM
Best way to populate an edit form from a database - by El Forum - 01-08-2010, 08:20 AM
Best way to populate an edit form from a database - by El Forum - 02-15-2010, 07:06 AM
Best way to populate an edit form from a database - by El Forum - 03-31-2010, 05:58 AM
Best way to populate an edit form from a database - by El Forum - 03-31-2010, 09:03 AM
Best way to populate an edit form from a database - by El Forum - 03-23-2011, 11:33 AM
Best way to populate an edit form from a database - by El Forum - 07-06-2011, 01:40 PM
Best way to populate an edit form from a database - by El Forum - 07-06-2011, 01:45 PM
Best way to populate an edit form from a database - by El Forum - 07-06-2011, 02:57 PM



Theme © iAndrew 2016 - Forum software by © MyBB