爆炸复选框循环php

时间:2015-03-07 14:17:31

标签: php html mysql sql arrays

我想要从表“ school_minat ”中分解数据 从“ dayaminat ”进入checbox循环表中的清单。

我已经尝试了,但我的清单只显示了一个。表格中的数据minat_id school_minat 4 data = smk2,smk3,smk1,smk4

<center> 
<?php $sql = "select * from dayaminat";
$rs = mysql_query($sql);
$i = 0;
while($row = mysql_fetch_array($rs)){  // Looping data from Table "dayaminat"
    ?>
    <input name='minat[]' 
    <?php 
    $values = $result['minat_id']; // Data "minat_id" already Selected in Database table "school_minat"
    $array_of_values = explode(",", $values); //Explode Data "Minat"  already Selected in Database  table "school_minat"
    if (in_array($row['minat_id'],$array_of_values)) {
    echo 'checked="checked"';
    } 
    ?> 
    value='<?php echo $row['minat_id']?>'  type='checkbox'>&nbsp; <?php echo $row['minat'] ?> &nbsp;
    <?php 
    $i ++;
}?>
</center>

帮帮我。谢谢:)

2 个答案:

答案 0 :(得分:2)

您必须循环播放$array_of_values

<center> 
<?php $sql = "select * from dayaminat";
$rs = mysql_query($sql);

while($row = mysql_fetch_array($rs)){  // Looping data from Table "dayaminat"
    $values = $result['minat_id']; // Data "minat_id" already Selected in Database table "school_minat"
    $array_of_values = explode(",", $values); //Explode Data "Minat"  already Selected in Database  table "school_minat"

    for($i = 0; $i < count($array_of_values); $i++) {
    echo "<input name='minat[]' ";
    if (in_array($row['minat_id'], $array_of_values)) {
    echo 'checked="checked"';
    } 
    ?> 
    value='<?php echo $array_of_values[$i]; ?>'  type='checkbox'>&nbsp; <?php echo $array_of_values[$i]; ?> &nbsp;
    <?php 
  }
}?>
</center>

答案 1 :(得分:1)

您只能通过数据库行进行循环。因为只有1行,所以只得到1个结果。

所以尝试循环遍历数组!可能使用foreach循环..

<center> 
<?php $sql = "select * from dayaminat";
$rs = mysql_query($sql);
$i = 0;
while($row = mysql_fetch_array($rs)){  // Looping data from Table "dayaminat"

    $values = $result['minat_id']; // Data "Minat" already Selected in Database table "school_minat"
    $array_of_values = explode(",", $values); //Explode Data "Minat"  already Selected in Database  table "school_minat"?>
    foreach ($array_of_values as $arrayItem){

        <input name='minat<?php echo $i;?>' 
        <?php 

        if (in_array($row['minat_id'],$arrayItem)) {
        echo 'checked="checked"';
        } 
        ?> 
        value='<?php echo $arrayItem ?>'  type='checkbox'>&nbsp; <?php echo $arrayItem ?> &nbsp;
        <?php 
        $i ++;
    }
}?>
</center>