CodeIgniter Forums
Setting radio button values from database for edit form - 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: Setting radio button values from database for edit form (/showthread.php?tid=19600)



Setting radio button values from database for edit form - El Forum - 06-12-2009

[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.


Setting radio button values from database for edit form - El Forum - 06-12-2009

[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


Setting radio button values from database for edit form - El Forum - 06-12-2009

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


Setting radio button values from database for edit form - El Forum - 06-12-2009

[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.


Setting radio button values from database for edit form - El Forum - 06-12-2009

[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.


Setting radio button values from database for edit form - El Forum - 06-12-2009

[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!


Setting radio button values from database for edit form - El Forum - 06-12-2009

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