While循环内的循环不循环

时间:2016-01-19 01:47:37

标签: php mysql while-loop

我有一个php页面,上面有手风琴标签。每个标签的标题标题应为类别的标题。在数据库中有10个类别。

每个标签应列出该特定类别中的所有玩家。因此,手风琴tab1将具有“类别1”的标题,并且包括来自“类别1”中的数据库的所有玩家的列表。这是在两个单独的php页面中完成的。

第一页是手风琴面板主体,它应遍历数据库并为每个类别创建一个新面板。然后在这个面板页面中为玩家提供“包含”。

我的问题是,面板While循环只进行一次循环,并为类别1创建面板,但从不进入“类别2”。不知何故的原因是球员的“PHP包括”。如果我为玩家注释"include 'includes/player-select.php';"以便它只为类别创建标签,那么它将遍历整个数据库并创建10个标签。包括玩家将仅在第一个类别选项卡中停止循环。

有想法吗?

这是Panel.php

        <?php
    $query = "SELECT catID,cat_name,categoryColor FROM Categories";
    $result = mysqli_query($conn, $query);

    if($result === FALSE) { 
        die(mysqli_error());
        }

    while ($row = mysqli_fetch_array($result)) {

        $cat_id = $row['catID'];
        $catName = $row['cat_name'];
        $catColor = $row['categoryColor'];

        //  BEGIN PANEL 
        echo '<div class="panel panel-default">';
            echo '<a data-toggle="collapse" data-parent="#accordion" href="#collapse' . $cat_id .'">';
                echo '<div class="panel-heading" style="background-color: ' . $catColor . '; color: #2b2c31;">';
                    echo '<h4 class="panel-title">';
                        echo $catName . '<span class="glyphicon glyphicon-chevron-down caret-right"></span>';
                    echo '</h4>';
                echo '</div>';
            echo '</a>'; // END Panel Title Bar
        // BEGIN Panel Body
            echo '<div id="collapse' . $cat_id . '" class="panel-collapse collapse in">';
                echo '<div class="panel-body votes">';

//       BEGIN Player1Select.php 
                    include 'includes/player-select.php';
                    //       END Player1Select.php 

                            echo '</div>'; // END panel-body
                    echo '</div>'; // END collapse' $cat_id 
            echo '</div>'; // END panel panel-default 
    //  END PANEL   
    }
?>

AND现在这是player-select.php

    <div class="col-md-4 text-center">
<div class="tab-inner">
    <div class="form-group">
        <form action="" method="post">
            <div class="select-box-left">

                <?php
                    //execute the SQL query and return records
                    $query = "SELECT first_name,last_name, playerID, categoryID FROM Players WHERE categoryID = '$cat_id' ORDER BY last_name";
                    $result = mysqli_query($conn, $query);

                    if($result === FALSE) { 
                        die(mysqli_error());
                    }

                    while ($row = mysqli_fetch_array($result)) {

                        $playerID = $row['playerID'];
                        $playerFN = $row['first_name'];
                        $playerLN = $row['last_name'];
                        $playerCatID = $row['categoryID'];

                        echo '<div class="selection1" value=' . $playerFN . $playerLN . '>' . $playerFN . " " . $playerLN ."</div>";
                    }

                ?>
                </div>
        </form>
    </div><!-- END form-group -->
                                </div> <!-- END tab-inner -->
                            </div> <!-- END col-md-4 -->

                <!-- BEGIN Player Images -->
                            <div class="col-md-4 text-center">
<div class="tab-inner">
    <img src="images/players/3_image.png" class="img-responsive img-left" alt="Vote" width="160" height="116">
    <img src="images/players/2_image.png" class="img-responsive img-right" alt="Vote" width="160" height="116">
    <img src="images/vs.png" class="img-responsive img-middle" alt="vs" width="45" height="45">
                                </div>
<div id="btnVote"><button type="submit" class="btn btn-primary" id="voteButton" disabled>VOTE NOW</button></div>
                            </div>
                <!-- END Player Images -->

                            <div class="col-md-4 text-center">
<div class="tab-inner">
    <div class="form-group">
            <div class="select-box-right">
                <?php
                    //execute the SQL query and return records
                    $query = "SELECT first_name,last_name, playerID, categoryID FROM Players WHERE categoryID = '$cat_id' ORDER BY last_name";
                    $result = mysqli_query($conn, $query);

                    if($result === FALSE) { 
                        die(mysql_error()); // TODO: better error handling
                    }

                    //fetch tha data from the database
                    while ($row = mysqli_fetch_array($result)) {

                        $playerID = $row['playerID'];
                        $playerFN = $row['first_name'];
                        $playerLN = $row['last_name'];
                        $playerCatID = $row['categoryID'];

                        echo '<div class="selection2" value=' . $playerFN . $playerLN . '>' . $playerFN ." " . $playerLN . "</div>";
                    }
                ?>
                </div>
    </div><!-- END form-group -->
                                </div> <!-- END tab-inner -->
                            </div> <!-- END col-md-4 -->

2 个答案:

答案 0 :(得分:0)

我有两种可能的解决方案:

1。)可能是因为在你的while循环结束大括号之后你有一个分号。

2。)每当我使用MySQL_fetch_array时,我都会对$ row变量进行索引。

像:

$query = "SELECT catID,cat_name,categoryColor FROM Categories";

                                                while ($row = mysqli_fetch_array($result)) {

                                                $cat_id = $row['catID'];
                                                $catName = $row['cat_name'];
                                                $catColor = $row['categoryColor'];

我的例子

while ($row = mysqli_fetch_array($result)) {

                                                $cat_id = $row[0];
                                                $catName = $row[1];
                                                $catColor = $row[2];

答案 1 :(得分:0)

知道了。发生的事情是在所有三个循环中都使用了相同的$变量,$ query,$ row ....所以原来的while循环变量在其他两个完成时被重置为false。所以答案是重命名$ variables。