PHP - Testing if radio button is checked or not
<p>Im looping my result from the database in a foreach loop .... i want to check if a radiobutton in the database is checked, if it is, check the radio button, if not leave it blank.</p>

<p>I have the following code which sort of works, if i have 4 values checked in the database, it will print out 4 versions of the same radiobutton.</p>

<p>I have to use foreach loops aswell which is getting on my nerves, any help would be great ... code is as follows:</p>

<pre><code>&lt;?php
$i = 0;
$output = "";
foreach($roles as $row){
    if($i &gt; 0){
        $i = 0;
    }
    if($i == 0) {
        $output .= "&lt;div class='box'&gt;";
    }

    $output .= '&lt;div class="row"&gt;';

    $test = "_".$row-&gt;key;
    foreach($positions as $position){
        if($test == $position-&gt;role_key){
            $output .= '    &lt;input name="_'.$row-&gt;key.'" type="radio" id="'.$row-&gt;key.'" class="radio" checked /&gt;';
        } else {
            $output .= '    &lt;input name="_'.$row-&gt;key.'" type="radio" id="'.$row-&gt;key.'" class="radio" /&gt;';
        }
    }                                   

    $output .= '    &lt;label for="'.$row-&gt;key.'" style="text-transform: lowercase;"&gt;'.$row-&gt;name.'&lt;/label&gt;';
    $output .= '&lt;/div&gt;';

    if($i ==0) {
        $output .= "&lt;/div&gt;";
    }

    $i++;
}
if($i != 1) {
    $output .= "&lt;/div&gt;";
}
echo $output;
</code></pre>

<p>?></p>
