0

可能重复:
内部while循环不起作用

这是我项目网页中的一段代码。在这里,我想显示用户选择的类别,然后想显示属于类别的主题。在那里,用户可能有超过 1 个类别,没问题我可以在我的第一个 while 循环中打印所有这些类别……但问题是当我尝试打印主题时,结果只有一行……还有更多主题在每个类别中。谁能告诉我发生了什么?

这是我的代码....注意:两个查询都正常工作..我尝试了那些用户 mysql 客户端程序。

    <?php

    require_once ('../../includes/config.inc.php');
    require_once( MYSQL1 );

    $outQuery = "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";  

    $outResult = mysqli_query( $dbc, $outQuery);


    while ( $outRow = mysqli_fetch_array ( $outResult, MYSQLI_ASSOC) )   {

        $categoryId = $outRow['category_id']; 
        $category = $outRow['category_name']; 

        echo '<fieldset class="alt">
                <legend><span>Category : <em style="color: red;">' . $category . '</em></span></legend>';



                    $innerQuery = "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";  

                    $innerResult = mysqli_query( $dbc, $innerQuery);

                    $c = $i = 0;

                        echo '<table class="form_table" ><tr>'; 

                                while($innerRow = mysqli_fetch_array( $innerResult, 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="' . $innerRow['category_id'] . ":" . $category . ":"  . $innerRow['subject_id'] . ":". $innerRow['subjects'] . '" />&nbsp;&nbsp;' . $innerRow['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>'; 
?>

这是我的 HTML

    good<fieldset class='alt'>
<legend><span>Category : <em style='color: red;'>grade 1 - 4</em></span></legend>
    <table class='form_table'><tr>
    <tr>
        <td width='50%'><input type='checkbox' name='subject[]' value='2:grade 1 - 4:2:Art' />&nbsp;&nbsp;Art</td>
        <td width='50%'><input type='checkbox' name='subject[]' value='2:grade 1 - 4:3:Art & Craft' />&nbsp;&nbsp;Art & Craft</td>
    </tr>
    <tr>
        <td width='50%'><input type='checkbox' name='subject[]' value='2:grade 1 - 4:4:Bialogy' />&nbsp;&nbsp;Bialogy</td>
        <td width='50%'><input type='checkbox' name='subject[]' value='2:grade 1 - 4:5:Buddhism' />&nbsp;&nbsp;Buddhism</td>
    </tr>
    </table>
<fieldset class='alt'>
    <legend><span>Category : <em style='color: red;'>grade 5 (scholarship exam)</em></span></legend>
        <table class='form_table'><tr>
            <tr>
            <td width='50%'><input type='checkbox' name='subject[]' value='3:grade 5 (scholarship exam):1:Agro & Food Technology' />&nbsp;&nbsp;Agro & Food Technology</td>
            <td width='50%'><input type='checkbox' name='subject[]' value='3:grade 5 (scholarship exam):2:Art' />&nbsp;&nbsp;Art</td>
            </tr>
    <tr>
        <td width='50%'><input type='checkbox' name='subject[]' value='3:grade 5 (scholarship exam):3:Art & Craft' />&nbsp;&nbsp;Art & Craft</td>
        <td width='50%'><input type='checkbox' name='subject[]' value='3:grade 5 (scholarship exam):4:Bialogy' />&nbsp;&nbsp;Bialogy</td>
    </tr>
    </table>
        <fieldset class='alt'>
    <legend><span>Category : <em style='color: red;'>grade 6 - 10</em></span></legend>
        <table class='form_table'><tr>
    <tr>
        <td width='50%'><input type='checkbox' name='subject[]' value='4:grade 6 - 10:2:Art' />&nbsp;&nbsp;Art</td>
        <td width='50%'><input type='checkbox' name='subject[]' value='4:grade 6 - 10:3:Art & Craft' />&nbsp;&nbsp;Art & Craft</td>
    </tr>
    <tr>
        <td width='50%'><input type='checkbox' name='subject[]' value='4:grade 6 - 10:4:Bialogy' />&nbsp;&nbsp;Bialogy</td>
        <td width='50%'><input type='checkbox' name='subject[]' value='4:grade 6 - 10:5:Buddhism' />&nbsp;&nbsp;Buddhism</td>
    </tr>
    </table>
</fieldset> 

任何意见都非常感谢..

谢谢你

4

1 回答 1

0

Since you fetch your rows in the inner loop into an associative array, and the key is the category_id, every iteration will replace the result of the previous one. I think you should use mysqli_fetch_row() instead.

于 2012-05-21T09:08:06.580 回答