如何使用mySql中的数据选择单选按钮

时间:2015-09-17 12:38:19

标签: php html codeigniter

我想用mySql中的数据选择编辑形式的单选按钮,如何用单选按钮值写入?

这是我的代码:

foreach($arrAnswer as $ans)
{           
  <input name = "answer_'.$i.'" type="radio"  value="'..'"> '.$ans->descript.'<br>
}

谢谢你的帮助

3 个答案:

答案 0 :(得分:0)

Use can set radio box checked as shown below

 if ($fetch['sex'] = 'male')
    {
    echo "<td><input type='radio' name='sex' value='male' id='sex' checked> Male <input type='radio' name='sex' value='female' id='sex'> Female </td>";
    }
    else
    {
    echo "<td><input type='radio' name='sex' value='male' id='sex'> Male <input type='radio' name='sex' value='female' id='sex' checked> Female </td>";
    }

答案 1 :(得分:0)

所以我认为这应该有用

$dummyAnswer = new stdClass();
$dummyAnswer->value = "somevalue";
$dummyAnswer->description = "some description";

$secondDummyAnswer = new stdClass();
$secondDummyAnswer->value = "second somevalue";
$secondDummyAnswer->description = "second some description";

// just a dummy, here would be your database result array
$arrAnswer = array(
    $dummyAnswer,
    $secondDummyAnswer
);

//wherever that comes from
$selectedValue = "somevalue";

foreach($arrAnswer as $index => $ans) {
    $checked = ( $ans->value == $selectedValue )? "checked" :"";

    echo '<input name = "answer_'.$index.'" type="radio"  value="'.$ans->value.'" '.$checked.'> '.$ans->description.'<br>';
}

答案 2 :(得分:0)

这是三元运算符派上用场的地方:

$checked = (statement here) ? 'checked' : '';

(statement here)替换为您的布尔值(1或0),例如$ans->field_name_here

然后将$checked放入<input>标记内,并确保输入值为1

<input name = "answer_'.$i.'" type="radio"  value="1" '.$checked.'> '.$ans->descript.'<br>