Php 从数据库读取值后显示选中和未选中复选框列表无效
以下是我的PHP代码:Php 从数据库读取值后显示选中和未选中复选框列表无效,php,mysql,arrays,checkbox,Php,Mysql,Arrays,Checkbox,以下是我的PHP代码: <?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))
<?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>
我不确定我是否正确,但请尝试类似的方法,如果您告诉我们一些变量来自何处,我们可以更好地帮助您,为什么要使用do-while
$a = 0;
// $thisSub = []; put this before you first while loop
while ($row = mysql_fetch_array($query))
{
$sub1 = $row['TopicName']; ?>
<div class="col-sm-6">
<input type="checkbox" class="sub" name="checklist1[]" value="<?php echo $sub1;?>" disabled <?php echo (in_array($sub1, $thisSub)) ? 'checked="checked"' : ''; ?> >
<?php $a++; ?>
<label><?php echo $sub1;?></label>
</div>
<?php
}
$a=0;
//$thisSub=[];先把这个放在你面前,然后循环
while($row=mysql\u fetch\u array($query))
{
$sub1=$row['TopicName'];?>
>
我想
$query = mysql_query("SELECT * FROM topics WHERE SubjectID = '1'", $connection);
应该是
$query = mysql_query("SELECT * FROM topics", $connection);
也改变
if($thisSub[$a] == $sub1)
到
在条件看起来不正确时执行,并且在哪里定义了$arrlength
?在哪里定义了$thisSub
等。选择*看起来不对劲。我编辑了我的帖子。这让我烦了快3个小时了。请帮帮我。主题应该在那里,这样它只能得到与该主题相等的主题。这是主题的类别。@rodortega哦,我明白了。那么你确定有足够多的主题主题主题主题主题为1吗?@rodortega尝试更改if属性正如我所提到的。仍然不起作用。它说“in_array()期望参数2为数组,在..中为null”,这意味着没有满足条件的行:USER_ID='$login\u session'
。您可以通过声明$thisSub=array()
来避免错误。您正在将数组与字符串$thisSub[$a]进行比较==$sub1,你不能只这样做,你必须在\u数组中使用。我已经按照dreamster的要求这样做了。仍然给我错误“在\u数组()中预期参数2为数组,在..中为空。”确保你在$thisSub数组中有项,并按照dreamster的说明,在第一个数组之前定义你的数组,而$thisSub=[];我避免了错误,但复选框的检查不起作用
if(in_array($sub1, $thisSub))