English 中文(简体)
内部循环不工作, 而循环无效。 它限制为仅一行 [重复]
原标题:Inner while loop not working.. it limit to only one row [duplicate]
  • 时间:2012-05-21 08:20:13
  •  标签:
  • php
  • mysql
This question already has answers here:
Closed 11 years ago.

Possible Duplicate:
Inner while loop not working

这是一个来自我项目网页的代码抓取。 在这里我要显示用户选择的分类, 然后显示属于分类的主体。 在那里, 用户可能拥有超过一个分类, 并且我可以在第一个分类中打印所有这些类别, 环绕... 但问题是当 Im 试图打印主题时, 它只有一行。 每个分类中有更多的主题。 有人能告诉我发生了什么吗?

这是我的代码。 注意: 两个查询都正常工作。 我尝试了这些用户的 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>  . "
";

                                    $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> 

如有任何意见,将不胜感激。

感谢您,感谢您

问题回答

由于您将内部循环中的行拖进关联阵列,且密钥是 类别_id , 每一次迭代都将取代上一个循环的结果。 我认为您应该使用 Mysqli_fetch_row () () 。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签