thara
thara

Reputation: 39

Inner while loop not working

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>'; 
?>

Upvotes: 0

Views: 321

Answers (3)

Punit
Punit

Reputation: 1120

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.

Upvotes: 0

Thorsten Dittmar
Thorsten Dittmar

Reputation: 56697

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.

Upvotes: 1

Rohit Choudhary
Rohit Choudhary

Reputation: 2269

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

Upvotes: 0

Related Questions