CodeIgniter form_radio with set_value

From the user guide: https://www.codeigniter.com/user_guide/helpers/form_helper.html

form_radio()

This function is identical in all respects to the form_checkbox() function above except that is sets it as a "radio" type.

So reading further:

form_checkbox()

Lets you generate a checkbox field. Simple example:

echo form_checkbox('newsletter', 'accept', TRUE);

Would produce:

<input type="checkbox" name="newsletter" value="accept" checked="checked" />

The third parameter contains a boolean TRUE/FALSE to determine whether the box should be checked or not.

So in your case, it might be something like this:

// Pass boolean value to third param
// Example:
$radio_is_checked = $this->input->post('gender') === 'F';

echo form_radio('gender', 'F', $radio_is_checked, 'id=female');

Since set_radio() just returns a string checked="checked", you could wedge it in to the fourth paramter if you really wanted to but it makes for some ugly looking code:

echo form_radio('gender', 'F', NULL, 'id="female" '.set_radio('gender', 'F'));

I use Ternary Operator for this. Suppose you got return value of "M" (Male). I am making $selected variable for example and assume that it has value of M.

<tr>
    <td>
        <?php echo form_label('Gender: ', 'gender'); ?>
    </td>
    <td>
        <?php echo form_label('Male', 'male') . form_radio(array('name' => 'gender', 'value' => 'M', 'checked' => ('M' == $selected) ? TRUE : FALSE, 'id' => 'male')); ?><br>
        <?php echo form_label('Female', 'female') . form_radio(array('name' => 'gender', 'value' => 'F', 'checked' => ('F' == $selected) ? TRUE : FALSE, 'id' => 'female')); ?>
    </td>
    <td>
        <?php echo form_error('gender'); ?>
    </td>
</tr>

This worked well for me. Even though I didnt use form_helper, this was much easier way. Everyone face issue to take input for gender field :)

               <input id="gender" name="gender" type="radio" class="" <?php if($gender=='0') echo "checked='checked'"; ?> value="0" <?php echo $this->form_validation->set_radio('gender', 0); ?> />
                <label for="gender" class="">Male</label>

                <input id="gender" name="gender" type="radio" class="" <?php if($gender=='1') echo "checked='checked'"; ?> value="1" <?php echo $this->form_validation->set_radio('gender', 1); ?> />
                <label for="gender" class="">Female</label>

                <input id="gender" name="gender" type="radio" class="" <?php if($gender=='2') echo "checked='checked'"; ?> value="2" <?php echo $this->form_validation->set_radio('gender', 2); ?> />
                <label for="gender" class="">Others</label>

I hope it will help someone :)