0

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'] . '" />&nbsp;&nbsp;' . $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>&nbsp;</td>", ( 2 - ( $i % 2 ) ) );
    }
    echo "</tr></table>";   
} 
echo '</fieldset>'; 
?>
5
  • Only looking at your code for a second I can see that you're using the $rowvariable both for the outer loop and for the inner loop. Try renaming the outer loop's $row variable to $outerRow and the inner loop's $row variable to $innerRow. This may be the first problem. Commented May 21, 2012 at 6:43
  • @ThorstenDittmar Your comment should be an answer ;) . Commented May 21, 2012 at 6:45
  • chech this query for while list $qy = "SELECT cs.* FROM category_subject cs INNER JOIN category c ON cs.category_id = c.category_id INNER JOIN subject s ON cs.subject_id = s.subject_id WHERE cs.category_id = $categoryId"; Commented May 21, 2012 at 6:47
  • @SoboLAN: I thought I should post it as a comment as I didn't try anything ;-) Commented May 21, 2012 at 6:47
  • @SamArulRaj: Why? The OP already said that both SQL queries are working fine when executed directly. Commented May 21, 2012 at 6:48

3 Answers 3

1

Turning my comment into an answer:

Only looking at your code for a second I can see that you're using the $row variable both for the outer loop and for the inner loop. Try renaming the outer loop's $row variable to $outerRow and the inner loop's $row variable to $innerRow. This may be the first problem. This may apply to other variables, too, like for example the $result variable.

Sign up to request clarification or add additional context in comments.

1 Comment

Could you please change your question so that it reflects the new source code?
0

user different variable name for inner loop $result and $row like $inresult and $inrow

Comments

0

Because you have used a same variable $row in both of the loops, $row[] in inner loop could be taking reference of outer one and in that result set the variable you are looking for not present so that not printing anything from inner loop, please change the variable of inner or outer loop.

1 Comment

just for debugging :first try to find how many records returned by the inner query (echo no. of records for every iteration) based on the outer one if every thing is working fine than go for the html structure.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.