This is a code pinch from a webpage of my project. Here I want to display user selected categories and then want to display its subjects that belong to the categories. There, users could have more than one category, and it is no problem. I can print all those categories in my first while loop. The problem is when I'm trying to print subjects, they only show one row as a result, but there are more subjects in each category. Can anybody tell me what is happening?
This is my code.
Note: Both queries are working properly. I tried those using a mysql client program.
<?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>';
?>
$rowvariable both for the outer loop and for the inner loop. Try renaming the outer loop's$rowvariable to$outerRowand the inner loop's$rowvariable to$innerRow. This may be the first problem.