CodeIgniter Forums
How do I use set_select with database values? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: How do I use set_select with database values? (/showthread.php?tid=18129)



How do I use set_select with database values? - El Forum - 04-26-2009

[eluser]RS71[/eluser]
Hey,

I was using form helpers to build my selects but I think I'm gonna switch typing them out on HTML.

What I had before:

Code:
<?= form_dropdown('sex', $sex_options, set_value('sex', (isset($database['basic_info']->sex)) ? $database['basic_info']->sex : '')); ?>

I'm having some problem understanding how to use database values with set_selects (uh.. its pretty late at night heh)

Could somebody assist me?

Basic, check if there is a validation error value, if not use a database value, if not use default (which in the code I posted above for example is '')

Thanks in advance


How do I use set_select with database values? - El Forum - 04-26-2009

[eluser]Thorpe Obazee[/eluser]
Quote:
Code:
<select name="visibility">
    <option value="visible" &lt;?php echo ($visibility == 'visible') ? set_select('visibility', 'visible', TRUE) : set_select('visibility', 'visible');?&gt;>Visible to everyone</option>
    <option value="invisible" &lt;?php echo ($visibility == 'invisible') ? set_select('visibility', 'invisible', TRUE) : set_select('visibility', 'invisible');?&gt;>Visible only to logged-in users</option>
</select>

$visibility is a database value.

This is what I usually do. I am not fond of form helpers.


How do I use set_select with database values? - El Forum - 04-26-2009

[eluser]RS71[/eluser]
Hey thanks for the tip

I was thinking, what if I edited the set_select function?

Here is the default code:

Code:
// --------------------------------------------------------------------
    
    /**
     * Set Select
     *
     * Enables pull-down lists to be set to the value the user
     * selected in the event of an error
     *
     * @access    public
     * @param    string
     * @param    string
     * @return    string
     */    
    function set_select($field = '', $value = '', $default = FALSE)
    {        
        if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
        {
            if ($default === TRUE AND count($this->_field_data) === 0)
            {
                return ' selected="selected"';
            }
            return '';
        }
    
        $field = $this->_field_data[$field]['postdata'];
        
        if (is_array($field))
        {
            if ( ! in_array($value, $field))
            {
                return '';
            }
        }
        else
        {
            if (($field == '' OR $value == '') OR ($field != $value))
            {
                return '';
            }
        }
            
        return ' selected="selected"';
    }

Perhaps a fourth parameter used for database values and modifying the first if statement?


How do I use set_select with database values? - El Forum - 04-26-2009

[eluser]Thorpe Obazee[/eluser]
I didn't look at your code but nothing prevents you from 'extending' it.


How do I use set_select with database values? - El Forum - 04-29-2009

[eluser]taro uma[/eluser]
I am doing this to create a dropdown menu of years, starting with the current year and looping to 10+ years. Is there a way to use the form helper set_select() with this so if the form is reloaded (like with errors) this dropdown remembers the user selected option? currently I have it set so that the current year is selected, but can't see a way to use set_select() with it.

Code:
&lt;?php
$year = date("Y");
$years = array();
for($i = $year; $i <= $year + 10; $i++){
    $years['$i']='$i';
}

echo form_dropdown('ccyear', $years, $year);
?&gt;