0

如果复选框中的值与数据库中的值相同,我尝试在我的复选框中添加选中:

My database :
Column = skin_type 
Value = Normal, Dry, Oily

My Default html checkbox :
<input type="checkbox" name="skin_type[]" value="Normal">Normal &nbsp;
<input type="checkbox" name="skin_type[]" value="Dry">Dry &nbsp;
<input type="checkbox" name="skin_type[]" value="Oily">Oily &nbsp;
<input type="checkbox" name="skin_type[]" value="Combination">Combination &nbsp;
<input type="checkbox" name="skin_type[]" value="Sensitive">Sensitive

所以如果值是Normal, Dry, Oily 复选框,这个值将被选中,如下所示

在此处输入图像描述

下面是我当前的 php 脚本(我尝试先分解数据,但不确定如何添加检查值是否与数据库中的值相等):

<?php 
$query_skin_type = mysql_query("SELECT skin_type FROM `customers` WHERE customers_id='".$_GET['cID']."'");
while($info = mysql_fetch_array($query_skin_type)) {

$skin_type = explode(", ", $info['skin_type']);

    foreach ($skin_type as $value) {
    $value.'<br>';
    }
}
?>
4

4 回答 4

1

您可以编写一个小函数来检查您的 $skin_type 数组。例如:

$skin_type = explode(", ", $info['skin_type']);

function isChecked($val, $arr){
  if(in_array($val, $arr)){
    echo 'checked';
  }
}

<input type="checkbox" name="skin_type[]" value="Normal" checked="<?php isChecked('Normal', $skin_type); ?>">Normal &nbsp;
//etc...

请注意,我会以完全不同的方式生成标记,但这无需任何重构就足够了。

于 2012-10-09T05:27:41.237 回答
0
<input type="checkbox" name="skin_type[]" value="Normal" 
<?php
foreach ($skin_type as $value) {
if($value == "Normal") { echo "checked='checked'"; }
}
?> >Normal &nbsp;

<input type="checkbox" name="skin_type[]" value="Dry" <?php
foreach ($skin_type as $value) {
if($value == "Dry") { echo "checked='checked'"; }
}
?> >Dry &nbsp;

<input type="checkbox" name="skin_type[]" value="Oily"  
<?php
foreach ($skin_type as $value) {
if($value == "Oily") { echo "checked='checked'"; }
}
?> 
>Oily &nbsp;

<input type="checkbox" name="skin_type[]" value="Combination"  
<?php
foreach ($skin_type as $value) {
if($value == "Combination") { echo "checked='checked'"; }
}
?> 
>Combination &nbsp;

<input type="checkbox" name="skin_type[]" value="Sensitive"  <?php
foreach ($skin_type as $value) {
if($value == "Sensitive") { echo "checked='checked'"; }
}
?> 
>Sensitive
于 2012-10-09T05:42:37.277 回答
0
$skin_type = explode(", ", $info['skin_type']);


<input type="checkbox" name="skin_type[]" value="Normal" <?php if(in_array("Normal",$skin_type)){ ?>?checked="checked" <?php } ?>>Normal &nbsp;
于 2012-10-09T06:04:34.977 回答
0

选中的复选框取决于数据库值

'<input type="checkbox" class="checkbox" name="s_info[]" id="'.$ccid.'-S-'.$value['receipt_no'].'" value="'.$ccid.'-S-'.$value['receipt_no'].'" '. ($value['verified']? "checked":"") .'/>'.

于 2017-10-27T10:38:50.610 回答