在我的MySQL数据库中,我有一个这样的表:
Column Value
----------------------------------
Id int pk autoinc
Name varchar(50)
Choices set('a', 'b', 'c', ...)
... ...
当这个数据被更新时,我希望显示许多复选框,每个复选框代表一个Choices成员。每个复选框都在PHP中创建,类似于:
echo "<input type=Checkbox name=$choiceName[i] value=$choiceName[i]>"
我的问题是如何从表格中创建这些复选框并为其填充(通过选中或不选中)表格中的特定ID。
答案 0 :(得分:4)
我终于找到了解决方案。
为了获取Choice
数据类型set
列中的允许值。您必须执行以下操作:
$result = mysql_query("SHOW COLUMNS FROM Table LIKE 'Choice'");
$line = mysql_fetch_array($result);
//Remove the unwanted characters from the Type
$set = substr($line['Type'],5,strlen($line['Type'])-7);
//An array containing all allowed members from the Choice set
$choices = preg_split("/','/",$set);
$result = mysql_query("SELECT * FROM Table WHERE Id=$someId");
$row = mysql_fetch_row($result);
$selected = explode(',', $row[$indexOfChoicesColumn]);
$j=0;
for($i=0; $i<count($days); $i++){
if($j<count($selected) && $selected[$j] == $choices[$i]){
echo $choices[$i]." <input type='checkbox' name='choices[]' value='". $choices[$i] ."' checked> ";
$j++;
}
else echo $choices[$i]." <input type='checkbox' name='choices[]' value='". $choices[$i] ."'> ";
}
答案 1 :(得分:0)
示例:
//or you can obtain $choices = $row['Choices']; from your DB, its the same
$choices = "Example1,Example4,Example10";
$arrChoices = explode(',', $choices);
foreach ($arrChoices as $key => $value) {
echo "<input type='checkbox' name='$value' value='$value'/>";
}
Saludos;)