内部while循环不起作用 [英] Inner while loop not working

查看:75
本文介绍了内部while循环不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我项目网页上的代码夹.在这里,我要显示用户选择的类别,然后要显示其属于这些类别的主题.在那里,用户可以有多个类别,这没问题.我可以在第一个while循环中打印所有这些类别.问题是当我尝试打印主题时,它们仅显示一行,但是每个类别中都有更多主题.谁能告诉我发生了什么事?

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?

这是我的代码.
注意:这两个查询均正常运行.我尝试使用mysql客户端程序的人.

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

推荐答案

将我的评论变成答案:

只有一秒钟的时间,我可以看到您在外部循环和内部循环中都使用了$row变量.尝试将外部循环的$row变量重命名为$outerRow,将内部循环的$row变量重命名为$innerRow.这可能是第一个问题.这也可能适用于其他变量,例如$result变量.

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.

这篇关于内部while循环不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆