这是我的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))
{
$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>
我正在比较数据库中所有主题的列表和数据库中具有相同ID的主题,并将其标记为已选中。 但它只显示第一个选中的复选框并结束循环。因此,唯一选中的复选框仅为代数。我在这里错过了什么?
通过将第二个复选框放在else块而不是while循环下来解决它,就像@dreamster建议的那样。
答案 0 :(得分:0)
我不确定我是否让你做对,但尝试这样的事情,如果你给我们一些变量来自哪里以及为什么要这样做,我们可以帮助你更好?
create table test (code text);
create sequence test_sequence;
create function next_code()
returns text language sql as $$
select format('20151208-1-20-%s', to_char(nextval('test_sequence'), 'FM000'));
$$;
insert into test values (next_code());
insert into test values (next_code());
select * from test;
code
-------------------
20151208-1-20-001
20151208-1-20-002
(2 rows)
答案 1 :(得分:0)
我认为
$query = mysql_query("SELECT * FROM topics WHERE SubjectID = '1'", $connection);
应该是
$query = mysql_query("SELECT * FROM topics", $connection);
也改变了
if($thisSub[$a] == $sub1)
到
if(in_array($sub1, $thisSub))