Welcome Guest, Not a member yet? Register   Sign In
how to use set_radio with database values
#1

[eluser]RS71[/eluser]
Hello

I'm wondering how to set a default value for a radio input based on the following:

use post/validation value => if not use database value => if not use default value

I got this to work with a set_value by doing the following:

Code:
set_value(field_name, (isset($database->value)) ? $database->value : 'default_text')

But how would I exactly accomplish this using radios?

I currently have two options for the radio group. If there is no post value nor a database value I'd like to set one of them checked by default.

Could anyone please assist me?

Thank you in advance.
#2

[eluser]drewbee[/eluser]
set_radio determines if it is checked by default or not by passing a TRUE or FALSE to the third parameter.

I actually force the 'database' value to have a value if it is not set.


Code:
// If their is no database value, set value_1 to be the default value.
$database->value = isset($database->value) ? $database->value : 'value_2';

set_radio('radio_field', 'value_1', (($database->value == 'value_1') ? TRUE : FALSE));
set_radio('radio_field', 'value_2', (($database->value == 'value_2') ? TRUE : FALSE));

The above will cause 'value_2' radio to be checked if their is no database value. If the database value is 'value_1', the first option will be checked.

The value will be retained after the form is submited based on user selection.
#3

[eluser]Thorpe Obazee[/eluser]
Code:
<input type="radio" name="admins" value="<?php echo $admin->users_id;?>"
        <?php
        echo ($admin->users_id == $user_admins)
        ? set_radio('admin', $admin->users_id, TRUE)
        : set_radio('admin', $admin->users_id);
        ?>
        class="radio"/>

This is what I do. It doesn't need to have you force a default value from the database
#4

[eluser]drewbee[/eluser]
Aye, Speaking the same words with a different language Smile
#5

[eluser]RS71[/eluser]
I'm having a hard time visualizing this. Would one of you be so kind as to explain it for me?
#6

[eluser]drewbee[/eluser]
What are you failing to understand? The third parameter of the set_radio function is passed a TRUE to set the default value. You simply have to do some logic checks to see if the default OR database value is equal to the current radio button value. If it is, we pass true to it.
#7

[eluser]JulianM[/eluser]
I use this:

Code:
<li>
            <label>Status</label>
            <div>
                &lt;?php
                    // set default value
                    $value = set_value('status', @$row->status);
                    $row->status = (!empty($value) ? $value : '0' );
                ?&gt;
                <select name="status">
                <option value="0" &lt;?= set_select('status', '0', (($row->status=='0')?TRUE:FALSE) ); ?&gt; >Inactive</option>
                <option value="1" &lt;?= set_select('status', '1', (($row->status=='1')?TRUE:FALSE) ); ?&gt; >Active</option>
                </select>
                &lt;?= form_error('status'); ?&gt;
            </div>
        </li>


Julian




Theme © iAndrew 2016 - Forum software by © MyBB