douliao5467 2015-12-08 09:54
浏览 104
已采纳

从数据库中读取值后显示已选中和未选中的复选框列表不起作用

Here is my PHP code:

  <?php 
include ('../connection.php');
  $query00 = mysql_query("SELECT * FROM ta_subjects WHERE USER_ID='$login_session' AND Subject_ID='00000001'",$connection);
    while ($row00 = mysql_fetch_array($query00))
              { 
                $thisSub[] = $row00['TopicName'];
              }
    $arrlength = count($thisSub);
    ?>
<!--=====Math=======-->
      <div class="tab-content">
        <div id="math" class="tab-pane fade in active"><br>
          <?php
            include '../connection.php';
            $query = mysql_query("SELECT * FROM topics WHERE SubjectID = '1'",$connection);
            while ($row = mysql_fetch_array($query))
              { 
                $sub1 = $row['TopicName']; 
                ?>
                <div class="col-sm-6">
                <?php
                $a=0;
                do
                {
                  if($thisSub[$a] == $sub1)
                  {
                    ?>
                    <input type="checkbox" class="sub" name="checklist1[]" value="<?php echo $sub1;?>" disabled checked>
                    <?php
                    $a = $a + 1;
                  }
                  else
                    $a = $a + 1;
                }while($arrlength == $a);
                ?>
                    <input type="checkbox" class="sub" name="checklist1[]" value="<?php echo $sub1;?>" disabled>
                    <label><?php echo $sub1;?></label>
                </div>
                <?php
            }
          ?>
        </div>

It should look like this

I am comparing the list of all subjects from the database and the subjects with the same ID from the database also and mark it as checked. But it only displays the first checked check box and ends the loop. So the only checked check box is only Algebra. What am I missing here?


Solved

Solved it by putting the second check box in the else block instead of under the while loop as what @dreamster suggested.

  • 写回答

2条回答 默认 最新

  • duanbicheng3345 2015-12-08 10:31
    关注

    I think

    $query = mysql_query("SELECT * FROM topics WHERE SubjectID = '1'", $connection);
    

    should be

    $query = mysql_query("SELECT * FROM topics", $connection);
    

    also change

    if($thisSub[$a] == $sub1)
    

    to

    if(in_array($sub1, $thisSub))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘