这是我项目网页中的一段代码。在这里,我想显示用户选择的类别,然后想显示属于该类别的主题。在那里,用户可以拥有多个类别,这没问题。我可以在我的第一个 while 循环中打印所有这些类别。问题是当我尝试打印主题时,它们只显示一行,但每个类别中有更多主题。谁能告诉我发生了什么?
这是我的代码。
注意:两个查询都正常工作。我尝试了使用 mysql 客户端程序的那些。
<?php
require_once ('../../includes/config.inc.php');
require_once( MYSQL1 );
$q = "SELECT institute_category.category_id, category_name
FROM institute_category
INNER JOIN category ON institute_category.category_id = category.category_id
WHERE institute_category.institute_id = $instituteId";
$r = mysqli_query( $dbc, $q);
while ( $row = mysqli_fetch_array ( $r, MYSQLI_ASSOC) ) {
$categoryId = $row['category_id'];
$category = $row['category_name'];
echo '<fieldset class="alt">
<legend><span>Category : <em style="color: red;">' . $category .
'</em></span></legend>';
$qy = "SELECT category_subject.category_id, category_subject.subject_id, subjects
FROM category_subject
INNER JOIN category ON category_subject.category_id = category.category_id
INNER JOIN subject ON category_subject.subject_id = subject.subject_id
WHERE category_subject.category_id = $categoryId";
$result = mysqli_query( $dbc, $qy);
$c = $i = 0;
echo '<table class="form_table" ><tr>';
while($row = mysqli_fetch_array( $result, MYSQLI_ASSOC )){
// if remainder is zero after 2 iterations (for 2 columns) and when $c > 0, end row and start a new row:
if( ($c % 2) == 0 && $c != 0){
echo "</tr><tr>";
}
echo '<td width="50%"><input type="checkbox" name="subject[]" value="' .
$row['category_id'] . ":" . $category . ":" . $row['subject_id'] .
":". $row['subjects'] . '" /> ' . $row['subjects'] .
'</td>' . "\n";
$c++;
} // while..
// in case you need to fill a last empty cell:
if ( ( $i % 2 ) != 0 ){
// str_repeat() will be handy when you want more than 2 columns
echo str_repeat( "<td> </td>", ( 2 - ( $i % 2 ) ) );
}
echo "</tr></table>";
}
echo '</fieldset>';
?>