Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php mysql一个用户多行,嵌套循环_Php_Mysql_Loops_Nested - Fatal编程技术网

Php mysql一个用户多行,嵌套循环

Php mysql一个用户多行,嵌套循环,php,mysql,loops,nested,Php,Mysql,Loops,Nested,我的同事创建了一个SQL select,它输出以下数组: Array ( [0] => Array ( [total_completion_time] => 163

我的同事创建了一个SQL select,它输出以下数组:

            Array
            (
                                    [0] => Array
                                            (
                                    [total_completion_time] => 163
                                    [promotion_id] => 1
                                    [challenge_id] => 1
                                    [userId] => 2
                                    [display_name] => laxadmin
                                    [completion_time] => 163
                                    [completion_time_mins] => 0
                                    [completion_time_secs] => 0
                                    [completion_time_mmss] => 0:0
                                    [score] => 40
            )

    [1] => Array
            (
                                    [total_completion_time] => 345
                                    [promotion_id] => 1
                                    [challenge_id] => 1
                                    [userId] => 1
                                    [display_name] => siteadmin
                                    [completion_time] => 345
                                    [completion_time_mins] => 5
                                    [completion_time_secs] => 45
                                    [completion_time_mmss] => 5:45
                                    [score] => 50
            )

    [2] => Array
            (
                                    [total_completion_time] => 345
                                    [promotion_id] => 1
                                    [challenge_id] => 1
                                    [userId] => 3
                                    [display_name] => mdeville
                                    [completion_time] => 345
                                    [completion_time_mins] => 0
                                    [completion_time_secs] => 0
                                    [completion_time_mmss] => 0:0
                                    [score] => 0
            )

    [3] => Array
            (
                                    [total_completion_time] => 943
                                    [promotion_id] => 1
                                    [challenge_id] => 1
                                    [userId] => 4
                                    [display_name] => Matthew DeVille
                                    [completion_time] => 163
                                    [completion_time_mins] => 2
                                    [completion_time_secs] => 43
                                    [completion_time_mmss] => 2:43
                                    [score] => 90
            )

    [4] => Array
            (
                                    [total_completion_time] => 943
                                    [promotion_id] => 1
                                    [challenge_id] => 3
                                    [userId] => 4
                                    [display_name] => Matthew DeVille
                                    [completion_time] => 90
                                    [completion_time_mins] => 1
                                    [completion_time_secs] => 30
                                    [completion_time_mmss] => 1:30
                                    [score] => 30
            )

    [5] => Array
            (
                                    [total_completion_time] => 943
                                    [promotion_id] => 1
                                    [challenge_id] => 4
                                    [userId] => 4
                                    [display_name] => Matthew DeVille
                                    [completion_time] => 90
                                    [completion_time_mins] => 1
                                    [completion_time_secs] => 30
                                    [completion_time_mmss] => 1:30
                                    [score] => 35
            )

    [6] => Array
            (
                                    [total_completion_time] => 943
                                    [promotion_id] => 1
                                    [challenge_id] => 5
                                    [userId] => 4
                                    [display_name] => Matthew DeVille
                                    [completion_time] => 300
                                    [completion_time_mins] => 5
                                    [completion_time_secs] => 0
                                    [completion_time_mmss] => 5:0
                                    [score] => 30
            )

    [7] => Array
            (
                                    [total_completion_time] => 943
                                    [promotion_id] => 1
                                    [challenge_id] => 6
                                    [userId] => 4
                                    [display_name] => Matthew DeVille
                                    [completion_time] => 300
                                    [completion_time_mins] => 5
                                    [completion_time_secs] => 0
                                    [completion_time_mmss] => 5:0
                                    [score] => 20
            )
)

他还提供了以下代码来循环遍历结果,并创建具有以下标题的表:

用户| 挑战1次| 挑战2次| 挑战3次| 挑战4次| 挑战5次| 挑战6次| 挑战7次| 总时间|

<?php
for ($x=0; $x < $results_length; $x++) {
    $row = $results[$x]; // get next row from results
    $userId = $row["userId"]; // get userId from row

    // by default, we'll expect to continue to build UI display for current user
    $beginNewUserDisplay = false;

    if ($userId !== $currentUserId) {
        // if not first user, we need to close the current table row
        if ($currentUserId != -1) {
        // if we haven't filled in one table cell for each possible result
            // do that now...
            while ($challengesCtr <= $TOTAL_CHALLENGES_CT) {
                // this means we're missing results we need
                // to properly display the table so we add in blank cells
                echo "<td></td>";
                $challengesCtr = $challengesCtr+1;
            }

            // display total time
            // TODO: verify this is the correct display value
            echo "<td>".$row["total_completion_time"]. "-" .$row['userId']."</td>";
            echo "</tr>";
        }

    // Regardless, we need to start a new table row
    // no more results for previous user, starting UI display for new user
    $currentUserId = $userId;
    $beginNewUserDisplay = true;

    // reset the results counter
    $challengesCtr = 1;

    }



    if ($beginNewUserDisplay == true) {
        // start UI for new User's results
        echo "<tr>";
        echo "<td>".$row['userId']."</td>";
        echo "<td>".$row['display_name']. "-" .$row['userId']."</td>";  
    } else {
        // continue UI for current User's results
        $challengeId = $row["challenge_id"];
        while ($challengesCtr < $challengeId) {
            // if this executes it means we're missing results
            // we need to properly display the table so we add in blank cells
            echo "<td></td>";
            $challengesCtr = $challengesCtr+1;
        }

        // echo table cell current challenge time here
        // TODO: verify this is the correct display value
        echo "<td>".$row['completion_time_mmss']."</td>";

        // increment our results counter
        $challengesCtr = $challengesCtr+1;
    }

}
?>

问题是循环跳过了第一个用户的大部分数据,而表缺少最后两列

您可以在此处看到结果:


我通常可以找出嵌套循环,但这一个让我瘫痪。

似乎您还没有设置第一个$currentUserId。查看SQL I recon会有很大帮助,因为它可能与连接或SQL的设计方式有关。

什么是$TOTAL\u CHALLENGES\u CT?您的链接不起作用,您到底想显示什么,表中的所有数据?