Welcome Guest, Not a member yet? Register   Sign In
Setting radio button values from database for edit form
#1

[eluser]barrysampson[/eluser]
Hi,

I'm steadily plodding on with my first CI project, and have hit another little problem that has me stumped.

User are asked to complete a form which has 30 questions, for which they have to select one of four answers. I have this working just fine, and I'm using serialize() to store the data in the db. I can get the data back out in the right format using unserialize() but I can't work out how to repopulate the edit form using the values I've pulled from the db.

I won't post the whole form here, but here's the code I'm using in my submit form to record the answer to 1 of the 30 questions.

I'm using a copy of that form to create my edit form but I'm not sure what I need to do to set the radio button values using the the values from the db.

Code:
<tr align="center">
            <td> </td>
            <td>
                &lt;?php
                $selection = array(
                    'name' => 'a1',
                    'id' => 'a1',
                    'value' => 0, //None
                    'checked' => FALSE
                    );
                echo form_radio($selection) ."</p>";
                ?&gt;
            </td>
            <td>
                &lt;?php
                $selection = array(
                    'name' => 'a1',
                    'id' => 'a1',
                    'value' => 1, //Basic
                    'checked' => FALSE
                    );
                echo form_radio($selection) ."</p>";
                ?&gt;
            </td>
            <td>
                &lt;?php
                $selection = array(
                    'name' => 'a1',
                    'id' => 'a1',
                    'value' => 2,  //Intermediate
                    'checked' => FALSE
                    );
                echo form_radio($selection) ."</p>";
                ?&gt;
            </td>
            <td>
                &lt;?php
                $selection = array(
                    'name' => 'a1',
                    'id' => 'a1',
                    'value' => 3, //Advanced
                    'checked' => FALSE
                    );
                echo form_radio($selection) ."</p>";
                ?&gt;
            </td>
        </tr>

Thanks for any help you can offer.
#2

[eluser]Evil Wizard[/eluser]
Code:
<tr align="center">
    <td>
        &lt;?php
        $selection = array(
        'name' => 'a1',
        'id' => 'a1',
        'value' => 0 //None
        );
        echo form_radio($selection, $selection['value'], set_radio($selection['name'], $selection['value'], TRUE)) ."</p>";
        ?&gt;
    </td>
    <td>
        &lt;?php
        $selection = array(
        'name' => 'a1',
        'id' => 'a1',
        'value' => 1 //Basic
        );
        echo form_radio($selection, $selection['value'], set_radio($selection['name'], $selection['value'])) ."</p>";
        ?&gt;
    </td>
    <td>
        &lt;?php
        $selection = array(
        'name' => 'a1',
        'id' => 'a1',
        'value' => 2  //Intermediate
        );
        echo form_radio($selection, $selection['value'], set_radio($selection['name'], $selection['value'])) ."</p>";
        ?&gt;
    </td>
    <td>
        &lt;?php
        $selection = array(
        'name' => 'a1',
        'id' => 'a1',
        'value' => 3 //Advanced
        );
        echo form_radio($selection, $selection['value'], set_radio($selection['name'], $selection['value'])) ."</p>";
        ?&gt;
    </td>
</tr>
Just remember to add the radio option to the form validation, you can leave the validation rule blank but the input needs to be added to the validation array or the "set_radio()" function won't pick up the re-posted value

hope that helps
#3

[eluser]barrysampson[/eluser]
Thanks Evil Wizard. I shall give that a try Smile
#4

[eluser]barrysampson[/eluser]
Ok, I know I'm probably missing something really obvious here, but... Now I've looked at this properly I don't get how I pass in the values that I've pulled from the db.
#5

[eluser]Evil Wizard[/eluser]
I think that's my fault, I must have missed off a line
Code:
<td>
        &lt;?php
        $selection = array(
        'name' => 'a1',
        'id' => 'a1',
        'value' => 0 //None
        );
    $checked = ($dbrecord->current_question->answer == selection['value']) ? TRUE : FALSE
        echo form_radio($selection, $selection['value'], set_radio($selection['name'], $selection['value'], $checked)) ."</p>";
        ?&gt;
    </td>
All you have to do is compare the value from the database with the value of the radio option and if it matches set the $checked variable to TRUE as the third parameter of the set_radio() function instructs the function to check the radio by default.
#6

[eluser]barrysampson[/eluser]
Ah, now that makes more sense to me (which as I'm such a n00b with CI, is saying something).

Thanks again!
#7

[eluser]Evil Wizard[/eluser]
lol I cant believe I missed off the crucial part of the answer you asked for first time




Theme © iAndrew 2016 - Forum software by © MyBB